From dd7a86c90e80d4af9b5020fef9ce367aa0d6b0f9 Mon Sep 17 00:00:00 2001 From: Patrick Bethke Date: Mon, 4 Jan 2016 14:30:25 +0100 Subject: [PATCH 001/116] Add abstract AWG base class, a dummy implementation for debugging and tests. --- qctoolkit/hardware/awgs/awg.py | 99 ++++++++++++++++++++++++++++++++++ setup.cfg | 2 +- tests/hardware/awg_tests.py | 20 +++++++ 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 qctoolkit/hardware/awgs/awg.py create mode 100644 tests/hardware/awg_tests.py diff --git a/qctoolkit/hardware/awgs/awg.py b/qctoolkit/hardware/awgs/awg.py new file mode 100644 index 000000000..67c646f16 --- /dev/null +++ b/qctoolkit/hardware/awgs/awg.py @@ -0,0 +1,99 @@ + +from abc import ABCMeta, abstractmethod, abstractproperty +from typing import Dict,List, Tuple, Set +from collections import Ordered +import numpy as np +import logging + + +from qctoolkit.pulses.instructions import InstructionBlock, EXECInstruction + +__all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", "OutOfWaveformMemoryExecption"] + +Program = List[InstructionBlock] + +class AWG(metaclass = ABCMeta): + """An arbitrary waveform generator abstraction class. It keeps track of the AWG state and manages waveforms and programs on the hardware.""" + + @abstractmethod + def upload(self, name: str, program: List[InstructionBlock]): + """Take a name for a program, the program and upload all the necessary waveforms to the AWG hardware. This method should be cheap for programs already on the device and can therefore be used for syncing.""" + + @abstractmethod + def remove(self, name: str, force=False): + """Take the name of a program and remove it from the AWG, deleting all unneeded waveforms in the process.""" + + @abstractproperty + def programs(self) -> Set[str]: + """Return the set of program names, that can currently be executed on the hardware AWG.""" + + # @abstractmethod + # def clean(self) -> None: + # """Delete all waveforms from the hardware AWG that are not needed by the programs on the machine.""" + + @abstractmethod + def run(self, name) -> None: + """Load the program 'name' and either arm the device for running it or run it.""" + + + + +class DummyAWG(AWG): + """Dummy AWG for debugging purposes.""" + def __init__(self, memory=100): + self.__programs = {} # contains program names and programs + self.__waveform_memory = [None for i in memory] + self.__waveform_indices = {} # dict that maps from waveform hash to memory index + self.__program_wfs = {} # contains program names and necessary waveforms indices + + def add_waveform(self, waveform): + try: + index = self.__waveform_memory.index(None) + except ValueError: + raise OutOfWaveformMemoryException + self.__waveform_memory[index] = waveform + self.__waveform_indices[hash(waveform)] = index + return index + + def upload(self, name, program, force=False): + if name in self.programs: + if not force: + raise ProgramOverwriteException(name) + else: + self.remove(name) + self.upload(name, program) + else: + self.__programs[name] = program + exec_blocks = filter(lambda x: type(x) == EXECInstruction, program) + indices = frozenset(add_waveform(block.waveform) for block in exec_blocks) + self.__program_wfs[name] = indices + + def remove(self,name): + if name in self.programs: + self.__programs.pop(name) + self.program_wfs.pop(name) + self.clean() + + def clean(self): + necessary_wfs = reduce(lambda acc, s: acc.union(s), self.__program_wfs.values(), set()) + all_wfs = set(self.__waveform_indices.values()) + delete = all_wfs - necessary_wfs + for index in delete: + wf = self.__waveform_memory(index) + self.__waveform_indices.pop(wf) + self.__waveform_memory = None + + def programs(self): + return frozenset(self.__programs.keys()) + +class ProgramOverwriteException(Exception): + def __init__(self, name): + super().__init__() + self.name = name + + def __str__(self) -> str: + return "A program with the given name '{}' is already present on the device. Use force to overwrite.".format(self.name) + +class OutOfWaveformMemoryException(Exception): + def __str__(self): + return "Out of memory error adding waveform to waveform memory." diff --git a/setup.cfg b/setup.cfg index 73c7ccf25..315f5fb3b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,3 @@ [pytest] -testpaths = tests/pulses tests/experiment tests/bugs +testpaths = tests/pulses tests/experiment tests/bugs tests/hardware python_files=*_tests.py *_bug.py diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py new file mode 100644 index 000000000..1a393d295 --- /dev/null +++ b/tests/hardware/awg_tests.py @@ -0,0 +1,20 @@ +import unittest +import numpy as np + +import qctoolkit.hardware.awg as awg +import qctoolkit.pulses as pls + +class DummyAWGTest(unittest.TestCase): + def setUp(self): + self.awg = awg.DummyAWG(10) + self.pulse_template = pls.TablePulseTemplate() + self.pulse_template.add_entry('value', 5) + self.sequencer = pls.Sequencer() + for i in range(1,12): + pars = dict(value=i) + sequencer.push(self.pulse_template, pars) + self.program = self.sequencer.build() + + def test_outofmemoryexception(self): + with self.assertRaises(awg.OutOfMemoryException): + self.awg.upload('program', self.program) From d50320aa8632c10a3535503fda909df50dc7f920 Mon Sep 17 00:00:00 2001 From: Patrick Bethke Date: Wed, 13 Jan 2016 15:16:43 +0100 Subject: [PATCH 002/116] add a tektronix awg driver (not complete) and more tests --- qctoolkit/hardware/awgs/__init__.py | 4 + qctoolkit/hardware/awgs/awg.py | 12 ++- qctoolkit/hardware/awgs/tektronix.py | 123 +++++++++++++++++++++++++++ tests/hardware/awg_tests.py | 21 ++++- 4 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 qctoolkit/hardware/awgs/tektronix.py diff --git a/qctoolkit/hardware/awgs/__init__.py b/qctoolkit/hardware/awgs/__init__.py index e69de29bb..df3606d49 100644 --- a/qctoolkit/hardware/awgs/__init__.py +++ b/qctoolkit/hardware/awgs/__init__.py @@ -0,0 +1,4 @@ +__all__ = [ + 'awg', + 'tektronix' +] diff --git a/qctoolkit/hardware/awgs/awg.py b/qctoolkit/hardware/awgs/awg.py index 67c646f16..542505442 100644 --- a/qctoolkit/hardware/awgs/awg.py +++ b/qctoolkit/hardware/awgs/awg.py @@ -23,10 +23,10 @@ def upload(self, name: str, program: List[InstructionBlock]): def remove(self, name: str, force=False): """Take the name of a program and remove it from the AWG, deleting all unneeded waveforms in the process.""" + @abstractproperty def programs(self) -> Set[str]: """Return the set of program names, that can currently be executed on the hardware AWG.""" - # @abstractmethod # def clean(self) -> None: # """Delete all waveforms from the hardware AWG that are not needed by the programs on the machine.""" @@ -35,7 +35,17 @@ def programs(self) -> Set[str]: def run(self, name) -> None: """Load the program 'name' and either arm the device for running it or run it.""" + @abstractproperty + def samplerate(self) -> float: + """Returns the AWG samplerate (needed by the sequencer)""" + + @abstractproperty + def identifier(self) -> str: + """Return a hardware identifier string.""" + @abstractproperty + def outputRange(self) -> Tuple(float, float): + """Return the minimal/maximal voltage the AWG can produce""" class DummyAWG(AWG): diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py new file mode 100644 index 000000000..ca206608a --- /dev/null +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -0,0 +1,123 @@ +from bidict import bidict +import visa +import datetime + +from .awg import AWG, DummyAWG, ProgramOverwriteException, OutOfWaveformMemoryException + + +__all__ = ['TektronixAWG'] + +class TektronixAWG(AWG): + def __init__(self, ip: str, samplerate: float): + self.__programs = {} # holds names and programs + self.__waveform_memory = set() #map index to waveform + self.__program_waveforms = {} # maps program names to set of waveforms used by the program + self.__ip = ip + self.rm = visa.ResourceManager() + self.inst = rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip)) + self.__samplerate = samplerate + self.__scale = 1. + self.__offset = 0. + self.__channel_template = '{0:s}_{1:d}' + self.__channels = [1] # use 2 channels. TODO: fix this + + def rescale_data(self, voltages: np.ndarray) -> np.ndarray: + """Converts an array of voltages to an array of unsigned integers for upload.""" + data = (voltages + self.__offset) / self.__scale + 1 + # scale data to uint14 range + data = (data * (2**13 - 1)) + data[data > 2**14 - 1] = 2**14 - 1 + data = data.astype(np.uint16) + data = marker.astype(np.uint16) * 2**14 + return data + + def waveform2name(self, waveform): + return str(hash(waveform)) + + def add_waveform(self, waveform, offset): + """Samples a Waveform object to actual data and sends it to the AWG.""" + # check if waveform is on the AWG already + if waveform in self.__waveform_memory: + pass + else: + start = datetime.datetime.now() + # first sample the waveform to get an array of data + ts = np.arange(waveform.duration/self.__samplerate) + data = waveform.sample(ts, offset) + wf_name = waveform2name(waveform) + + # now create a new waveform on the awg + total = len(data) + # TODO: also do this for multiple_channels + for c in self.__channels: + name = channel_template.format(wf_name, c) + self.inst.write_ascii_values('WLIST:WAVEFORM:NEW "{0}", {1:d}, INT'.format(name, total)) + chunksize = 65536 + for i, chunk in enumerate(np.split(data, chunksize)): + """This needs a lot of testing!!!""" #TODO: test + self.inst.write_binary_values('WLIST:WAVEFORM:DATA "{0:s}", {1:d}, {2:d}'.format(wf_name, i*chunksize, len(chunk), 2*len(chunk), chunk.astype('uint8'))) + + end = datetime.datetime.now() + duration = end - start + print('Load time for pulse {0}: {1:g}, seconds for {2:d} points.'.format( + wf_name, duration.total_seconds(), total)) + self.__waveform_memory += set(waveform) + + def build_sequence(self, name): + '''Puts a new sequence in the AWG sequence memory''' + length = len(self.__programs[name]) + self.inst.write('SEQUENCE:LENGTH {0:d}'.format(length)) # create new sequence + for i in range(length): + wf_name = waveform2name(waveform) + # set i'th sequence element + # TODO: for multiple channels write the following line for each channel, respectively + for c in __channels: + self.inst.write('SEQUENCE:ELEMENT{0:d}:WAVEFORM{1:d} "{2:s}"'.format(i+1, c+1, wf_name)) + # have sequence go back to index 1 after playback of index N + self.inst.write('SEQUENCE:ELEMENT{0:d}:GOTO:STATE ON'.format(length)) + + + def upload(self, name, program, force=False): + '''Uploads all necessary waveforms for the program to the AWG and create a corresponding sequence.''' + if name in self.programs: + if not force: + raise ProgramOverwriteException(name) + else: + self.remove(name) + self.upload(name, program) + else: + self.__programs[name] = program + exec_blocks = filter(lambda x: type(x) == EXECInstruction, program) + offset = 0 + for block in exec_blocks: + self.add_waveform(block.waveform, offset) + offset += block.waveform.duration + used_waveforms = frozenset([block.waveform for block in exec_blocks]) + self.__program_wfs[name] = used_waveforms + self.build_sequence(name) + + def run(self, name, autorun=False): + # there is only one sequence per devicee, so program the sequence first + self.build_sequence(name) + self.inst.write('AWGCONTROL:RMODE:SEQUENCE') # play sequence + self.inst.write('AWGCONTROL:RUN') + + + def remove(self, name): + if name in self.programs: + self.__programs.pop(name) + self.program_wfs.pop(name) + self.clean() + + def clean(self): + necessary_wfs = reduce(lambda acc, s: acc.union(s), self.__program_wfs.values(), set()) + all_wfs = self.__waveform_memory + delete = all_wfs - necessary_wfs + for waveform in delete: + wf_name = self.waveform2name(waveform) + for c in channel: + name = self.__channel_template.format(wf_name, c) + self.inst.write('WLIST:WAVEFORM:DELETE {0:s}'.format(wf_name)) + self.__waveform_memory.remove(wf) + print('Deleted {0:d} waveforms'.format(len(delete))) + return len(delete) diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 1a393d295..96ae32a23 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -6,7 +6,6 @@ class DummyAWGTest(unittest.TestCase): def setUp(self): - self.awg = awg.DummyAWG(10) self.pulse_template = pls.TablePulseTemplate() self.pulse_template.add_entry('value', 5) self.sequencer = pls.Sequencer() @@ -15,6 +14,22 @@ def setUp(self): sequencer.push(self.pulse_template, pars) self.program = self.sequencer.build() - def test_outofmemoryexception(self): + def test_OutOfMemoryException(self): + dummy = awg.DummyAWG(10) with self.assertRaises(awg.OutOfMemoryException): - self.awg.upload('program', self.program) + dummy.upload('program', self.program) + + def test_ProgramOverwriteException(self): + dummy = awg.DummyAWG(10) + dummy.upload('program', self.program) + with self.assertRaises(awg.ProgramOverwriteException): + dummy.upload('program') + + def test_upload(self): + dummy = awg.DummyAWG(100) + dummy.upload('program',self.program) + memory_part = [None for i in range(89)] + self.assertEqual(dummy._DummyAWG_waveform_memory[11:], memory_part) + self.assertEqual(dummy.programs, set(['program'])) + + From 44e1e5dad535f62ef479f975cdeb8e3a95c98bde Mon Sep 17 00:00:00 2001 From: Patrick Bethke Date: Mon, 14 Mar 2016 15:57:24 +0100 Subject: [PATCH 003/116] fix awg interface --- qctoolkit/hardware/awgs/awg.py | 11 +++-- qctoolkit/hardware/awgs/tektronix.py | 45 ++++++++++++++++----- qctoolkit/pulses/sequence_pulse_template.py | 5 ++- qctoolkit/pulses/table_pulse_template.py | 2 + tests/pulses/sequencing_dummies.py | 4 ++ 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/qctoolkit/hardware/awgs/awg.py b/qctoolkit/hardware/awgs/awg.py index 542505442..93541769d 100644 --- a/qctoolkit/hardware/awgs/awg.py +++ b/qctoolkit/hardware/awgs/awg.py @@ -1,9 +1,8 @@ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict,List, Tuple, Set -from collections import Ordered -import numpy as np -import logging +from typing import Set +from typing import Tuple +from typing import List from qctoolkit.pulses.instructions import InstructionBlock, EXECInstruction @@ -40,11 +39,11 @@ def samplerate(self) -> float: """Returns the AWG samplerate (needed by the sequencer)""" @abstractproperty - def identifier(self) -> str: + def identifier(self): """Return a hardware identifier string.""" @abstractproperty - def outputRange(self) -> Tuple(float, float): + def outputRange(self): """Return the minimal/maximal voltage the AWG can produce""" diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index ca206608a..e33868983 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -1,8 +1,12 @@ from bidict import bidict import visa import datetime +import numpy as np +from functools import reduce +import ipdb from .awg import AWG, DummyAWG, ProgramOverwriteException, OutOfWaveformMemoryException +from qctoolkit.pulses.instructions import EXECInstruction __all__ = ['TektronixAWG'] @@ -13,14 +17,31 @@ def __init__(self, ip: str, samplerate: float): self.__waveform_memory = set() #map index to waveform self.__program_waveforms = {} # maps program names to set of waveforms used by the program self.__ip = ip - self.rm = visa.ResourceManager() - self.inst = rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip)) + self.__rm = visa.ResourceManager() + self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip)) + self.__identifier = self.inst.query('*IDN?') self.__samplerate = samplerate self.__scale = 1. self.__offset = 0. self.__channel_template = '{0:s}_{1:d}' self.__channels = [1] # use 2 channels. TODO: fix this + @property + def outputRange(self): + return (-1, 1) + + @property + def identifier(self): + return self.__identifier + + @property + def programs(self): + return list(self.__programs.keys()) + + @property + def samplerate(self): + return self.__samplerate + def rescale_data(self, voltages: np.ndarray) -> np.ndarray: """Converts an array of voltages to an array of unsigned integers for upload.""" data = (voltages + self.__offset) / self.__scale + 1 @@ -44,14 +65,15 @@ def add_waveform(self, waveform, offset): # first sample the waveform to get an array of data ts = np.arange(waveform.duration/self.__samplerate) data = waveform.sample(ts, offset) - wf_name = waveform2name(waveform) + wf_name = self.waveform2name(waveform) # now create a new waveform on the awg total = len(data) # TODO: also do this for multiple_channels for c in self.__channels: name = channel_template.format(wf_name, c) - self.inst.write_ascii_values('WLIST:WAVEFORM:NEW "{0}", {1:d}, INT'.format(name, total)) + ipdb.set_trace() + self.inst.write('WLIST:WAVEFORM:NEW "{0}", {1:d}, INT'.format(name, total)) chunksize = 65536 for i, chunk in enumerate(np.split(data, chunksize)): """This needs a lot of testing!!!""" #TODO: test @@ -66,12 +88,14 @@ def add_waveform(self, waveform, offset): def build_sequence(self, name): '''Puts a new sequence in the AWG sequence memory''' length = len(self.__programs[name]) + program = self.__programs[name] self.inst.write('SEQUENCE:LENGTH {0:d}'.format(length)) # create new sequence for i in range(length): - wf_name = waveform2name(waveform) + waveform = program[i] + wf_name = self.waveform2name(waveform) # set i'th sequence element # TODO: for multiple channels write the following line for each channel, respectively - for c in __channels: + for c in self.__channels: self.inst.write('SEQUENCE:ELEMENT{0:d}:WAVEFORM{1:d} "{2:s}"'.format(i+1, c+1, wf_name)) # have sequence go back to index 1 after playback of index N self.inst.write('SEQUENCE:ELEMENT{0:d}:GOTO:STATE ON'.format(length)) @@ -79,6 +103,7 @@ def build_sequence(self, name): def upload(self, name, program, force=False): '''Uploads all necessary waveforms for the program to the AWG and create a corresponding sequence.''' + ipdb.set_trace() if name in self.programs: if not force: raise ProgramOverwriteException(name) @@ -87,13 +112,13 @@ def upload(self, name, program, force=False): self.upload(name, program) else: self.__programs[name] = program - exec_blocks = filter(lambda x: type(x) == EXECInstruction, program) + exec_blocks = list(filter(lambda x: type(x) == EXECInstruction, program)) offset = 0 for block in exec_blocks: self.add_waveform(block.waveform, offset) offset += block.waveform.duration used_waveforms = frozenset([block.waveform for block in exec_blocks]) - self.__program_wfs[name] = used_waveforms + self.__program_waveforms[name] = used_waveforms self.build_sequence(name) def run(self, name, autorun=False): @@ -106,11 +131,11 @@ def run(self, name, autorun=False): def remove(self, name): if name in self.programs: self.__programs.pop(name) - self.program_wfs.pop(name) + self.__program_waveforms.pop(name) self.clean() def clean(self): - necessary_wfs = reduce(lambda acc, s: acc.union(s), self.__program_wfs.values(), set()) + necessary_wfs = reduce(lambda acc, s: acc.union(s), self.__program_waveforms.values(), set()) all_wfs = self.__waveform_memory delete = all_wfs - necessary_wfs for waveform in delete: diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index 180df210d..f62646de2 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -93,6 +93,9 @@ def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, if not self.subtemplates: return False + if not conditions: + return False + # obtain first subtemplate (template, mapping_functions) = self.subtemplates[0] @@ -107,7 +110,7 @@ def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, def __map_parameter(self, mapping_function: str, parameters: Dict[str, Parameter]) -> Parameter: external_parameters = mapping_function.variables() external_values = {name: float(parameters[name]) for name in external_parameters} - return mapping_function.evaluate(external_values) + return mapping_function.evaluate(**external_values) def build_sequence(self, sequencer: Sequencer, diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index aa2cc97f3..80261c7ed 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -354,6 +354,8 @@ def build_sequence(self, instruction_block.add_instruction_exec(waveform) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: + if not conditions: + return False return any(parameters[name].requires_stop for name in parameters.keys() if (name in self.parameter_names) and not isinstance(parameters[name], numbers.Number)) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index 527ba22a5..6b6bebf13 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -116,6 +116,10 @@ def duration(self) -> float: def channels(self) -> int: return 1 + @property + def measurement_windows(self): + return [] + def sample(self, sample_times: numpy.ndarray, first_offset: float=0) -> numpy.ndarray: self.sample_calls.append((list(sample_times), first_offset)) if self.sample_output is not None: From f881a206539f2ec0b23339d5fdf836bad2c422d6 Mon Sep 17 00:00:00 2001 From: Patrick Bethke Date: Mon, 14 Mar 2016 15:57:44 +0100 Subject: [PATCH 004/116] change tektronix memory model --- qctoolkit/hardware/awgs/tektronix.py | 156 +++++++++++++++++++++------ 1 file changed, 123 insertions(+), 33 deletions(-) diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index e33868983..a392ebedc 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -3,28 +3,117 @@ import datetime import numpy as np from functools import reduce -import ipdb +import socket +import select +from itertools import chain, repeat +import ipdb from .awg import AWG, DummyAWG, ProgramOverwriteException, OutOfWaveformMemoryException from qctoolkit.pulses.instructions import EXECInstruction -__all__ = ['TektronixAWG'] +__all__ = ['TektronixAWG', 'AWGSocket', 'EchoTestServer'] + +class EchoTestServer(): + def __init__(self, port): + self.port = port + + def run(self): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(('', self.port)) + s.listen(5) + while True: + client, address = s.accept() + data = client.recv(65535) + if data: + client.send(data) + client.close() + +def grouper(n, iterable, padvalue=None): + return zip(*[chain(iterable, repeat(padvalue, n-1))]*n) + + +class AWGSocket(): + def __init__(self, ip, port, buffersize=1024, timeout=5): + self.__ip = ip + self.__port = port + self.__buffersize = buffersize + self.__timeout = timeout + + def _cleanstring(self, bytestring): + # accept strings as well, but encode them to bytes + if not isinstance(bytestring, bytes): + bytestring = bytestring.encode() + # make sure the message is delimited by b'\n' + if not bytestring.endswith(b'\n'): + bytestring = bytestring + b'\n' + return bytestring + + def send(self, bytestring): + """Sends a command to the AWG with no answer.""" + bytestring = self._cleanstring(bytestring) + s = socket.create_connection((self.__ip, self.__port)) + for chunk in grouper(self.__buffersize, bytestring): + s.send(bytestring) + s.close() + + def query(self, bytestring): + """Queries the AWG and returns its answer.""" + bytestring = self._cleanstring(bytestring) + if not bytestring.endswith(b'?\n'): + raise ValueError("Invalid query, does not end with '?'.") + # create socket and make query + s = socket.create_connection((self.__ip, self.__port)) + s.send(bytestring) + # receive answer, terminated by '\n' + chunks = [] + bytes_recvd = 0 + while True: + ready_to_read, _, _ = select.select([s],[],[], self.__timeout) + if ready_to_read: + s = ready_to_read[0] + data = s.recv(self.__buffersize) + if not data: + break # opposite side closed the connection (probably) + if b'\n' in data: # opposite side has sent message delimiter + chunks.append(data) + break + else: + chunks.append(data) + s.close() + answer = b''.join(chunks) + return answer + class TektronixAWG(AWG): - def __init__(self, ip: str, samplerate: float): + def __init__(self, ip: str, port: int, samplerate: float, first_index=None): self.__programs = {} # holds names and programs - self.__waveform_memory = set() #map index to waveform + self.__program_indices = {} # holds programs and their first index (for jumping to it) + self.__waveform_memory = set() #map sequence indices to waveforms and vice versa self.__program_waveforms = {} # maps program names to set of waveforms used by the program self.__ip = ip + self.__port = port self.__rm = visa.ResourceManager() - self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip)) - self.__identifier = self.inst.query('*IDN?') + self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip, self.__port)) + self.__identifier = self.inst.query('*IDN?\n') + self.inst.write('AWGCONTROL:RMODE SEQUENCE') # play sequence self.__samplerate = samplerate - self.__scale = 1. - self.__offset = 0. + self.__scale = 2. + self.__offset = 1. self.__channel_template = '{0:s}_{1:d}' self.__channels = [1] # use 2 channels. TODO: fix this + if not first_index: + # query how long the sequence already is + sequence_length_before = self.inst.query('SEQUENCE:LENGTH?') + # add 400 (?, arbitrary) more slots + self.inst.query('SEQUENCE:LENGTH', sequence_length_before + 400) # magic number + self.__first_index = sequence_length_before + else: + self.__first_index = first_index + self.__current_index = self.__first_index + 1 + # TODO: load dummy pulse to first_index + + @property def outputRange(self): @@ -42,14 +131,14 @@ def programs(self): def samplerate(self): return self.__samplerate - def rescale_data(self, voltages: np.ndarray) -> np.ndarray: + def rescale(self, voltages: np.ndarray) -> np.ndarray: """Converts an array of voltages to an array of unsigned integers for upload.""" data = (voltages + self.__offset) / self.__scale + 1 # scale data to uint14 range data = (data * (2**13 - 1)) data[data > 2**14 - 1] = 2**14 - 1 data = data.astype(np.uint16) - data = marker.astype(np.uint16) * 2**14 + # data = data + marker.astype(np.uint16) * 2**14 return data def waveform2name(self, waveform): @@ -61,49 +150,43 @@ def add_waveform(self, waveform, offset): if waveform in self.__waveform_memory: pass else: - start = datetime.datetime.now() # first sample the waveform to get an array of data - ts = np.arange(waveform.duration/self.__samplerate) + ts = np.arange(waveform.duration * self.__samplerate) * 1/self.samplerate data = waveform.sample(ts, offset) wf_name = self.waveform2name(waveform) # now create a new waveform on the awg total = len(data) - # TODO: also do this for multiple_channels - for c in self.__channels: - name = channel_template.format(wf_name, c) - ipdb.set_trace() + for ic, c in enumerate(self.__channels): + name = self.__channel_template.format(wf_name, c) self.inst.write('WLIST:WAVEFORM:NEW "{0}", {1:d}, INT'.format(name, total)) chunksize = 65536 - for i, chunk in enumerate(np.split(data, chunksize)): - """This needs a lot of testing!!!""" #TODO: test - self.inst.write_binary_values('WLIST:WAVEFORM:DATA "{0:s}", {1:d}, {2:d}'.format(wf_name, i*chunksize, len(chunk), 2*len(chunk), chunk.astype('uint8'))) + data = self.rescale(data[:,ic]) + n_chunks = np.ceil(total/chunksize) + for i, chunk in enumerate(np.array_split(data, n_chunks)): + header ='#{0:d}{1:d}'.format(len(str(len(chunk))), len(chunk)) + self.inst.write_binary_values('WLIST:WAVEFORM:DATA "{0:s}", {1:d}, {2:d}, '.format(name, i*chunksize, len(chunk)), chunk, datatype='H') - end = datetime.datetime.now() - duration = end - start - print('Load time for pulse {0}: {1:g}, seconds for {2:d} points.'.format( - wf_name, duration.total_seconds(), total)) - self.__waveform_memory += set(waveform) + self.__waveform_memory.add(waveform) def build_sequence(self, name): '''Puts a new sequence in the AWG sequence memory''' - length = len(self.__programs[name]) + length = len(self.__programs[name]) - 1 # - 1 for the stop instruction in the end program = self.__programs[name] - self.inst.write('SEQUENCE:LENGTH {0:d}'.format(length)) # create new sequence for i in range(length): waveform = program[i] wf_name = self.waveform2name(waveform) # set i'th sequence element # TODO: for multiple channels write the following line for each channel, respectively for c in self.__channels: - self.inst.write('SEQUENCE:ELEMENT{0:d}:WAVEFORM{1:d} "{2:s}"'.format(i+1, c+1, wf_name)) + name = self.__channel_template.format(wf_name, c) + self.inst.write('SEQUENCE:ELEMENT{0:d}:WAVEFORM{1:d} "{2:s}"'.format(self.__current_index + i + 1, c, name)) # have sequence go back to index 1 after playback of index N self.inst.write('SEQUENCE:ELEMENT{0:d}:GOTO:STATE ON'.format(length)) def upload(self, name, program, force=False): '''Uploads all necessary waveforms for the program to the AWG and create a corresponding sequence.''' - ipdb.set_trace() if name in self.programs: if not force: raise ProgramOverwriteException(name) @@ -119,12 +202,15 @@ def upload(self, name, program, force=False): offset += block.waveform.duration used_waveforms = frozenset([block.waveform for block in exec_blocks]) self.__program_waveforms[name] = used_waveforms + self.__program_indices[name] = self.__current_index + self.__current_index = self.__current_index + len(program) + self.build_sequence(name) def run(self, name, autorun=False): # there is only one sequence per devicee, so program the sequence first - self.build_sequence(name) - self.inst.write('AWGCONTROL:RMODE:SEQUENCE') # play sequence + self.inst.write('SEQUENCE:ELEMENT1:GOTO:STATE ON') + self.inst.write('SEQUENCE:ELEMENT1:GOTO:INDEX', self.__program_indices[name]) self.inst.write('AWGCONTROL:RUN') @@ -140,9 +226,13 @@ def clean(self): delete = all_wfs - necessary_wfs for waveform in delete: wf_name = self.waveform2name(waveform) - for c in channel: + for c in self.__channels: name = self.__channel_template.format(wf_name, c) - self.inst.write('WLIST:WAVEFORM:DELETE {0:s}'.format(wf_name)) - self.__waveform_memory.remove(wf) + self.inst.write('WLIST:WAVEFORM:DELETE "{0:s}"'.format(name)) + self.__waveform_memory.remove(waveform) print('Deleted {0:d} waveforms'.format(len(delete))) + + # reset sequence + self.inst.write('SEQUENCE:LENGTH', self.__first_index) + self.inst.write('SEQUENCE:LENGTH', self.__first_index + 400) return len(delete) From 3843abd6242aa9a9ce101facd0e22762afa172ef Mon Sep 17 00:00:00 2001 From: Patrick Bethke Date: Mon, 14 Mar 2016 15:58:18 +0100 Subject: [PATCH 005/116] fix plotting and formatting in sequencing --- qctoolkit/hardware/__init__.py | 4 ++++ qctoolkit/pulses/plotting.py | 9 +-------- qctoolkit/pulses/sequencing.py | 34 +++++++++++++++++----------------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/qctoolkit/hardware/__init__.py b/qctoolkit/hardware/__init__.py index e69de29bb..5d61ff09e 100644 --- a/qctoolkit/hardware/__init__.py +++ b/qctoolkit/hardware/__init__.py @@ -0,0 +1,4 @@ +__all__ = [ + 'awgs', + 'dacs' +] diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index dbca7ce84..4f77030e7 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -51,19 +51,12 @@ def plot(pulse: SequencingElement, parameters: Dict[str, Parameter]={}, sample_r if not sequencer.has_finished(): raise PlottingNotPossibleException(pulse) times, voltages = plotter.render(sequence) - import ipdb; ipdb.set_trace() # plot! f = plt.figure() ax = f.add_subplot(111) ax.step(times, voltages, where='post') - - # add some margins in the presentation - plt.plot() - plt.xlim( -0.5, times[-1] + 0.5) - plt.ylim(min(voltages) - 0.5, max(voltages) + 0.5) - - f.show() + return f class PlottingNotPossibleException(Exception): diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index 6ee45af04..1adaa72a1 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -11,13 +11,13 @@ __all__ = ["SequencingElement", "Sequencer"] - + class SequencingElement(metaclass = ABCMeta): """An entity which can be sequenced using Sequencer.""" - + def __init__(self) -> None: super().__init__() - + @abstractmethod def build_sequence(self, sequencer: "Sequencer", @@ -26,18 +26,18 @@ def build_sequence(self, instruction_block: InstructionBlock) -> None: """Translate this SequencingElement into an instruction sequence for the given instruction_block using sequencer and the given parameter sets. - + Implementation guide: Use instruction_block methods to add instructions or create new InstructionBlocks. Use sequencer to push child elements to the translation stack. """ - + @abstractmethod def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: """Return True if this SequencingElement cannot be translated yet. - + Sequencer will check requires_stop() before calling build_sequence(). If requires_stop() returns True, Sequencer interrupts the current translation process and will not call build_sequence(). - + Implementation guide: requires_stop() should only return True, if this SequencingElement cannot be build, i.e., the return value should only depend on the parameters/conditions of this SequencingElement, not on possible child elements. @@ -58,14 +58,14 @@ def __init__(self) -> None: self.__waveforms = dict() #type: Dict[int, Waveform] self.__main_block = InstructionBlock() self.__sequencing_stacks = {self.__main_block: []} #type: Dict[InstructionBlock, List[StackElement]] - + def push(self, sequencing_element: SequencingElement, parameters: Dict[str, Union[Parameter, float]] = dict(), conditions: Dict[str, 'Condition'] = dict(), target_block: InstructionBlock = None) -> None: """Add an element to the translation stack of the target_block with the given set of parameters. - + The element will be on top of the stack, i.e., it is the first to be translated if no subsequent calls to push with the same target_block occur. """ @@ -75,22 +75,22 @@ def push(self, for (key, value) in parameters.items(): if isinstance(value, numbers.Real): parameters[key] = ConstantParameter(value) - + if target_block not in self.__sequencing_stacks: self.__sequencing_stacks[target_block] = [] - + self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions)) - + def build(self) -> InstructionBlock: """Start the translation process. Translate all elements currently on the translation stacks into a sequence and return the InstructionBlock of the main sequence. - + Processes all stacks (for each InstructionBlock) until each stack is either empty or its topmost element requires a stop. If build is called after a previous translation process where some elements required a stop (i.e., has_finished returned False), it will append new modify the previously generated and returned main InstructionBlock. Make sure not to rely on that being unchanged. """ - if not self.has_finished(): + if not self.has_finished(): shall_continue = True # shall_continue will only be False, if the first element on all stacks requires a stop or all stacks are empty while shall_continue: shall_continue = False @@ -102,12 +102,12 @@ def build(self) -> InstructionBlock: sequencing_stack.pop() element.build_sequence(self, parameters, conditions, target_block) else: break - + return self.__main_block.compile_sequence() - + def has_finished(self) -> bool: """Returns True, if all translation stacks are empty. Indicates that the translation is complete. - + Note that has_finished that has_finished will return False, if there are stack elements that require a stop. In this case, calling build will only have an effect if these elements no longer require a stop, e.g. when required measurement results have been acquired since the last translation. From 491cade582f7539a70f49904ce6caa18d1de3861 Mon Sep 17 00:00:00 2001 From: Lukas Prediger Date: Fri, 3 Jun 2016 15:56:31 +0200 Subject: [PATCH 006/116] More documentation for abstract AWG class. Fixed some stuff. Tests curretnly fail at (de)serialization of multi-channel table pulses. --- qctoolkit/hardware/awgs/awg.py | 119 ++++++++++++++++++++------- qctoolkit/hardware/awgs/tektronix.py | 12 +-- setup.py | 2 +- tests/hardware/awg_tests.py | 16 ++-- 4 files changed, 104 insertions(+), 45 deletions(-) diff --git a/qctoolkit/hardware/awgs/awg.py b/qctoolkit/hardware/awgs/awg.py index 93541769d..0b09dac78 100644 --- a/qctoolkit/hardware/awgs/awg.py +++ b/qctoolkit/hardware/awgs/awg.py @@ -1,65 +1,104 @@ +"""This module defines the common interface for arbitrary waveform generators. + +Classes: + - AWG: Common AWG interface. + - DummyAWG: A software stub implementation of the AWG interface. + - ProgramOverwriteException + - OutOfWaveformMemoryException +""" from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Set -from typing import Tuple -from typing import List +from typing import Set, Tuple, List + +from qctoolkit.pulses.instructions import InstructionSequence, EXECInstruction +__all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", + "OutOfWaveformMemoryExecption"] -from qctoolkit.pulses.instructions import InstructionBlock, EXECInstruction +Program = InstructionSequence -__all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", "OutOfWaveformMemoryExecption"] -Program = List[InstructionBlock] +class AWG(metaclass=ABCMeta): + """An arbitrary waveform generator abstraction class. -class AWG(metaclass = ABCMeta): - """An arbitrary waveform generator abstraction class. It keeps track of the AWG state and manages waveforms and programs on the hardware.""" + It keeps track of the AWG state and manages waveforms and programs on the hardware. + """ @abstractmethod - def upload(self, name: str, program: List[InstructionBlock]): - """Take a name for a program, the program and upload all the necessary waveforms to the AWG hardware. This method should be cheap for programs already on the device and can therefore be used for syncing.""" + def upload(self, name: str, program: Program, force: bool=False) -> None: + """Upload a program to the AWG. + + Physically uploads all waveforms required by the program - excluding those already present - + to the device and sets up playback sequences accordingly. + This method should be cheap for program already on the device and can therefore be used + for syncing. + + Args: + name (str): A name for the program on the AWG. + program (Program): The program (a sequence of instructions) to upload. + force (bool): If a different sequence is already present with the same name, it is + overwritten if force is set to True. (default = False) + """ @abstractmethod - def remove(self, name: str, force=False): - """Take the name of a program and remove it from the AWG, deleting all unneeded waveforms in the process.""" + def remove(self, name: str) -> None: + """Remove a program from the AWG. + Also discards all waveforms referenced only by the program identified by name. - @abstractproperty - def programs(self) -> Set[str]: - """Return the set of program names, that can currently be executed on the hardware AWG.""" - # @abstractmethod - # def clean(self) -> None: - # """Delete all waveforms from the hardware AWG that are not needed by the programs on the machine.""" + Args: + name (str): The name of the program to remove. + """ @abstractmethod - def run(self, name) -> None: + def run(self, name: str) -> None: """Load the program 'name' and either arm the device for running it or run it.""" + # todo: isn't this semantically unlcear and should be separated into two explicit methods + + @abstractproperty + def programs(self) -> Set[str]: + """The set of program names that can currently be executed on the hardware AWG.""" @abstractproperty - def samplerate(self) -> float: - """Returns the AWG samplerate (needed by the sequencer)""" + def sample_rate(self) -> float: + """The sample rate of the AWG.""" @abstractproperty - def identifier(self): + def identifier(self) -> str: """Return a hardware identifier string.""" @abstractproperty - def outputRange(self): - """Return the minimal/maximal voltage the AWG can produce""" + def output_range(self) -> Tuple[float, float]: + """The minimal/maximal voltage the AWG can produce.""" class DummyAWG(AWG): """Dummy AWG for debugging purposes.""" - def __init__(self, memory=100): + + def __init__(self, + memory: int=100, + sample_rate: float=10, + output_range: Tuple[float, float]=(-5,5)) -> None: + """Create a new DummyAWG instance. + + Args: + memory (int): Available memory slots for waveforms. (default = 100) + sample_rate (float): The sample rate of the dummy. (default = 10) + output_range (float, float): A (min,max)-tuple of possible output values. + (default = (-5,5)). + """ self.__programs = {} # contains program names and programs - self.__waveform_memory = [None for i in memory] + self.__waveform_memory = [None for i in range(memory)] self.__waveform_indices = {} # dict that maps from waveform hash to memory index self.__program_wfs = {} # contains program names and necessary waveforms indices + self.__sample_rate = sample_rate + self.__output_range = output_range - def add_waveform(self, waveform): + def add_waveform(self, waveform) -> int: try: index = self.__waveform_memory.index(None) except ValueError: - raise OutOfWaveformMemoryException + raise OutOfWaveformMemoryException() self.__waveform_memory[index] = waveform self.__waveform_indices[hash(waveform)] = index return index @@ -74,7 +113,7 @@ def upload(self, name, program, force=False): else: self.__programs[name] = program exec_blocks = filter(lambda x: type(x) == EXECInstruction, program) - indices = frozenset(add_waveform(block.waveform) for block in exec_blocks) + indices = frozenset(self.add_waveform(block.waveform) for block in exec_blocks) self.__program_wfs[name] = indices def remove(self,name): @@ -92,16 +131,36 @@ def clean(self): self.__waveform_indices.pop(wf) self.__waveform_memory = None + def run(self, name: str) -> None: + raise NotImplementedError() + + @property def programs(self): return frozenset(self.__programs.keys()) + @property + def output_range(self) -> Tuple[float, float]: + return self.__output_range + + @property + def identifier(self) -> str: + return "DummyAWG{0}".format(id(self)) + + @property + def sample_rate(self) -> float: + return self.__sample_rate + + class ProgramOverwriteException(Exception): + def __init__(self, name): super().__init__() self.name = name def __str__(self) -> str: - return "A program with the given name '{}' is already present on the device. Use force to overwrite.".format(self.name) + return "A program with the given name '{}' is already present on the device." \ + " Use force to overwrite.".format(self.name) + class OutOfWaveformMemoryException(Exception): def __str__(self): diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index a392ebedc..da3cd8672 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -86,7 +86,8 @@ def query(self, bytestring): class TektronixAWG(AWG): - def __init__(self, ip: str, port: int, samplerate: float, first_index=None): + + def __init__(self, ip: str, port: int, sample_rate: float, first_index=None): self.__programs = {} # holds names and programs self.__program_indices = {} # holds programs and their first index (for jumping to it) self.__waveform_memory = set() #map sequence indices to waveforms and vice versa @@ -97,7 +98,7 @@ def __init__(self, ip: str, port: int, samplerate: float, first_index=None): self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip, self.__port)) self.__identifier = self.inst.query('*IDN?\n') self.inst.write('AWGCONTROL:RMODE SEQUENCE') # play sequence - self.__samplerate = samplerate + self.__sample_rate = sample_rate self.__scale = 2. self.__offset = 1. self.__channel_template = '{0:s}_{1:d}' @@ -128,8 +129,8 @@ def programs(self): return list(self.__programs.keys()) @property - def samplerate(self): - return self.__samplerate + def sample_rate(self): + return self.__sample_rate def rescale(self, voltages: np.ndarray) -> np.ndarray: """Converts an array of voltages to an array of unsigned integers for upload.""" @@ -151,7 +152,7 @@ def add_waveform(self, waveform, offset): pass else: # first sample the waveform to get an array of data - ts = np.arange(waveform.duration * self.__samplerate) * 1/self.samplerate + ts = np.arange(waveform.duration * self.__sample_rate) * 1/self.sample_rate data = waveform.sample(ts, offset) wf_name = self.waveform2name(waveform) @@ -213,7 +214,6 @@ def run(self, name, autorun=False): self.inst.write('SEQUENCE:ELEMENT1:GOTO:INDEX', self.__program_indices[name]) self.inst.write('AWGCONTROL:RUN') - def remove(self, name): if name in self.programs: self.__programs.pop(name) diff --git a/setup.py b/setup.py index 02fe8cedf..abba34277 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ else: requires_typing = [] -subpackages = ['pulses','utils','qcmatlab'] +subpackages = ['pulses', 'utils', 'qcmatlab', 'hardware'] packages = ['qctoolkit'] + ['qctoolkit.' + subpackage for subpackage in subpackages] setup(name='qctoolkit', diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 96ae32a23..72129a825 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -1,35 +1,35 @@ import unittest import numpy as np -import qctoolkit.hardware.awg as awg +import qctoolkit.hardware.awgs.awg as awg import qctoolkit.pulses as pls + class DummyAWGTest(unittest.TestCase): + def setUp(self): self.pulse_template = pls.TablePulseTemplate() self.pulse_template.add_entry('value', 5) self.sequencer = pls.Sequencer() for i in range(1,12): pars = dict(value=i) - sequencer.push(self.pulse_template, pars) + self.sequencer.push(self.pulse_template, pars) self.program = self.sequencer.build() def test_OutOfMemoryException(self): dummy = awg.DummyAWG(10) - with self.assertRaises(awg.OutOfMemoryException): + with self.assertRaises(awg.OutOfWaveformMemoryException): dummy.upload('program', self.program) def test_ProgramOverwriteException(self): - dummy = awg.DummyAWG(10) + dummy = awg.DummyAWG(100) dummy.upload('program', self.program) with self.assertRaises(awg.ProgramOverwriteException): - dummy.upload('program') + dummy.upload('program', self.program) def test_upload(self): dummy = awg.DummyAWG(100) dummy.upload('program',self.program) memory_part = [None for i in range(89)] - self.assertEqual(dummy._DummyAWG_waveform_memory[11:], memory_part) + self.assertEqual(dummy._DummyAWG__waveform_memory[11:], memory_part) self.assertEqual(dummy.programs, set(['program'])) - - From e6a106a97c857c27ed9f423edf5995f3929dff19 Mon Sep 17 00:00:00 2001 From: Lukas Prediger Date: Fri, 10 Jun 2016 11:28:48 +0200 Subject: [PATCH 007/116] Added pyvisa as install requirement. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 95d022497..0e038c99e 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ package_dir = {'qctoolkit': 'qctoolkit'}, packages=packages, tests_require=['pytest'], - install_requires= ['py_expression_eval', 'numpy'] + requires_typing, + install_requires= ['py_expression_eval', 'numpy', 'pyvisa'] + requires_typing, extras_require={ 'testing' : ['pytest'], 'plotting' : ['matplotlib'], From d1bf4ebaeff2d62aaac810ad0b6953162cf91d28 Mon Sep 17 00:00:00 2001 From: Patrick Bethke Date: Thu, 3 Nov 2016 15:00:05 +0100 Subject: [PATCH 008/116] simulation mode for tektronix driver and tests for it --- qctoolkit/hardware/awgs/tektronix.py | 40 +++++++++++++++++++++++----- tests/hardware/awg_tests.py | 25 +++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index 34698d5c8..cef6ec974 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -4,8 +4,15 @@ from itertools import chain, repeat from typing import Iterable, Any, Set, Tuple -import visa import numpy as np +import warnings + +try: + import visa + hasVisa = True +except ImportError: + warnings.warn("pyVISA not available, Tektronix AWGs only available in simulation mode!") + hasVisa = False from qctoolkit.hardware.awgs.awg import AWG, ProgramOverwriteException, Program from qctoolkit.pulses.instructions import EXECInstruction, Waveform @@ -87,18 +94,39 @@ def query(self, bytestring: bytes) -> None: answer = b''.join(chunks) return answer +class TektronixAWGSimulator(): + def query(self, *args) -> str: + string = ' '.join(map(str, args)) + if string.startswith('*IDN='): + return "Tektronix Simulator" + elif string.startswith('SEQUENCE:LENGTH?'): + return 0 + else: + return "QUERY: %s" % string + + def write(self, string, *args, **kwargs) -> None: + pass + + def write_binary_values(self, string, *args, **kwargs) -> None: + pass + class TektronixAWG(AWG): - def __init__(self, ip: str, port: int, sample_rate: float, first_index=None) -> None: + def __init__(self, ip: str, port: int, sample_rate: float, first_index=None, simulation=False) -> None: self.__programs = {} # holds names and programs self.__program_indices = {} # holds programs and their first index (for jumping to it) self.__waveform_memory = set() #map sequence indices to waveforms and vice versa self.__program_waveforms = {} # maps program names to set of waveforms used by the program self.__ip = ip self.__port = port - self.__rm = visa.ResourceManager() - self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip, self.__port)) + self.__simulation = simulation + if simulation: + warnings.warn("Using Tektronix AWG driver in simulation mode!") + self.inst = TektronixAWGSimulator() + else: + self.__rm = visa.ResourceManager() + self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip, self.__port)) self.__identifier = self.inst.query('*IDN?\n') self.inst.write('AWGCONTROL:RMODE SEQUENCE') # play sequence self.__sample_rate = sample_rate @@ -108,7 +136,7 @@ def __init__(self, ip: str, port: int, sample_rate: float, first_index=None) -> self.__channels = [1] # use 2 channels. TODO: fix this if not first_index: # query how long the sequence already is - sequence_length_before = self.inst.query('SEQUENCE:LENGTH?') + sequence_length_before = int(self.inst.query('SEQUENCE:LENGTH?')) # add 400 (?, arbitrary) more slots self.inst.query('SEQUENCE:LENGTH', sequence_length_before + 400) # magic number self.__first_index = sequence_length_before @@ -120,7 +148,7 @@ def __init__(self, ip: str, port: int, sample_rate: float, first_index=None) -> @property - def outputRange(self) -> Tuple[float, float]: + def output_range(self) -> Tuple[float, float]: return (-1, 1) @property diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 72129a825..51cd9a89a 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -2,6 +2,7 @@ import numpy as np import qctoolkit.hardware.awgs.awg as awg +import qctoolkit.hardware.awgs.tektronix as tek import qctoolkit.pulses as pls @@ -33,3 +34,27 @@ def test_upload(self): memory_part = [None for i in range(89)] self.assertEqual(dummy._DummyAWG__waveform_memory[11:], memory_part) self.assertEqual(dummy.programs, set(['program'])) + + +class TektronixAWGTest(unittest.TestCase): + + def setUp(self): + self.pulse_template = pls.TablePulseTemplate() + self.pulse_template.add_entry('value', 5) + self.sequencer = pls.Sequencer() + for i in range(1,12): + pars = dict(value=i) + self.sequencer.push(self.pulse_template, pars) + self.program = self.sequencer.build() + + def test_ProgramOverwriteException(self): + dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) + dummy.upload('program', self.program) + with self.assertRaises(awg.ProgramOverwriteException): + dummy.upload('program', self.program) + + def test_upload(self): + dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) + dummy.upload('program', self.program) + programs = dummy.programs + self.assertEqual(programs, ['program']) From 230bc33490d11117a38c31999217f3d1983d5fb0 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 22 Nov 2016 15:46:50 +0100 Subject: [PATCH 009/116] Measurement window integration: They now have an identifier that can be remapped for subtemplates They end up in the EXEC instruction --- qctoolkit/pulses/branch_pulse_template.py | 10 ++- qctoolkit/pulses/conditions.py | 20 +++-- qctoolkit/pulses/function_pulse_template.py | 14 ++- qctoolkit/pulses/instructions.py | 9 +- qctoolkit/pulses/loop_pulse_template.py | 9 +- .../pulses/multi_channel_pulse_template.py | 47 ++++++---- qctoolkit/pulses/pulse_template.py | 43 ++++++---- .../pulse_template_parameter_mapping.py | 28 ++++-- qctoolkit/pulses/repetition_pulse_template.py | 13 ++- qctoolkit/pulses/sequence_pulse_template.py | 64 ++++++++------ qctoolkit/pulses/sequencing.py | 13 ++- qctoolkit/pulses/table_pulse_template.py | 85 +++++++++++++++---- tests/pulses/branch_pulse_template_tests.py | 28 ++++-- tests/pulses/conditions_tests.py | 34 ++++---- tests/pulses/loop_pulse_template_tests.py | 6 +- .../multi_channel_pulse_template_tests.py | 58 +++++++++---- .../pulse_template_parameter_mapping_tests.py | 59 ++++++++++++- tests/pulses/pulse_template_tests.py | 20 +++-- .../pulses/repetition_pulse_template_tests.py | 16 ++-- tests/pulses/sequence_pulse_template_tests.py | 66 ++++++++------ tests/pulses/sequencing_dummies.py | 21 ++++- tests/pulses/sequencing_tests.py | 64 ++++++++------ tests/pulses/table_pulse_template_tests.py | 62 +++++++++----- ...e_sequence_sequencer_intergration_tests.py | 21 +++-- tests/serialization_tests.py | 4 +- 25 files changed, 556 insertions(+), 258 deletions(-) diff --git a/qctoolkit/pulses/branch_pulse_template.py b/qctoolkit/pulses/branch_pulse_template.py index 6251e3388..dd398c1b4 100644 --- a/qctoolkit/pulses/branch_pulse_template.py +++ b/qctoolkit/pulses/branch_pulse_template.py @@ -60,10 +60,6 @@ def parameter_names(self) -> Set[str]: def parameter_declarations(self) -> Set[str]: return self.__if_branch.parameter_declarations | self.__else_branch.parameter_declarations - def get_measurement_windows(self, parameters: Dict[str, Parameter]=None) \ - -> List[MeasurementWindow]: - raise NotImplementedError() - @property def is_interruptable(self) -> bool: return self.__if_branch.is_interruptable and self.__else_branch.is_interruptable @@ -72,6 +68,10 @@ def is_interruptable(self) -> bool: def num_channels(self) -> int: return self.__if_branch.num_channels + @property + def measurement_names(self) -> Set[str]: + return self.__if_branch.measurement_names | self.__else_branch.measurement_names + def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: try: return conditions[self.__condition] @@ -82,6 +82,7 @@ def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: self.__obtain_condition_object(conditions).build_sequence_branch(self, self.__if_branch, @@ -89,6 +90,7 @@ def build_sequence(self, sequencer, parameters, conditions, + measurement_mapping, instruction_block) def requires_stop(self, diff --git a/qctoolkit/pulses/conditions.py b/qctoolkit/pulses/conditions.py index 839ff95ec..f57dc63b5 100644 --- a/qctoolkit/pulses/conditions.py +++ b/qctoolkit/pulses/conditions.py @@ -46,6 +46,7 @@ def build_sequence_loop(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: """Translate a looping SequencingElement using this Condition into an instruction sequence for the given instruction block using sequencer and the given parameter sets. @@ -73,6 +74,7 @@ def build_sequence_branch(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: """Translate a branching SequencingElement using this Condition into an instruction sequence for the given instruction block using sequencer and the given parameter sets. @@ -122,13 +124,14 @@ def build_sequence_loop(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: body_block = InstructionBlock() body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block.instructions)) instruction_block.add_instruction_cjmp(self.__trigger, body_block) - sequencer.push(body, parameters, conditions, body_block) + sequencer.push(body, parameters, conditions, measurement_mapping, body_block) def build_sequence_branch(self, delegator: SequencingElement, @@ -137,15 +140,16 @@ def build_sequence_branch(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: if_block = InstructionBlock() else_block = InstructionBlock() instruction_block.add_instruction_cjmp(self.__trigger, if_block) - sequencer.push(if_branch, parameters, conditions, if_block) + sequencer.push(if_branch, parameters, conditions, measurement_mapping, if_block) instruction_block.add_instruction_goto(else_block) - sequencer.push(else_branch, parameters, conditions, else_block) + sequencer.push(else_branch, parameters, conditions, measurement_mapping, else_block) if_block.return_ip = InstructionPointer(instruction_block, len(instruction_block.instructions)) @@ -192,14 +196,15 @@ def build_sequence_loop(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: evaluation_result = self.__callback(self.__loop_iteration) if evaluation_result is None: raise ConditionEvaluationException() if evaluation_result is True: - sequencer.push(delegator, parameters, conditions, instruction_block) - sequencer.push(body, parameters, conditions, instruction_block) + sequencer.push(delegator, parameters, conditions, measurement_mapping, instruction_block) + sequencer.push(body, parameters, conditions, measurement_mapping, instruction_block) self.__loop_iteration += 1 # next time, evaluate for next iteration def build_sequence_branch(self, @@ -209,15 +214,16 @@ def build_sequence_branch(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: evaluation_result = self.__callback(self.__loop_iteration) if evaluation_result is None: raise ConditionEvaluationException() if evaluation_result is True: - sequencer.push(if_branch, parameters, conditions, instruction_block) + sequencer.push(if_branch, parameters, conditions, measurement_mapping, instruction_block) else: - sequencer.push(else_branch, parameters, conditions, instruction_block) + sequencer.push(else_branch, parameters, conditions, measurement_mapping, instruction_block) class ConditionEvaluationException(Exception): diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 13fea8ef9..9bcf0a234 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -88,14 +88,12 @@ def get_pulse_length(self, parameters: Dict[str, Parameter]) -> float: for (parameter_name, parameter) in parameters.items()} ) - def get_measurement_windows(self, - parameters: Optional[Dict[str, Parameter]]=None) \ - -> List[MeasurementWindow]: - raise NotImplementedError() - if not self.__is_measurement_pulse: - return - else: - return [(0, self.get_pulse_length(parameters))] + def get_measurement_windows(self, parameters: Dict[str, Parameter]) -> List[MeasurementWindow]: + return [] + + @property + def measurement_names(self) -> Set[str]: + return set() @property def is_interruptable(self) -> bool: diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index bbe63a8ef..24d7086b7 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -17,7 +17,7 @@ """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import List, Any, Dict, Iterable, Optional +from typing import List, Any, Dict, Iterable, Optional, Tuple import numpy from qctoolkit.comparable import Comparable @@ -214,7 +214,7 @@ def __str__(self) -> str: class EXECInstruction(Instruction): """An instruction to execute/play back a waveform.""" - def __init__(self, waveform: Waveform) -> None: + def __init__(self, waveform: Waveform, measurement_windows: List[Tuple[str,List['MeasurementWindow']]] = []) -> None: """Create a new EXECInstruction object. Args: @@ -222,6 +222,7 @@ def __init__(self, waveform: Waveform) -> None: """ super().__init__() self.waveform = waveform + self.measurement_windows = measurement_windows @property def compare_key(self) -> Any: @@ -354,14 +355,14 @@ def add_instruction(self, instruction: Instruction) -> None: """ self.__instruction_list.append(instruction) - def add_instruction_exec(self, waveform: Waveform) -> None: + def add_instruction_exec(self, waveform: Waveform, measurement_windows: List[Tuple[str,List['MeasurementWindows']]] = None) -> None: """Create and append a new EXECInstruction object for the given waveform at the end of this instruction block. Args: waveform (Waveform): The Waveform object referenced by the new EXECInstruction. """ - self.add_instruction(EXECInstruction(waveform)) + self.add_instruction(EXECInstruction(waveform,measurement_windows)) def add_instruction_goto(self, target_block: 'InstructionBlock') -> None: """Create and append a new GOTOInstruction object with a given target block at the end of diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index 28f86213b..8186127e5 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -53,9 +53,6 @@ def condition(self) -> str: def parameter_names(self) -> Set[str]: return self.__body.parameter_names - def get_measurement_windows(self, parameters: Dict[str, Parameter]=None) -> MeasurementWindow: - raise NotImplementedError() - @property def parameter_declarations(self) -> Set[str]: return self.__body.parameter_declarations @@ -68,6 +65,10 @@ def is_interruptable(self) -> bool: def num_channels(self) -> int: return self.__body.num_channels + @property + def measurement_names(self) -> Set[str]: + return self.__body.measurement_names + def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: try: return conditions[self.__condition] @@ -78,12 +79,14 @@ def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: self.__obtain_condition_object(conditions).build_sequence_loop(self, self.__body, sequencer, parameters, conditions, + measurement_mapping, instruction_block) def requires_stop(self, diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index a59e0bd9c..9b33c5033 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -7,16 +7,16 @@ - MultiChannelWaveform: A waveform defined for several channels by combining waveforms """ -from typing import Dict, List, Tuple, Set, Optional, Any, Iterable +from typing import Dict, List, Tuple, Set, Optional, Any, Iterable, Union import numpy from qctoolkit.serialization import Serializer from qctoolkit.pulses.instructions import Waveform -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, SubTemplate from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, \ - MissingMappingException + MissingMappingException, get_measurement_name_mappings from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ ParameterNotProvidedException from qctoolkit.pulses.conditions import Condition @@ -144,17 +144,17 @@ class MultiChannelPulseTemplate(AtomicPulseTemplate): - MultiChannelWaveform """ - Subtemplate = Tuple[AtomicPulseTemplate, Dict[str, str], List[int]] + SimpleSubTemplate = Tuple[AtomicPulseTemplate, Dict[str, str], Dict[str,str], List[int]] def __init__(self, - subtemplates: Iterable[Subtemplate], + subtemplates: Iterable[Union[SubTemplate,SimpleSubTemplate]], external_parameters: Set[str], identifier: str=None) -> None: """Creates a new MultiChannelPulseTemplate instance. Requires a list of subtemplates in the form (PulseTemplate, Dict(str -> str), List(int)) where the dictionary is a mapping between the - external parameters exposed by this SequencePulseTemplate to the parameters declared by the + external parameters exposed by this PulseTemplate to the parameters declared by the subtemplates, specifying how the latter are derived from the former, i.e., the mapping is subtemplate_parameter_name -> mapping_expression (as str) where the free variables in the mapping_expression are parameters declared by this MultiChannelPulseTemplate. @@ -179,13 +179,16 @@ def __init__(self, that was not declared in the external parameters of this MultiChannelPulseTemplate. """ super().__init__(identifier=identifier) + + subtemplates = [st if isinstance(st,SubTemplate) else SubTemplate(st[0],st[1],channel_mapping=st[2]) for st in subtemplates ] + self.__parameter_mapping = PulseTemplateParameterMapping(external_parameters) - self.__subtemplates = [(template, channel_mapping) for (template, _, channel_mapping) - in subtemplates] + self.__subtemplates = [(st.template, st.channel_mapping) for st in subtemplates] + self.__measurement_window_mappings = get_measurement_name_mappings(subtemplates) assigned_channels = set() num_channels = self.num_channels - for template, mapping_functions, channel_mapping in subtemplates: + for template, mapping_functions, _, channel_mapping in subtemplates: # Consistency checks for parameter, mapping_function in mapping_functions.items(): self.__parameter_mapping.add(template, parameter, mapping_function) @@ -222,9 +225,10 @@ def parameter_declarations(self) -> Set[ParameterDeclaration]: # TODO: min, max, default values not mapped (required?) return {ParameterDeclaration(parameter_name) for parameter_name in self.parameter_names} - def get_measurement_windows(self, parameters: Dict[str, Parameter] = None) \ - -> List['MeasurementWindow']: - raise NotImplementedError() + def get_measurement_windows(self, parameters: Dict[str, Parameter]) -> List['MeasurementWindow']: + mapped_window = lambda window : (self.__measurement_window_mappings[window[0]],window[1],window[2]) if \ + window[0] in self.__measurement_window_mappings else window + return [ mapped_window(window) for st, _ in self.__subtemplates for window in st.get_measurement_windows(parameters) ] @property def is_interruptable(self) -> bool: @@ -234,6 +238,12 @@ def is_interruptable(self) -> bool: def num_channels(self) -> int: return sum(t.num_channels for t, _ in self.__subtemplates) + @property + def measurement_names(self) -> Set[str]: + return {measurement_mapping[name] if name in measurement_mapping else name + for template, measurement_mapping in zip(self.__subtemplates, self.__measurement_window_mappings) + for name in template[0].measurement_names} + def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition]) -> bool: @@ -256,13 +266,14 @@ def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[Waveform] def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: subtemplates = [] - for subtemplate, channel_mapping in self.__subtemplates: + for subtemplate, channel_mapping, measurement_mapping in zip(*zip(*self.__subtemplates) ,self.__measurement_window_mappings): mapping_functions = self.__parameter_mapping.get_template_map(subtemplate) mapping_function_strings = \ {k: serializer.dictify(m) for k, m in mapping_functions.items()} subtemplate = serializer.dictify(subtemplate) subtemplates.append(dict(template=subtemplate, parameter_mappings=mapping_function_strings, + measurement_mapping=measurement_mapping, channel_mappings=channel_mapping)) return dict(subtemplates=subtemplates, external_parameters=sorted(list(self.parameter_names))) @@ -273,10 +284,12 @@ def deserialize(serializer: Serializer, external_parameters: Iterable[str], identifier: Optional[str]=None) -> 'MultiChannelPulseTemplate': subtemplates = \ - [(serializer.deserialize(subt['template']), - {k: str(serializer.deserialize(m)) for k, m in subt['parameter_mappings'].items()}, - subt['channel_mappings']) - for subt in subtemplates] + [SubTemplate( + serializer.deserialize(subt['template']), + {k: str(serializer.deserialize(m)) for k, m in subt['parameter_mappings'].items()}, + measurement_mapping=subt['measurement_mapping'], + channel_mapping=subt['channel_mappings']) + for subt in subtemplates] template = MultiChannelPulseTemplate(subtemplates, external_parameters, diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index 8446acc98..c9775c9e2 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -7,7 +7,7 @@ directly translated into a waveform. """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict, List, Tuple, Set, Optional +from typing import Dict, List, Tuple, Set, Optional, NamedTuple from qctoolkit.serialization import Serializable @@ -16,8 +16,7 @@ __all__ = ["MeasurementWindow", "PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException"] - -MeasurementWindow = Tuple[float, float] +MeasurementWindow = Tuple[str, float, float] class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): @@ -44,15 +43,9 @@ def parameter_declarations(self) -> Set[ParameterDeclaration]: this PulseTemplate. """ - @abstractmethod - def get_measurement_windows(self, parameters: Dict[str, Parameter]=None) \ - -> List[MeasurementWindow]: - """ - FLAWED / OBSOLETE: should be fixed already in a different branch and will be merged soon - - Returns: - All measurement windows defined in this PulseTemplate. - """ + @abstractproperty + def measurement_names(self) -> Set[str]: + """The set of measurement identifiers in this pulse template""" @abstractproperty def is_interruptable(self) -> bool: @@ -74,11 +67,16 @@ def __matmul__(self, other) -> 'SequencePulseTemplate': # if there are parameter name conflicts, throw an exception raise DoubleParameterNameException(self, other, double_parameters) else: - subtemplates = [(self, {p:p for p in self.parameter_names}), - (other, {p:p for p in other.parameter_names})] + subtemplates = [(self, {p:p for p in self.parameter_names}, {}), + (other, {p:p for p in other.parameter_names}, {})] external_parameters = self.parameter_names | other.parameter_names # union return SequencePulseTemplate(subtemplates, external_parameters) +SubTemplate = NamedTuple('SubTemplate',[('template',PulseTemplate), + ('parameter_mapping',Dict[str,str]), + ('measurement_mapping',Optional[Dict[str,str]]), + ('channel_mapping',Optional[List[int]])] ) +SubTemplate.__new__.__defaults__ = (dict(),None) class AtomicPulseTemplate(PulseTemplate): """A PulseTemplate that does not imply any control flow disruptions and can be directly @@ -104,14 +102,29 @@ def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['Waveform does not represent a valid waveform. """ + @abstractmethod + def get_measurement_windows(self, parameters: Dict[str, Parameter]=None) -> List[MeasurementWindow]: + """ + :param parameters: + :return: + """ + def build_sequence(self, sequencer: 'Sequencer', parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: waveform = self.build_waveform(parameters) if waveform: - instruction_block.add_instruction_exec(waveform) + meas_windows = self.get_measurement_windows(parameters) + + meas_windows = [(measurement_mapping[name],begin,end) for name, begin, end in meas_windows] + + instruction_block.add_instruction_exec(waveform,meas_windows) + + + class DoubleParameterNameException(Exception): diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index e0b10c6c7..0c066ac28 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -1,17 +1,18 @@ """This module defines PulseTemplateParameterMapping, a helper class for pulse templates that offer mapping of parameters of subtemplates.""" -from typing import Optional, Set, Dict, Union +from typing import Optional, Set, Dict, Union, Iterable, List from qctoolkit.expressions import Expression -from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.pulse_template import PulseTemplate, SubTemplate from qctoolkit.pulses.parameters import Parameter, MappedParameter, ParameterNotProvidedException __all__ = [ "MissingMappingException", "MissingParameterDeclarationException", "UnnecessaryMappingException", - "PulseTemplateParameterMapping" + "PulseTemplateParameterMapping", + "get_measurement_name_mappings" ] @@ -156,6 +157,19 @@ def map_parameters(self, return inner_parameters +def get_measurement_name_mappings(subtemplates: Iterable[SubTemplate]) -> List[Dict[str,str]]: + mappings = [template.measurement_mapping.copy() for template in subtemplates] + for subtemplate, measurement_mapping in zip(subtemplates,mappings): + internal_names = subtemplate.template.measurement_names + mapped_names = set(measurement_mapping.keys()) + if mapped_names - internal_names: + raise UnnecessaryMappingException(subtemplate.template,mapped_names - internal_names) + # add the missing identity mappings + for unmapped_name in internal_names - mapped_names: + measurement_mapping[unmapped_name] = unmapped_name + return mappings + + class MissingParameterDeclarationException(Exception): """Indicates that a parameter declaration mapping in a SequencePulseTemplate maps to an external parameter declaration that was not declared.""" @@ -177,13 +191,13 @@ class MissingMappingException(Exception): """Indicates that no mapping was specified for some parameter declaration of a SequencePulseTemplate's subtemplate.""" - def __init__(self, template: PulseTemplate, key: str) -> None: + def __init__(self, template: PulseTemplate, key: Union[str,Set[str]]) -> None: super().__init__() self.key = key self.template = template def __str__(self) -> str: - return "The template {} needs a mapping function for parameter {}".\ + return "The template {} needs a mapping function for parameter(s) {}".\ format(self.template, self.key) @@ -191,11 +205,11 @@ class UnnecessaryMappingException(Exception): """Indicates that a mapping was provided that does not correspond to any of a SequencePulseTemplate's subtemplate's parameter declarations and is thus obsolete.""" - def __init__(self, template: PulseTemplate, key: str) -> None: + def __init__(self, template: PulseTemplate, key: Union[str,Set[str]]) -> None: super().__init__() self.template = template self.key = key def __str__(self) -> str: - return "Mapping function for parameter '{}', which template {} does not need"\ + return "Mapping function for parameter(s) '{}', which template {} does not need"\ .format(self.key, self.template) diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index 6f8b79898..7f1c3f744 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -60,12 +60,6 @@ def parameter_names(self) -> Set[str]: def parameter_declarations(self) -> Set[str]: return self.__body.parameter_declarations - def get_measurement_windows(self, - parameters: Dict[str, Parameter]=None - ) -> List[MeasurementWindow]: - """Return all measurement windows defined in this PulseTemplate.""" - raise NotImplementedError() - @property def is_interruptable(self) -> bool: return self.__body.is_interruptable @@ -74,10 +68,15 @@ def is_interruptable(self) -> bool: def num_channels(self) -> int: return self.__body.num_channels + @property + def measurement_names(self) -> Set[str]: + return self.__body.measurement_names + def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: repetition_count = self.__repetition_count if isinstance(repetition_count, ParameterDeclaration): @@ -89,7 +88,7 @@ def build_sequence(self, body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) instruction_block.add_instruction_repj(int(repetition_count), body_block) - sequencer.push(self.body, parameters, conditions, body_block) + sequencer.push(self.body, parameters, conditions, measurement_mapping, body_block) def requires_stop(self, parameters: Dict[str, Parameter], diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index 29a0b794d..cf523402b 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -7,13 +7,13 @@ from qctoolkit.serialization import Serializer from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow, \ - DoubleParameterNameException + DoubleParameterNameException, SubTemplate from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ ParameterNotProvidedException from qctoolkit.pulses.sequencing import InstructionBlock, Sequencer from qctoolkit.pulses.conditions import Condition from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, \ - MissingMappingException + MissingMappingException, get_measurement_name_mappings __all__ = ["SequencePulseTemplate"] @@ -32,10 +32,10 @@ class SequencePulseTemplate(PulseTemplate): """ # a subtemplate consists of a pulse template and mapping functions for its "internal" parameters - Subtemplate = Tuple[PulseTemplate, Dict[str, str]] # pylint: disable=invalid-name + SimpleSubTemplate = Tuple[PulseTemplate, Dict[str, str]] # pylint: disable=invalid-name def __init__(self, - subtemplates: List[Subtemplate], + subtemplates: Iterable[Union[SimpleSubTemplate,SubTemplate]], external_parameters: List[str], # pylint: disable=invalid-sequence-index identifier: Optional[str]=None) -> None: """Create a new SequencePulseTemplate instance. @@ -70,22 +70,28 @@ def __init__(self, if subtemplates: num_channels = subtemplates[0][0].num_channels + subtemplates = [ st if isinstance(st,SubTemplate) else SubTemplate(*st) for st in subtemplates ] + self.__parameter_mapping = PulseTemplateParameterMapping(external_parameters) - for template, mapping_functions in subtemplates: + for template, parameter_mapping, _, channel_mapping in subtemplates: + if channel_mapping: + raise ValueError('Channel mapping not allowed (yet) in SequencePulseTemplate') + # Consistency checks if template.num_channels != num_channels: raise ValueError("Subtemplates have different number of channels!") - for parameter, mapping_function in mapping_functions.items(): - self.__parameter_mapping.add(template, parameter, mapping_function) + for parameter, parameter_mapping_function in parameter_mapping.items(): + self.__parameter_mapping.add(template, parameter, parameter_mapping_function) remaining = self.__parameter_mapping.get_remaining_mappings(template) if remaining: raise MissingMappingException(template, remaining.pop()) - self.__subtemplates = [template for (template, _) in subtemplates] + self.__measurement_window_mappings = get_measurement_name_mappings(subtemplates) + self.__subtemplates = [st.template for st in subtemplates] self.__is_interruptable = True @property @@ -98,14 +104,11 @@ def parameter_declarations(self) -> Set[ParameterDeclaration]: return {ParameterDeclaration(name) for name in self.parameter_names} @property - def subtemplates(self) -> List[Subtemplate]: - return [(template, self.__parameter_mapping.get_template_map(template)) - for template in self.__subtemplates] - - def get_measurement_windows(self, - parameters: Dict[str, Parameter]=None - ) -> List[MeasurementWindow]: - raise NotImplementedError() # will be computed by Sequencer #TODO: IMPORTANT + def subtemplates(self) -> List[SubTemplate]: + return [SubTemplate(template, + self.__parameter_mapping.get_template_map(template), + measurement_mapping=name_mapping) + for template, name_mapping in zip(self.__subtemplates, self.__measurement_window_mappings)] @property def is_interruptable(self) -> bool: @@ -119,6 +122,11 @@ def is_interruptable(self, new_value: bool) -> None: def num_channels(self) -> int: return self.__subtemplates[0].num_channels + @property + def measurement_names(self) -> Set[str]: + return set.union(*(set(measurement_mapping.values()) + for measurement_mapping in self.__measurement_window_mappings)) + def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: @@ -128,6 +136,7 @@ def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping : Dict[str, str], instruction_block: InstructionBlock) -> None: # todo: currently ignores is_interruptable @@ -136,10 +145,15 @@ def build_sequence(self, if missing: raise ParameterNotProvidedException(missing.pop()) + def concatenate_dicts(d1, d2): + return dict(((key, d1[value]) for key, value in d2.items())) + # push subtemplates to sequencing stack with mapped parameters - for template in reversed(self.__subtemplates): + for template, local_window_mapping in zip(reversed(self.__subtemplates),reversed(self.__measurement_window_mappings)): inner_parameters = self.__parameter_mapping.map_parameters(template, parameters) - sequencer.push(template, inner_parameters, conditions, instruction_block) + inner_names = concatenate_dicts(measurement_mapping, local_window_mapping) + sequencer.push(template, inner_parameters, conditions, + inner_names, instruction_block) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict() @@ -147,12 +161,13 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data['is_interruptable'] = self.is_interruptable subtemplates = [] - for subtemplate in self.__subtemplates: + for subtemplate,window_name_mappings in zip(self.__subtemplates,self.__measurement_window_mappings): mapping_functions = self.__parameter_mapping.get_template_map(subtemplate) mapping_functions_strings = \ {k: serializer.dictify(m) for k, m in mapping_functions.items()} subtemplate = serializer.dictify(subtemplate) - subtemplates.append(dict(template=subtemplate, mappings=mapping_functions_strings)) + subtemplates.append(dict(template=subtemplate, parameter_mappings=mapping_functions_strings, + measurement_mappings=window_name_mappings)) data['subtemplates'] = subtemplates data['type'] = serializer.get_type_identifier(self) @@ -165,10 +180,11 @@ def deserialize(serializer: Serializer, external_parameters: Iterable[str], identifier: Optional[str]=None) -> 'SequencePulseTemplate': subtemplates = \ - [(serializer.deserialize(d['template']), - {k: str(serializer.deserialize(m)) - for k, m in d['mappings'].items()}) - for d in subtemplates] + [SubTemplate( + serializer.deserialize(d['template']), + {k: str(serializer.deserialize(m)) for k, m in d['parameter_mappings'].items()}, + measurement_mapping=d['measurement_mappings'] + ) for d in subtemplates] template = SequencePulseTemplate(subtemplates, external_parameters, identifier=identifier) template.is_interruptable = is_interruptable diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index e3ff82669..23aed8ee7 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -33,6 +33,7 @@ def build_sequence(self, sequencer: "Sequencer", parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: """Translate this SequencingElement into an instruction sequence for the given instruction_block using sequencer and the given parameter and condition sets. @@ -45,6 +46,7 @@ def build_sequence(self, Dict[str -> Parameter] parameters: A mapping of parameter names to Parameter objects. Dict[str -> Condition] conditions: A mapping of condition identifiers to Condition objects. + Dict[str -> str] measurement_mapping: A mapping of measurement window names InstructionBlock instruction_block: The instruction block into which instructions resulting from the translation of this SequencingElement will be placed. """ @@ -94,7 +96,7 @@ class Sequencer: SequencingElement """ - StackElement = Tuple[SequencingElement, Dict[str, Parameter], Dict[str, 'Condition']] + StackElement = Tuple[SequencingElement, Dict[str, Parameter], Dict[str, 'Condition'], Dict[str,str]] def __init__(self) -> None: """Create a Sequencer.""" @@ -108,6 +110,7 @@ def push(self, sequencing_element: SequencingElement, parameters: Optional[Dict[str, Union[Parameter, float]]]=None, conditions: Optional[Dict[str, 'Condition']]=None, + window_mapping: Optional[Dict[str,str]] = None, target_block: Optional[InstructionBlock]=None) -> None: """Add an element to the translation stack of the target_block with the given set of parameters. @@ -134,6 +137,8 @@ def push(self, conditions = dict() if target_block is None: target_block = self.__main_block + if window_mapping is None: + window_mapping = dict() for (key, value) in parameters.items(): if isinstance(value, numbers.Real): @@ -142,7 +147,7 @@ def push(self, if target_block not in self.__sequencing_stacks: self.__sequencing_stacks[target_block] = [] - self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions)) + self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping)) def build(self) -> InstructionBlock: """Start the translation process. Translate all elements currently on the translation stacks @@ -164,11 +169,11 @@ def build(self) -> InstructionBlock: shall_continue = False for target_block, sequencing_stack in self.__sequencing_stacks.copy().items(): while sequencing_stack: - (element, parameters, conditions) = sequencing_stack[-1] + (element, parameters, conditions, window_mapping) = sequencing_stack[-1] if not element.requires_stop(parameters, conditions): shall_continue |= True sequencing_stack.pop() - element.build_sequence(self, parameters, conditions, target_block) + element.build_sequence(self, parameters, conditions, window_mapping, target_block) else: break return ImmutableInstructionBlock(self.__main_block, dict()) diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 42266427e..342913f1b 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -21,6 +21,7 @@ HoldInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.conditions import Condition +from qctoolkit.expressions import Expression __all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] @@ -82,6 +83,7 @@ def sample(self, sample_times: np.ndarray, first_offset: float=0) -> np.ndarray: "TableEntry", [('t', TableValue), ('v', TableValue), ('interp', InterpolationStrategy)] ) +MeasurementDeclaration = Tuple[Union[float,Expression], Union[float,Expression]] class TablePulseTemplate(AtomicPulseTemplate): @@ -98,7 +100,7 @@ class TablePulseTemplate(AtomicPulseTemplate): Each TablePulseTemplate contains at least an entry at time 0. """ - def __init__(self, channels: int=1, measurement=False, identifier: Optional[str]=None) -> None: + def __init__(self, channels: int=1, identifier: Optional[str]=None) -> None: """Create a new TablePulseTemplate. Args: @@ -118,11 +120,11 @@ def __init__(self, channels: int=1, measurement=False, identifier: Optional[str] self.__entries.append([TableEntry(0, 0, self.__interpolation_strategies['hold'])]) self.__time_parameter_declarations = {} # type: Dict[str, ParameterDeclaration] self.__voltage_parameter_declarations = {} # type: Dict[str, ParameterDeclaration] - self.__is_measurement_pulse = measurement# type: bool + self.__measurement_windows = {} # type: Dict[str,List[MeasurementDeclaration]] self.__channels = channels # type: int @staticmethod - def from_array(times: np.ndarray, voltages: np.ndarray, measurement=False) \ + def from_array(times: np.ndarray, voltages: np.ndarray) \ -> 'TablePulseTemplate': """Static constructor to build a TablePulse from numpy arrays. @@ -135,12 +137,12 @@ def from_array(times: np.ndarray, voltages: np.ndarray, measurement=False) \ parameters. """ if voltages.ndim == 1: - res = TablePulseTemplate(channels=1, measurement=measurement) + res = TablePulseTemplate(channels=1) for time, voltage in zip(times, voltages): res.add_entry(time, voltage, interpolation='hold') elif voltages.ndim == 2: channels = voltages.shape[1] - res = TablePulseTemplate(channels=channels, measurement=measurement) + res = TablePulseTemplate(channels=channels) for channel in range(channels): for time, voltage in zip(times, voltages[:, channel]): res.add_entry(time, voltage, interpolation='hold', channel=channel) @@ -374,16 +376,62 @@ def parameter_declarations(self) -> Set[ParameterDeclaration]: return set(self.__time_parameter_declarations.values()) | \ set(self.__voltage_parameter_declarations.values()) - def get_measurement_windows(self, - parameters: Optional[Dict[str, Parameter]]=None) \ - -> List[MeasurementWindow]: # TODO: remove - if not self.__is_measurement_pulse: - return [] + def get_measurement_windows(self, parameters: Dict[str, Parameter]) -> List[MeasurementWindow]: + def get_val(v): + return v if not isinstance(v, Expression) else v.evaluate( + **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] + for name_ in v.variables()}) + + t_max = [entry[-1][0] for entry in self.__entries] + t_max = max([t if isinstance(t,numbers.Number) else t.get_value(parameters) for t in t_max]) + + resulting_windows = [] + for name, windows in self.__measurement_windows.items(): + for begin, end in windows: + resulting_windows.append( (name, get_val(begin), get_val(end)) ) + if resulting_windows[-1][2] > t_max: + raise ValueError('Measurement window out of pulse') + return resulting_windows + + + @property + def measurement_declarations(self): + """ + :return: Measurement declarations as added by the add_measurement_declaration method + """ + as_builtin = lambda x: str(x) if isinstance(x, Expression) else x + return { name: [(as_builtin(begin), as_builtin(end)) for begin, end in windows] for name, windows in self.__measurement_windows.items() } + + + @property + def measurement_names(self) -> Set[str]: + """ + :return: + """ + return set(self.__measurement_windows.keys()) + + def add_measurement_declaration(self, name: str, begin: Union[float,str], end: Union[float,str]) -> None: + if isinstance(begin,str): + begin = Expression(begin) + for v in begin.variables(): + if not v in self.__time_parameter_declarations: + if v in self.__voltage_parameter_declarations: + raise ValueError("Argument begin=<{}> must not refer to a voltage parameter declaration." + .format(str(begin))) + self.__time_parameter_declarations[v] = ParameterDeclaration(v) + if isinstance(end,str): + end = Expression(end) + for v in end.variables(): + if not v in self.__time_parameter_declarations: + if v in self.__voltage_parameter_declarations: + raise ValueError("Argument begin=<{}> must not refer to a voltage parameter declaration." + .format(str(end))) + self.__time_parameter_declarations[v] = ParameterDeclaration(v) + if name in self.__measurement_windows: + self.__measurement_windows[name].append((begin,end)) else: - # instantiated_entries = self.get_entries_instantiated(parameters) - last_entries = [channel[-1].t for channel in self.get_entries_instantiated(parameters)] - maxtime = max(last_entries) - return [(0, maxtime)] + self.__measurement_windows[name] = [(begin,end)] + @property def is_interruptable(self) -> bool: @@ -487,7 +535,6 @@ def requires_stop(self, def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict() - data['is_measurement_pulse'] = self.__is_measurement_pulse data['time_parameter_declarations'] = \ [serializer.dictify(self.__time_parameter_declarations[key]) for key in sorted(self.__time_parameter_declarations.keys())] @@ -505,6 +552,7 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: entries.append((time, voltage, str(interpolation))) serialized_entries.append(entries) data['entries'] = serialized_entries + data['measurement_declarations'] = self.measurement_declarations data['type'] = serializer.get_type_identifier(self) return data @@ -513,7 +561,7 @@ def deserialize(serializer: Serializer, time_parameter_declarations: Iterable[Any], voltage_parameter_declarations: Iterable[Any], entries: Iterable[Any], - is_measurement_pulse: bool, + measurement_declarations: Dict[str,Iterable[Any]], identifier: Optional[str]=None) -> 'TablePulseTemplate': time_parameter_declarations = \ {declaration['name']: serializer.deserialize(declaration) @@ -523,7 +571,6 @@ def deserialize(serializer: Serializer, for declaration in voltage_parameter_declarations} template = TablePulseTemplate(channels=len(entries), - measurement=is_measurement_pulse, identifier=identifier) for channel, channel_entries in enumerate(entries): @@ -534,4 +581,8 @@ def deserialize(serializer: Serializer, voltage = voltage_parameter_declarations[voltage] template.add_entry(time, voltage, interpolation=interpolation, channel=channel) + for name, windows in measurement_declarations.items(): + for window in windows: + template.add_measurement_declaration(name,*window) + return template diff --git a/tests/pulses/branch_pulse_template_tests.py b/tests/pulses/branch_pulse_template_tests.py index 96c4cf115..9884ab689 100644 --- a/tests/pulses/branch_pulse_template_tests.py +++ b/tests/pulses/branch_pulse_template_tests.py @@ -35,6 +35,12 @@ def test_num_channels(self) -> None: template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) self.assertEqual(3, template.num_channels) + def test_measurement_names(self) -> None: + if_dummy = DummyPulseTemplate(measurement_names={'if_meas'}) + else_dummy = DummyPulseTemplate(measurement_names={'else_meas'}) + template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) + self.assertEqual({'if_meas','else_meas'}, template.measurement_names) + def test_is_interruptable(self) -> None: if_dummy = DummyPulseTemplate(num_channels=3, is_interruptable=True) else_dummy = DummyPulseTemplate(num_channels=3) @@ -57,8 +63,10 @@ class BranchPulseTemplateSequencingTests(unittest.TestCase): def setUp(self) -> None: self.maxDiff = None - self.if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}) - self.else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}) + self.if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}, + measurement_names={'if_meas'}) + self.else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}, + measurement_names={'else_meas'}) self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) self.sequencer = DummySequencer() self.block = DummyInstructionBlock() @@ -111,7 +119,9 @@ def test_build_sequence(self) -> None: parameters = dict(foo=DummyParameter(326.272), bar=DummyParameter(-2624.23), hugo=DummyParameter(3.532)) - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + window_mapping = dict(else_meas='my_meas',if_meas='thy_meas') + + self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, self.block) self.assertFalse(foo_condition.loop_call_data) self.assertEqual( dict( @@ -121,6 +131,7 @@ def test_build_sequence(self) -> None: sequencer=self.sequencer, parameters=parameters, conditions=conditions, + measurement_mapping=window_mapping, instruction_block=self.block ), foo_condition.branch_call_data @@ -133,15 +144,17 @@ def test_build_sequence_condition_missing(self) -> None: parameters = dict(foo=DummyParameter(326.272), bar=DummyParameter(-2624.23), hugo=DummyParameter(3.532)) + window_mapping = {} with self.assertRaises(ConditionMissingException): - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, self.block) def test_build_sequence_parameter_missing(self) -> None: foo_condition = DummyCondition() conditions = dict(foo_condition=foo_condition) parameters = dict(foo=DummyParameter(326.272), bar=DummyParameter(-2624.23)) - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + window_mapping = dict(else_meas='my_meas',if_meas='thy_meas') + self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, self.block) self.assertFalse(foo_condition.loop_call_data) self.assertEqual( dict( @@ -151,6 +164,7 @@ def test_build_sequence_parameter_missing(self) -> None: sequencer=self.sequencer, parameters=parameters, conditions=conditions, + measurement_mapping=window_mapping, instruction_block=self.block ), foo_condition.branch_call_data @@ -163,8 +177,8 @@ class BranchPulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: self.maxDiff = None - self.if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}) - self.else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}) + self.if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}, measurement_names={'if_mease'}) + self.else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}, measurement_names={'else_meas'}) self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) def test_get_serialization_data(self) -> None: diff --git a/tests/pulses/conditions_tests.py b/tests/pulses/conditions_tests.py index 4dc75b7e3..24b2eb6dd 100644 --- a/tests/pulses/conditions_tests.py +++ b/tests/pulses/conditions_tests.py @@ -19,14 +19,14 @@ def test_build_sequence_loop(self) -> None: trigger = Trigger() condition = HardwareCondition(trigger) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) self.assertEqual(1, len(block.embedded_blocks)) body_block = block.embedded_blocks[0] self.assertEqual([DummyInstruction(), CJMPInstruction(trigger, InstructionPointer(body_block))], block.instructions, "The expected conditional jump was not generated by HardwareConditon.") self.assertEqual(InstructionPointer(block, 1), body_block.return_ip, "The return address of the loop body block was set wrongly by HardwareCondition.") - self.assertEqual({body_block: [(body, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the body element to the stack") + self.assertEqual({body_block: [(body, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the body element to the stack") self.assertFalse(condition.requires_stop()) def test_build_sequence_branch(self) -> None: @@ -39,7 +39,7 @@ def test_build_sequence_branch(self) -> None: trigger = Trigger() condition = HardwareCondition(trigger) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) self.assertEqual(2, len(block.embedded_blocks)) if_block = block.embedded_blocks[0] @@ -48,7 +48,7 @@ def test_build_sequence_branch(self) -> None: self.assertEqual([CJMPInstruction(trigger, InstructionPointer(if_block)), GOTOInstruction(InstructionPointer(else_block))], block.instructions, "The expected jump instruction were not generated by HardwareConditon.") self.assertEqual(InstructionPointer(block, 2), if_block.return_ip, "The return address of the if branch block was set wrongly by HardwareConditon.") self.assertEqual(InstructionPointer(block, 2), else_block.return_ip, "The return address of the else branch block was set wrongly by HardwareConditon.") - self.assertEqual({if_block: [(if_branch, {}, {})], else_block: [(else_branch, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the branch elements to the stack") + self.assertEqual({if_block: [(if_branch, {}, {}, {})], else_block: [(else_branch, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the branch elements to the stack") class IterationCallbackDummy: @@ -76,9 +76,9 @@ def test_build_cannot_evaluate(self) -> None: self.assertTrue(condition.requires_stop()) with self.assertRaises(ConditionEvaluationException): - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) with self.assertRaises(ConditionEvaluationException): - condition.build_sequence_branch(delegator, body, body, sequencer, {}, {}, block) + condition.build_sequence_branch(delegator, body, body, sequencer, {}, {}, {}, block) self.assertEqual(str(ConditionEvaluationException()), "The Condition can currently not be evaluated.") def test_build_sequence_loop_true(self) -> None: @@ -90,13 +90,13 @@ def test_build_sequence_loop_true(self) -> None: callback = IterationCallbackDummy(True) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) - self.assertEqual({block: [(delegator, {}, {}), (body, {}, {})]}, sequencer.sequencing_stacks) + self.assertEqual({block: [(delegator, {}, {}, {}), (body, {}, {}, {})]}, sequencer.sequencing_stacks) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) self.assertEqual(1, callback.loop_iteration) def test_build_sequence_loop_false(self): @@ -108,13 +108,13 @@ def test_build_sequence_loop_false(self): callback = IterationCallbackDummy(False) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) self.assertFalse(sequencer.sequencing_stacks) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) def test_build_sequence_branch_true(self): @@ -127,13 +127,13 @@ def test_build_sequence_branch_true(self): callback = IterationCallbackDummy(True) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) - self.assertEqual({block: [(if_branch, {}, {})]}, sequencer.sequencing_stacks) + self.assertEqual({block: [(if_branch, {}, {}, {})]}, sequencer.sequencing_stacks) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) @@ -147,13 +147,13 @@ def test_build_sequence_branch_false(self): callback = IterationCallbackDummy(False) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) - self.assertEqual({block: [(else_branch, {}, {})]}, sequencer.sequencing_stacks) + self.assertEqual({block: [(else_branch, {}, {}, {})]}, sequencer.sequencing_stacks) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) if __name__ == "__main__": diff --git a/tests/pulses/loop_pulse_template_tests.py b/tests/pulses/loop_pulse_template_tests.py index 0cb629ff5..903fc1255 100644 --- a/tests/pulses/loop_pulse_template_tests.py +++ b/tests/pulses/loop_pulse_template_tests.py @@ -58,13 +58,15 @@ def test_build_sequence(self) -> None: block = DummyInstructionBlock() parameters = {} conditions = {'foo_cond': condition} - t.build_sequence(sequencer, parameters, conditions, block) + measurement_mapping = {'swag':'aufdrehen'} + t.build_sequence(sequencer, parameters, conditions, measurement_mapping, block) expected_data = dict( delegator=t, body=body, sequencer=sequencer, parameters=parameters, conditions=conditions, + measurement_mapping=measurement_mapping, instruction_block=block ) self.assertEqual(expected_data, condition.loop_call_data) @@ -78,7 +80,7 @@ def test_condition_missing(self) -> None: block = DummyInstructionBlock() with self.assertRaises(ConditionMissingException): t.requires_stop({}, {}) - t.build_sequence(sequencer, {}, {}, block) + t.build_sequence(sequencer, {}, {}, {}, block) class LoopPulseTemplateSerializationTests(unittest.TestCase): diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index bc67591a4..e7dfbb849 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -2,10 +2,12 @@ import numpy -from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MissingParameterDeclarationException +from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ + MissingParameterDeclarationException, UnnecessaryMappingException from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, MappedParameter, ConstantParameter from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelPulseTemplate, MultiChannelWaveform from qctoolkit.expressions import Expression +from qctoolkit.pulses.pulse_template import SubTemplate from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform from tests.serialization_dummies import DummySerializer @@ -225,7 +227,7 @@ def test_init_multi_subtemplates_wrong_channel_mapping(self) -> None: {'bar'} ) - def test_init_broken_mappings(self) -> None: + def test_init_broken_parameter_mappings(self) -> None: st1 = DummyPulseTemplate(parameter_names={'foo'}, is_interruptable=True, num_channels=2, duration=1.3) st2 = DummyPulseTemplate(parameter_names={'bar'}, is_interruptable=True, num_channels=1, @@ -243,6 +245,29 @@ def test_init_broken_mappings(self) -> None: {} ) + def test_init_broken_measurement_mappings(self) -> None: + st1 = DummyPulseTemplate(measurement_names={'foo'}, num_channels=2) + st2 = DummyPulseTemplate(num_channels=1) + + with self.assertRaises(UnnecessaryMappingException): + MultiChannelPulseTemplate([SubTemplate(st1, {},measurement_mapping={'asd': 'fdg'},channel_mapping=[0, 2]), + SubTemplate(st2, {},channel_mapping=[1])], {}) + + def test_measurement_names(self) -> None: + st1 = DummyPulseTemplate(num_channels=2,measurement_names={'foo'}) + st2 = DummyPulseTemplate(num_channels=1, measurement_names={'bar'}) + + self.assertEqual( + MultiChannelPulseTemplate([ + SubTemplate(st1, {}, measurement_mapping={'foo': 'mw'}, channel_mapping=[0, 2]), + SubTemplate(st2, {}, measurement_mapping={'bar': 'mw'},channel_mapping=[1]) + ], {}).measurement_names, {'mw'}) + self.assertEqual( + MultiChannelPulseTemplate([ + SubTemplate(st1, {}, measurement_mapping={'foo': 'mw1'}, channel_mapping=[0, 2]), + SubTemplate(st2, {}, measurement_mapping={'bar': 'mw2'}, channel_mapping=[1]) + ], {}).measurement_names, {'mw1','mw2'}) + class MultiChannelPulseTemplateSequencingTests(unittest.TestCase): @@ -280,7 +305,7 @@ def test_build_sequence_no_params(self) -> None: pulse.build_waveform({}) with self.assertRaises(ParameterNotProvidedException): - pulse.build_sequence(DummySequencer(), dict(), dict(), DummyInstructionBlock()) + pulse.build_sequence(DummySequencer(), dict(), dict(), dict(), DummyInstructionBlock()) def test_build_sequence(self) -> None: dummy_wf1 = DummyWaveform(duration=2.3, num_channels=2) @@ -339,16 +364,16 @@ class MutliChannelPulseTemplateSerializationTests(unittest.TestCase): def __init__(self, methodName) -> None: super().__init__(methodName=methodName) self.maxDiff = None + self.dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, measurement_names={'meas_1'}) + self.dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1) def test_get_serialization_data(self) -> None: serializer = DummySerializer( serialize_callback=lambda x: str(x) if isinstance(x, Expression) else str(id(x))) - dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2) - dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1) template = MultiChannelPulseTemplate( [ - (dummy1, {'foo': "bar+3"}, [0, 2]), - (dummy2, {}, [1]) + SubTemplate(self.dummy1, {'foo': "bar+3"}, measurement_mapping={'meas_1': 'meas_N'}, channel_mapping=[0, 2]), + SubTemplate(self.dummy2, {}, channel_mapping=[1]) ], {'bar'}, identifier='herbert' @@ -356,11 +381,13 @@ def test_get_serialization_data(self) -> None: expected_data = dict( external_parameters=['bar'], subtemplates = [ - dict(template=str(id(dummy1)), + dict(template=str(id(self.dummy1)), parameter_mappings=dict(foo=str(Expression("bar+3"))), + measurement_mapping={'meas_1': 'meas_N'}, channel_mappings=[0, 2]), - dict(template=str(id(dummy2)), + dict(template=str(id(self.dummy2)), parameter_mappings=dict(), + measurement_mapping={}, channel_mappings=[1]) ] ) @@ -368,30 +395,31 @@ def test_get_serialization_data(self) -> None: self.assertEqual(expected_data, data) def test_deserialize(self) -> None: - dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2) - dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1) exp = Expression("bar - 35") data = dict( external_parameters=['bar'], subtemplates=[ - dict(template=str(id(dummy1)), + dict(template=str(id(self.dummy1)), parameter_mappings=dict(foo=str(exp)), + measurement_mapping={'meas_1': 'meas_N'}, channel_mappings=[0, 2]), - dict(template=str(id(dummy2)), + dict(template=str(id(self.dummy2)), parameter_mappings=dict(), + measurement_mapping={}, channel_mappings=[1]) ] ) serializer = DummySerializer(serialize_callback=lambda x: str(x) if isinstance(x, Expression) else str(id(x))) - serializer.subelements[str(id(dummy1))] = dummy1 - serializer.subelements[str(id(dummy2))] = dummy2 + serializer.subelements[str(id(self.dummy1))] = self.dummy1 + serializer.subelements[str(id(self.dummy2))] = self.dummy2 serializer.subelements[str(exp)] = exp template = MultiChannelPulseTemplate.deserialize(serializer, **data) self.assertEqual(set(data['external_parameters']), template.parameter_names) self.assertEqual({ParameterDeclaration('bar')}, template.parameter_declarations) + self.assertEqual(template.measurement_names,{'meas_N'}) recovered_data = template.get_serialization_data(serializer) self.assertEqual(data, recovered_data) diff --git a/tests/pulses/pulse_template_parameter_mapping_tests.py b/tests/pulses/pulse_template_parameter_mapping_tests.py index 8f5abdd59..445d40e92 100644 --- a/tests/pulses/pulse_template_parameter_mapping_tests.py +++ b/tests/pulses/pulse_template_parameter_mapping_tests.py @@ -1,6 +1,8 @@ import unittest -from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException +from qctoolkit.pulses.pulse_template import SubTemplate +from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, MissingMappingException,\ + UnnecessaryMappingException, MissingParameterDeclarationException, get_measurement_name_mappings from qctoolkit.expressions import Expression from qctoolkit.pulses.parameters import MappedParameter, ParameterNotProvidedException, ConstantParameter @@ -93,6 +95,61 @@ def test_map_parameters(self) -> None: ), mapped) +class GetMeasurementNameMappingsTest(unittest.TestCase): + def setUp(self): + self.template = DummyPulseTemplate() + + def test_no_mapping(self): + template = DummyPulseTemplate() + + mappings = get_measurement_name_mappings([SubTemplate(template, dict())]) + self.assertEqual(mappings,[dict()]) + + def test_mapping(self): + template = DummyPulseTemplate() + template.measurement_names_ = set(['foo', 'bar']) + + mapping_arg={'foo': 'hendrix', 'bar': 'shoe'} + mappings = get_measurement_name_mappings([SubTemplate(template,dict(),measurement_mapping=mapping_arg)]) + + self.assertEqual(mappings,[mapping_arg]) + + def test_missing_mapping(self): + template = DummyPulseTemplate() + template.measurement_names_ = set(['foo', 'bar']) + + mapping_arg = {'foo': 'hendrix'} + mappings = get_measurement_name_mappings([SubTemplate(template, dict(), measurement_mapping=mapping_arg)]) + + self.assertEqual(mappings, [dict(**mapping_arg,bar='bar')]) + + def test_unnecessary_mapping(self): + template = DummyPulseTemplate() + template.measurement_names_ = set(['foo', 'bar']) + + mapping_arg = {'foo': 'hendrix', 'my': 'oh_my'} + with self.assertRaises(UnnecessaryMappingException): + get_measurement_name_mappings([SubTemplate(template, dict(), measurement_mapping=mapping_arg)]) + + def test_multiple_arguments(self): + template1 = DummyPulseTemplate() + template1.measurement_names_ = set(['foo', 'bar']) + + template2 = DummyPulseTemplate() + template2.measurement_names_ = set(['hurr', 'durr']) + + mappings = get_measurement_name_mappings([SubTemplate(template1,dict(),measurement_mapping=dict()), + SubTemplate(template1,dict(),measurement_mapping={'foo': 'bar'}), + SubTemplate(template2, dict(), measurement_mapping={'hurr': 'bar','durr': 'bar'})]) + self.assertEqual(mappings,[dict(foo='foo',bar='bar'),dict(foo='bar',bar='bar'),{'hurr': 'bar','durr': 'bar'}]) + + with self.assertRaises(UnnecessaryMappingException): + get_measurement_name_mappings([SubTemplate(template1, dict(), measurement_mapping=dict()), + SubTemplate(template1, dict(), + measurement_mapping={'foo': 'bar'}), + SubTemplate(template2, dict(), + measurement_mapping={'hurr': 'bar', 'durr': 'bar', 'hopfen': 'malz'})]) + class PulseTemplateParameterMappingExceptionsTests(unittest.TestCase): def test_missing_parameter_declaration_exception_str(self) -> None: diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index 11684fd5e..4de63b13e 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -1,8 +1,8 @@ import unittest -from typing import Optional, Dict, Set, Any +from typing import Optional, Dict, Set, Any, List -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow from qctoolkit.pulses.instructions import Waveform, EXECInstruction from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration @@ -14,23 +14,29 @@ class AtomicPulseTemplateStub(AtomicPulseTemplate): def is_interruptable(self) -> bool: return super().is_interruptable() - def __init__(self, waveform: Waveform, identifier: Optional[str]=None) -> None: + def __init__(self, waveform: Waveform, measurement_windows: List[MeasurementWindow] = [], + identifier: Optional[str]=None) -> None: super().__init__(identifier=identifier) self.waveform = waveform + self.measurement_windows = measurement_windows def build_waveform(self, parameters: Dict[str, Parameter]): return self.waveform + def get_measurement_windows(self, parameters: Dict[str, Parameter] = None): + return self.measurement_windows + def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: return False - def get_measurement_windows(self, parameters: Dict[str, Parameter]=None) -> Any: + @property + def num_channels(self) -> int: raise NotImplementedError() @property - def num_channels(self) -> int: + def measurement_names(self): raise NotImplementedError() @property @@ -63,7 +69,7 @@ def test_build_sequence_no_waveform(self) -> None: block = DummyInstructionBlock() template = AtomicPulseTemplateStub(None) - template.build_sequence(sequencer, {}, {}, block) + template.build_sequence(sequencer, {}, {}, {}, block) self.assertFalse(block.instructions) def test_build_sequence(self) -> None: @@ -72,5 +78,5 @@ def test_build_sequence(self) -> None: block = DummyInstructionBlock() template = AtomicPulseTemplateStub(wf) - template.build_sequence(sequencer, {}, {}, block) + template.build_sequence(sequencer, {}, {}, {}, block) self.assertEqual([EXECInstruction(wf)], block.instructions) \ No newline at end of file diff --git a/tests/pulses/repetition_pulse_template_tests.py b/tests/pulses/repetition_pulse_template_tests.py index e9ca798e4..83fe1415a 100644 --- a/tests/pulses/repetition_pulse_template_tests.py +++ b/tests/pulses/repetition_pulse_template_tests.py @@ -85,24 +85,26 @@ def test_build_sequence_constant(self) -> None: repetitions = 3 t = RepetitionPulseTemplate(self.body, repetitions) parameters = {} + measurement_mapping = {'my' : 'thy'} conditions = dict(foo=DummyCondition(requires_stop=True)) - t.build_sequence(self.sequencer, parameters, conditions, self.block) + t.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, self.block) self.assertTrue(self.block.embedded_blocks) body_block = self.block.embedded_blocks[0] self.assertEqual({body_block}, set(self.sequencer.sequencing_stacks.keys())) - self.assertEqual([(self.body, parameters, conditions)], self.sequencer.sequencing_stacks[body_block]) + self.assertEqual([(self.body, parameters, conditions, measurement_mapping)], self.sequencer.sequencing_stacks[body_block]) self.assertEqual([REPJInstruction(repetitions, InstructionPointer(body_block, 0))], self.block.instructions) def test_build_sequence_declaration_success(self) -> None: parameters = dict(foo=3) conditions = dict(foo=DummyCondition(requires_stop=True)) - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + measurement_mapping = dict(moth='fire') + self.template.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, self.block) self.assertTrue(self.block.embedded_blocks) body_block = self.block.embedded_blocks[0] self.assertEqual({body_block}, set(self.sequencer.sequencing_stacks.keys())) - self.assertEqual([(self.body, parameters, conditions)], + self.assertEqual([(self.body, parameters, conditions, measurement_mapping)], self.sequencer.sequencing_stacks[body_block]) self.assertEqual([REPJInstruction(parameters['foo'], InstructionPointer(body_block, 0))], self.block.instructions) @@ -111,21 +113,21 @@ def test_build_sequence_declaration_exceeds_bounds(self) -> None: parameters = dict(foo=9) conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterValueIllegalException): - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) def test_build_sequence_declaration_parameter_missing(self) -> None: parameters = {} conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterNotProvidedException): - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) def test_build_sequence_declaration_parameter_value_not_whole(self) -> None: parameters = dict(foo=3.3) conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterNotIntegerException): - self.template.build_sequence(self.sequencer, parameters, conditions, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index 98d88b168..d577eafcc 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -1,7 +1,7 @@ import unittest import copy -from qctoolkit.pulses.pulse_template import DoubleParameterNameException +from qctoolkit.pulses.pulse_template import DoubleParameterNameException, SubTemplate from qctoolkit.expressions import Expression from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate @@ -21,6 +21,7 @@ def __init__(self, *args, **kwargs) -> None: self.square.add_entry('up', 'v', 'hold') self.square.add_entry('down', 0, 'hold') self.square.add_entry('length', 0) + self.square.add_measurement_declaration('mw1', 'up', 'down+length') self.mapping1 = { 'up': 'uptime', @@ -29,6 +30,8 @@ def __init__(self, *args, **kwargs) -> None: 'length': '0.5 * pulse_length' } + self.window_name_mapping = {'mw1' : 'test_window'} + self.outer_parameters = ['uptime', 'length', 'pulse_length', 'voltage'] self.parameters = {} @@ -37,13 +40,13 @@ def __init__(self, *args, **kwargs) -> None: self.parameters['pulse_length'] = ConstantParameter(100) self.parameters['voltage'] = ConstantParameter(10) - self.sequence = SequencePulseTemplate([(self.square, self.mapping1)], self.outer_parameters) + self.sequence = SequencePulseTemplate([SubTemplate(self.square, self.mapping1, measurement_mapping=self.window_name_mapping)], self.outer_parameters) def test_missing_mapping(self) -> None: mapping = self.mapping1 mapping.pop('v') - subtemplates = [(self.square, mapping)] + subtemplates = [(self.square, mapping, {})] with self.assertRaises(MissingMappingException): SequencePulseTemplate(subtemplates, self.outer_parameters) @@ -51,7 +54,7 @@ def test_unnecessary_mapping(self) -> None: mapping = self.mapping1 mapping['unnecessary'] = 'voltage' - subtemplates = [(self.square, mapping)] + subtemplates = [(self.square, mapping, {})] with self.assertRaises(UnnecessaryMappingException): SequencePulseTemplate(subtemplates, self.outer_parameters) @@ -62,14 +65,14 @@ def test_identifier(self) -> None: def test_multiple_channels(self) -> None: dummy = DummyPulseTemplate(parameter_names={'hugo'}, num_channels=2) - subtemplates = [(dummy, {'hugo': 'foo'}), (dummy, {'hugo': '3'})] + subtemplates = [(dummy, {'hugo': 'foo'}, {}), (dummy, {'hugo': '3'}, {})] sequence = SequencePulseTemplate(subtemplates, {'foo'}) self.assertEqual(2, sequence.num_channels) def test_multiple_channels_mismatch(self) -> None: dummy1 = DummyPulseTemplate(num_channels=6) dummy2 = DummyPulseTemplate(num_channels=5) - subtemplates = [(dummy1, dict()), (dummy2, dict())] + subtemplates = [(dummy1, dict(), dict()), (dummy2, dict(), dict())] with self.assertRaises(ValueError): SequencePulseTemplate(subtemplates, set()) @@ -82,14 +85,17 @@ def setUp(self) -> None: self.table_foo = TablePulseTemplate(identifier='foo') self.table_foo.add_entry('hugo', 2) self.table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') + self.table_foo.add_measurement_declaration('mw_foo','hugo','albert') - self.table = TablePulseTemplate(measurement=True) - self.foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') + self.foo_param_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') + self.foo_meas_mappings = dict(mw_foo='mw_bar') + + self.table = TablePulseTemplate() def test_get_serialization_data(self) -> None: serializer = DummySerializer(serialize_callback=lambda x: str(x)) - foo_mappings = {k: Expression(v) for k, v in self.foo_mappings.items()} - sequence = SequencePulseTemplate([(self.table_foo, self.foo_mappings), (self.table, {})], + foo_mappings = {k: Expression(v) for k, v in self.foo_param_mappings.items()} + sequence = SequencePulseTemplate([SubTemplate(self.table_foo, self.foo_param_mappings, self.foo_meas_mappings), (self.table, {})], ['ilse', 'albert', 'voltage'], identifier='foo') expected_data = dict( @@ -97,21 +103,25 @@ def test_get_serialization_data(self) -> None: external_parameters=['albert', 'ilse', 'voltage'], is_interruptable=True, subtemplates = [ - dict(template=str(self.table_foo), mappings={k: str(v) for k, v in foo_mappings.items()}), - dict(template=str(self.table), mappings=dict()) + dict(template=str(self.table_foo), + parameter_mappings={k: str(v) for k, v in foo_mappings.items()}, + measurement_mappings=self.foo_meas_mappings), + dict(template=str(self.table), parameter_mappings=dict(), measurement_mappings=dict()) ] ) data = sequence.get_serialization_data(serializer) self.assertEqual(expected_data, data) def test_deserialize(self) -> None: - foo_mappings = {k: Expression(v) for k, v in self.foo_mappings.items()} + foo_param_mappings = {k: Expression(v) for k, v in self.foo_param_mappings.items()} data = dict( external_parameters={'ilse', 'albert', 'voltage'}, is_interruptable=True, subtemplates = [ - dict(template=str(id(self.table_foo)), mappings={k: str(id(v)) for k, v in foo_mappings.items()}), - dict(template=str(id(self.table)), mappings=dict()) + dict(template=str(id(self.table_foo)), + parameter_mappings={k: str(id(v)) for k, v in foo_param_mappings.items()}, + measurement_mappings=self.foo_meas_mappings), + dict(template=str(id(self.table)), parameter_mappings=dict(),measurement_mappings=dict()) ], identifier='foo' ) @@ -119,7 +129,7 @@ def test_deserialize(self) -> None: # prepare dependencies for deserialization self.serializer.subelements[str(id(self.table_foo))] = self.table_foo self.serializer.subelements[str(id(self.table))] = self.table - for v in foo_mappings.values(): + for v in foo_param_mappings.values(): self.serializer.subelements[str(id(v))] = v # deserialize @@ -132,7 +142,7 @@ def test_deserialize(self) -> None: self.assertIs(self.table_foo, sequence.subtemplates[0][0]) self.assertIs(self.table, sequence.subtemplates[1][0]) #self.assertEqual(self.foo_mappings, {k: m.string for k,m in sequence.subtemplates[0][1].items()}) - self.assertEqual(foo_mappings, sequence.subtemplates[0][1]) + self.assertEqual(foo_param_mappings, sequence.subtemplates[0][1]) self.assertEqual(dict(), sequence.subtemplates[1][1]) self.assertEqual(data['identifier'], sequence.identifier) @@ -145,7 +155,7 @@ def test_missing_parameter(self): parameters = copy.deepcopy(self.parameters) parameters.pop('uptime') with self.assertRaises(ParameterNotProvidedException): - self.sequence.build_sequence(sequencer, parameters, {}, block) + self.sequence.build_sequence(sequencer, parameters, {}, {}, block) def test_build_sequence(self) -> None: sub1 = DummyPulseTemplate(requires_stop=False) @@ -154,19 +164,19 @@ def test_build_sequence(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() - seq = SequencePulseTemplate([(sub1, {}), (sub2, {'foo': 'foo'})], {'foo'}) - seq.build_sequence(sequencer, parameters, {}, block) + seq = SequencePulseTemplate([(sub1, {}, {}), (sub2, {'foo': 'foo'}, {})], {'foo'}) + seq.build_sequence(sequencer, parameters, {}, {}, block) self.assertEqual(2, len(sequencer.sequencing_stacks[block])) sequencer = DummySequencer() block = DummyInstructionBlock() - seq = SequencePulseTemplate([(sub2, {'foo': 'foo'}), (sub1, {})], {'foo'}) - seq.build_sequence(sequencer, parameters, {}, block) + seq = SequencePulseTemplate([(sub2, {'foo': 'foo'}, {}), (sub1, {}, {})], {'foo'}) + seq.build_sequence(sequencer, parameters, {}, {}, block) self.assertEqual(2, len(sequencer.sequencing_stacks[block])) def test_requires_stop(self) -> None: - sub1 = (DummyPulseTemplate(requires_stop=False), {}) - sub2 = (DummyPulseTemplate(requires_stop=True, parameter_names={'foo'}), {'foo': 'foo'}) + sub1 = (DummyPulseTemplate(requires_stop=False), {}, {}) + sub2 = (DummyPulseTemplate(requires_stop=True, parameter_names={'foo'}), {'foo': 'foo'}, {}) parameters = {'foo': DummyNoValueParameter()} seq = SequencePulseTemplate([],[]) @@ -188,7 +198,7 @@ def test_missing_parameter_declaration_exception(self): mapping = copy.deepcopy(self.mapping1) mapping['up'] = "foo" - subtemplates = [(self.square, mapping)] + subtemplates = [(self.square, mapping,{})] with self.assertRaises(MissingParameterDeclarationException): SequencePulseTemplate(subtemplates, self.outer_parameters) @@ -213,7 +223,7 @@ def test_crash(self) -> None: 'vb': 'va + vb', 'tend': '2 * tend' } - sequence = SequencePulseTemplate([(table, first_mapping), (table, second_mapping)], external_parameters) + sequence = SequencePulseTemplate([(table, first_mapping, {}), (table, second_mapping, {})], external_parameters) parameters = { 'ta': ConstantParameter(2), @@ -227,7 +237,7 @@ def test_crash(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() self.assertFalse(sequence.requires_stop(parameters, {})) - sequence.build_sequence(sequencer, parameters, {}, block) + sequence.build_sequence(sequencer, parameters, {}, {}, block) from qctoolkit.pulses.sequencing import Sequencer s = Sequencer() s.push(sequence, parameters) @@ -266,7 +276,7 @@ def test_concatenation_sequence_table_pulse(self): concat = seq @ a subtemplates2 = [(seq, {'t_ext':'t_ext', 'a_ext': 'a_ext'}), - (a, {'t':'t', 'a':'a'})] + (a, {'t': 't', 'a':'a'})] seq2 = SequencePulseTemplate(subtemplates2, ['a', 'a_ext', 't', 't_ext']) self.assertEqual(concat.parameter_names, seq2.parameter_names) self.assertEqual(concat.subtemplates, seq2.subtemplates) diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index 5134dbe22..dbb950c98 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -62,6 +62,7 @@ def __init__(self, requires_stop: bool = False, push_elements: Tuple[Instruction self.target_block = None self.parameters = None self.conditions = None + self.window_mapping = None self.requires_stop_ = requires_stop self.push_elements = push_elements self.parameter_names = set() @@ -71,15 +72,17 @@ def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], + measurement_mapping: Optional[Dict[str, str]], instruction_block: InstructionBlock) -> None: self.build_call_counter += 1 self.target_block = instruction_block instruction_block.add_instruction(DummyInstruction(self)) self.parameters = parameters self.conditions = conditions + self.window_mapping = measurement_mapping if self.push_elements is not None: for element in self.push_elements[1]: - sequencer.push(element, parameters, conditions, self.push_elements[0]) + sequencer.push(element, parameters, conditions, measurement_mapping, self.push_elements[0]) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Conditions']) -> bool: self.requires_stop_call_counter += 1 @@ -152,6 +155,7 @@ def push(self, sequencing_element: SequencingElement, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], + window_mapping: Optional[Dict[str, str]] = None, target_block: InstructionBlock = None) -> None: if target_block is None: target_block = self.__main_block @@ -159,7 +163,7 @@ def push(self, if target_block not in self.sequencing_stacks: self.sequencing_stacks[target_block] = [] - self.sequencing_stacks[target_block].append((sequencing_element, parameters, conditions)) + self.sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping)) def build(self) -> InstructionBlock: raise NotImplementedError() @@ -198,6 +202,7 @@ def build_sequence_loop(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: self.loop_call_data = dict( delegator=delegator, @@ -205,6 +210,7 @@ def build_sequence_loop(self, sequencer=sequencer, parameters=parameters, conditions=conditions, + measurement_mapping=measurement_mapping, instruction_block=instruction_block ) @@ -215,6 +221,7 @@ def build_sequence_branch(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock) -> None: self.branch_call_data = dict( delegator=delegator, @@ -223,6 +230,7 @@ def build_sequence_branch(self, sequencer=sequencer, parameters=parameters, conditions=conditions, + measurement_mapping=measurement_mapping, instruction_block=instruction_block ) @@ -235,7 +243,8 @@ def __init__(self, parameter_names: Set[str]={}, num_channels: int=1, duration: float=0, - waveform: Waveform=None) -> None: + waveform: Waveform=None, + measurement_names: Set[str] = set()) -> None: super().__init__() self.requires_stop_ = requires_stop self.is_interruptable_ = is_interruptable @@ -245,6 +254,7 @@ def __init__(self, self.duration = duration self.waveform = waveform self.build_waveform_calls = [] + self.measurement_names_ = measurement_names @property def parameter_names(self) -> Set[str]: @@ -266,10 +276,15 @@ def is_interruptable(self) -> bool: def num_channels(self) -> int: return self.num_channels_ + @property + def measurement_names(self) -> Set[str]: + return self.measurement_names_ + def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], instruction_block: InstructionBlock): self.build_sequence_calls += 1 diff --git a/tests/pulses/sequencing_tests.py b/tests/pulses/sequencing_tests.py index e456a23a9..76b09d010 100644 --- a/tests/pulses/sequencing_tests.py +++ b/tests/pulses/sequencing_tests.py @@ -18,9 +18,10 @@ def test_push(self) -> None: ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo' : 'bar'} elem = DummySequencingElement() - sequencer.push(elem, ps, cs) + sequencer.push(elem, ps, cs, wm) self.assertFalse(sequencer.has_finished()) sequencer.build() self.assertEqual(ps, elem.parameters) @@ -77,8 +78,9 @@ def test_build_path_o1_m2_i1_f_i0_one_element_custom_block_requires_stop(self) - elem = DummySequencingElement(True) ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {} target_block = InstructionBlock() - sequencer.push(elem, ps, cs, target_block) + sequencer.push(elem, ps, cs, wm, target_block) sequencer.build() self.assertFalse(sequencer.has_finished()) @@ -92,13 +94,13 @@ def test_build_path_o1_m2_i1_f_i1_f_one_element_custom_and_main_block_requires_s ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - + wm = {} elem_main = DummySequencingElement(True) sequencer.push(elem_main, ps, cs) elem_cstm = DummySequencingElement(True) target_block = InstructionBlock() - sequencer.push(elem_cstm, ps, cs, target_block) + sequencer.push(elem_cstm, ps, cs, wm, target_block) sequencer.build() @@ -248,10 +250,11 @@ def test_build_path_o2_m2_i0_i1_t_m2_i0_i0_one_element_custom_block(self) -> Non ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - + wm = {'foo' : 'bar'} + target_block = InstructionBlock() elem = DummySequencingElement(False) - sequencer.push(elem, ps, cs, target_block) + sequencer.push(elem, ps, cs, wm, target_block) sequencer.build() @@ -268,13 +271,14 @@ def test_build_path_o2_m2_i1_f_i1_t_m2_i1_f_i0_one_element_custom_block_one_elem ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} elem_main = DummySequencingElement(True) sequencer.push(elem_main, ps, cs) target_block = InstructionBlock() elem_cstm = DummySequencingElement(False) - sequencer.push(elem_cstm, ps, cs, target_block) + sequencer.push(elem_cstm, ps, cs, wm, target_block) sequencer.build() @@ -294,13 +298,14 @@ def test_build_path_o2_m2_i1_t_i1_t_m2_i0_i0_one_element_custom_block_one_elemen ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) target_block = InstructionBlock() elem_cstm = DummySequencingElement(False) - sequencer.push(elem_cstm, ps, cs, target_block) + sequencer.push(elem_cstm, ps, cs, wm, target_block) sequence = sequencer.build() @@ -323,13 +328,14 @@ def test_build_path_o2_m2_i0_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_req ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) sequencer.build() @@ -349,13 +355,14 @@ def test_build_path_o2_m2_i0_i2_tt_m2_i0_i0_two_elements_custom_block(self) -> N ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) sequencer.build() @@ -376,13 +383,14 @@ def test_build_path_o2_m2_i1_f_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_last ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) elem_main = DummySequencingElement(True) sequencer.push(elem_main, ps, cs) @@ -409,13 +417,14 @@ def test_build_path_o2_m2_i1_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_r ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) @@ -445,13 +454,14 @@ def test_build_path_o2_m2_i1_t_i2_tt_m2_i0_i0_two_elements_custom_block_one_elem ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - + wm = {'foo': 'bar'} + target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) @@ -482,13 +492,14 @@ def test_build_path_o2_m2_i2_tf_t_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_l ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) elem_main2 = DummySequencingElement(True) sequencer.push(elem_main2, ps, cs) @@ -526,13 +537,14 @@ def test_build_path_o2_m2_i2_tt_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_las ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) @@ -570,14 +582,14 @@ def test_build_path_o2_m2_i2_tt_t_i2_tt_m2_i0_i0_two_elements_custom_block_two_e ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - + wm = {'foo': 'bar'} target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, target_block) + sequencer.push(elem2, ps, cs, wm, target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, target_block) + sequencer.push(elem1, ps, cs, wm, target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 0fa808b0e..465788daa 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -38,27 +38,30 @@ def test_add_entry_for_interpolation(self) -> None: table.add_entry(1,2, "bar") def test_measurement_windows(self) -> None: - pulse = TablePulseTemplate(measurement=True) + pulse = TablePulseTemplate() pulse.add_entry(1, 1) pulse.add_entry(3, 0) pulse.add_entry(5, 0) - windows = pulse.get_measurement_windows() - self.assertEqual([(0,5)], windows) + pulse.add_measurement_declaration('mw',0,5) + windows = pulse.get_measurement_windows({}) + self.assertEqual([('mw',0,5)], windows) def test_no_measurement_windows(self) -> None: - pulse = TablePulseTemplate(measurement=False) + pulse = TablePulseTemplate() pulse.add_entry(1, 1) pulse.add_entry(3, 0) pulse.add_entry(5, 0) - windows = pulse.get_measurement_windows() + windows = pulse.get_measurement_windows({}) self.assertEqual([], windows) def test_measurement_windows_with_parameters(self) -> None: - pulse = TablePulseTemplate(measurement=True) + pulse = TablePulseTemplate() + pulse.add_entry(1, 1) pulse.add_entry('length', 0) + pulse.add_measurement_declaration('mw',1,'(1+length)/2') parameters = dict(length=100) windows = pulse.get_measurement_windows(parameters) - self.assertEqual(windows, [(0, 100)]) + self.assertEqual(windows, [('mw', 1, 101/2)]) def test_add_entry_empty_time_is_negative(self) -> None: table = TablePulseTemplate() @@ -596,7 +599,7 @@ def test_get_instaniated_entries_multi_one_empty_channel(self) -> None: self.assertEqual(expected, entries) def test_measurement_windows_multi(self) -> None: - pulse = TablePulseTemplate(measurement=True, channels=2) + pulse = TablePulseTemplate(channels=2) pulse.add_entry(1, 1) pulse.add_entry(3, 0) pulse.add_entry(5, 0) @@ -604,31 +607,49 @@ def test_measurement_windows_multi(self) -> None: pulse.add_entry(1, 1, channel=1) pulse.add_entry(3, 0, channel=1) pulse.add_entry(10, 0, channel=1) - windows = pulse.get_measurement_windows() - self.assertEqual([(0,10)], windows) + + pulse.add_measurement_declaration('mw',1,7) + windows = pulse.get_measurement_windows({}) + self.assertEqual([('mw',1,7)], windows) + + def test_measurement_windows_multi_out_of_pulse(self) -> None: + pulse = TablePulseTemplate(channels=2) + pulse.add_entry(1, 1) + pulse.add_entry(3, 0) + pulse.add_entry(5, 0) + + pulse.add_entry(1, 1, channel=1) + pulse.add_entry(3, 0, channel=1) + pulse.add_entry(10, 0, channel=1) + + with self.assertRaises(ValueError): + pulse.add_measurement_declaration('mw', 1, 't_meas') + pulse.get_measurement_windows({'t_meas':20}) class TablePulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: self.serializer = DummySerializer(lambda x: dict(name=x.name), lambda x: x.name, lambda x: x['name']) - self.template = TablePulseTemplate(measurement=True, identifier='foo') + self.template = TablePulseTemplate(identifier='foo') self.expected_data = dict(type=self.serializer.get_type_identifier(self.template)) def test_get_serialization_data(self) -> None: self.template.add_entry('foo', 2) self.template.add_entry('hugo', 'ilse', interpolation='linear') + self.template.add_measurement_declaration('mw',2,'hugo+franz') - self.expected_data['is_measurement_pulse'] = True - self.expected_data['time_parameter_declarations'] = [dict(name='foo'), dict(name='hugo')] + self.expected_data['measurement_declarations'] = {'mw': [(2,'hugo+franz')]} + self.expected_data['time_parameter_declarations'] = [dict(name=name) for name in sorted(['foo','hugo','franz'])] self.expected_data['voltage_parameter_declarations'] = [dict(name='ilse')] self.expected_data['entries'] = [[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')]] + data = self.template.get_serialization_data(self.serializer) self.assertEqual(self.expected_data, data) def test_deserialize(self) -> None: - data = dict(is_measurement_pulse=True, - time_parameter_declarations=[dict(name='hugo'), dict(name='foo')], + data = dict(measurement_declarations={'mw': [(2,'hugo+franz')]}, + time_parameter_declarations=[dict(name='hugo'), dict(name='foo'), dict(name='franz')], voltage_parameter_declarations=[dict(name='ilse')], entries=[[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')]], identifier='foo') @@ -637,6 +658,7 @@ def test_deserialize(self) -> None: self.serializer.subelements['foo'] = ParameterDeclaration('foo') self.serializer.subelements['hugo'] = ParameterDeclaration('hugo') self.serializer.subelements['ilse'] = ParameterDeclaration('ilse') + self.serializer.subelements['franz'] = ParameterDeclaration('franz') # deserialize template = TablePulseTemplate.deserialize(self.serializer, **data) @@ -653,7 +675,7 @@ def test_deserialize(self) -> None: # compare! self.assertEqual(all_declarations, template.parameter_declarations) - self.assertEqual({'foo', 'hugo', 'ilse'}, template.parameter_names) + self.assertEqual({'foo', 'hugo', 'ilse', 'franz'}, template.parameter_names) self.assertEqual(entries, template.entries) self.assertEqual('foo', template.identifier) @@ -671,7 +693,7 @@ def test_build_sequence(self) -> None: waveform = table.build_waveform(parameters) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, instruction_block) + table.build_sequence(sequencer, parameters, {}, {}, instruction_block) expected_waveform = TableWaveform(instantiated_entries) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] @@ -683,7 +705,7 @@ def test_build_sequence_empty(self) -> None: table = TablePulseTemplate() sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, {}, {}, instruction_block) + table.build_sequence(sequencer, {}, {}, {}, instruction_block) self.assertFalse(instruction_block.instructions) self.assertIsNone(table.build_waveform({})) @@ -732,7 +754,7 @@ def test_build_sequence_multi(self) -> None: sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, instruction_block) + table.build_sequence(sequencer, parameters, {}, {}, instruction_block) expected_waveform = TableWaveform(instantiated_entries) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] @@ -750,7 +772,7 @@ def test_build_sequence_multi_one_channel_empty(self) -> None: sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, instruction_block) + table.build_sequence(sequencer, parameters, {}, {}, instruction_block) expected_waveform = TableWaveform(instantiated_entries) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] diff --git a/tests/pulses/table_sequence_sequencer_intergration_tests.py b/tests/pulses/table_sequence_sequencer_intergration_tests.py index 3b7db9244..82a097082 100644 --- a/tests/pulses/table_sequence_sequencer_intergration_tests.py +++ b/tests/pulses/table_sequence_sequencer_intergration_tests.py @@ -1,9 +1,11 @@ import unittest +from qctoolkit.pulses.pulse_template import SubTemplate from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate from qctoolkit.pulses.parameters import ParameterNotProvidedException from qctoolkit.pulses.sequencing import Sequencer +from qctoolkit.pulses.instructions import EXECInstruction from tests.pulses.sequencing_dummies import DummyParameter, DummyNoValueParameter @@ -14,13 +16,16 @@ def test_table_sequence_sequencer_integration(self) -> None: t1 = TablePulseTemplate() t1.add_entry(2, 'foo') t1.add_entry(5, 0) + t1.add_measurement_declaration('foo', 2, 5) t2 = TablePulseTemplate() t2.add_entry(4, 0) t2.add_entry(4.5, 'bar', 'linear') t2.add_entry(5, 0) + t2.add_measurement_declaration('foo', 4, 5) - seqt = SequencePulseTemplate([(t1, {'foo': 'foo'}), (t2, {'bar': '2 * hugo'})], {'foo', 'hugo'}) + seqt = SequencePulseTemplate([SubTemplate(t1, {'foo': 'foo'}, measurement_mapping={'foo': 'bar'}), + SubTemplate(t2, {'bar': '2 * hugo'})], {'foo', 'hugo'}) with self.assertRaises(ParameterNotProvidedException): t1.requires_stop(dict(), dict()) @@ -31,7 +36,7 @@ def test_table_sequence_sequencer_integration(self) -> None: foo = DummyNoValueParameter() bar = DummyNoValueParameter() sequencer = Sequencer() - sequencer.push(seqt, {'foo': foo, 'hugo': bar}) + sequencer.push(seqt, {'foo': foo, 'hugo': bar}, window_mapping=dict(bar='my', foo='thy')) instructions = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertEqual(1, len(instructions)) @@ -39,7 +44,7 @@ def test_table_sequence_sequencer_integration(self) -> None: foo = DummyParameter(value=1.1) bar = DummyNoValueParameter() sequencer = Sequencer() - sequencer.push(seqt, {'foo': foo, 'hugo': bar}) + sequencer.push(seqt, {'foo': foo, 'hugo': bar}, window_mapping=dict(bar='my', foo='thy')) instructions = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertEqual(2, len(instructions)) @@ -47,7 +52,7 @@ def test_table_sequence_sequencer_integration(self) -> None: foo = DummyParameter(value=1.1) bar = DummyNoValueParameter() sequencer = Sequencer() - sequencer.push(seqt, {'foo': bar, 'hugo': foo}) + sequencer.push(seqt, {'foo': bar, 'hugo': foo}, window_mapping=dict(bar='my', foo='thy')) instructions = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertEqual(1, len(instructions)) @@ -55,7 +60,11 @@ def test_table_sequence_sequencer_integration(self) -> None: foo = DummyParameter(value=1.1) bar = DummyParameter(value=-0.2) sequencer = Sequencer() - sequencer.push(seqt, {'foo': foo, 'hugo': bar}) + sequencer.push(seqt, {'foo': foo, 'hugo': bar}, window_mapping=dict(bar='my', foo='thy')) instructions = sequencer.build() self.assertTrue(sequencer.has_finished()) - self.assertEqual(3, len(instructions)) \ No newline at end of file + self.assertEqual(3, len(instructions)) + + for instruction in instructions: + if isinstance(instruction,EXECInstruction): + self.assertIn(instruction.measurement_windows[0], [('my', 2, 5),('thy', 4, 5)]) diff --git a/tests/serialization_tests.py b/tests/serialization_tests.py index e99011074..c6f89aa2e 100644 --- a/tests/serialization_tests.py +++ b/tests/serialization_tests.py @@ -327,9 +327,9 @@ def test_serialization_and_deserialization_combined(self) -> None: table_foo = TablePulseTemplate(identifier='foo') table_foo.add_entry('hugo', 2) table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') - table = TablePulseTemplate(measurement=True) + table = TablePulseTemplate() foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') - sequence = SequencePulseTemplate([(table_foo, foo_mappings), (table, {})], ['ilse', 'albert', 'voltage'], identifier=None) + sequence = SequencePulseTemplate([(table_foo, foo_mappings, {}), (table, {}, {})], ['ilse', 'albert', 'voltage'], identifier=None) storage = DummyStorageBackend() serializer = Serializer(storage) From 10c06465d23a046ffc4fc06d52ec84075f005bec Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 23 Nov 2016 11:38:03 +0100 Subject: [PATCH 010/116] Add REPJInstruction export --- qctoolkit/pulses/instructions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index 24d7086b7..76f29b312 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -24,7 +24,7 @@ __all__ = ["Waveform", "Trigger", "InstructionPointer", "Instruction", "CJMPInstruction", "EXECInstruction", - "GOTOInstruction", "STOPInstruction", "AbstractInstructionBlock", "InstructionBlock", + "GOTOInstruction", "STOPInstruction", "REPJInstruction", "AbstractInstructionBlock", "InstructionBlock", "ImmutableInstructionBlock", "InstructionSequence" ] From e1b94348abf5a2e824a1afb3be908667d141c839 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 6 Dec 2016 10:15:02 +0100 Subject: [PATCH 011/116] Them mapping of parameters, measurement window names and channel ids is now done in the class MappingTemplate Sequence- and MultiChannelPulseTemplate check only things that are bound to their purpose Channels now have an ID --- qctoolkit/pulses/branch_pulse_template.py | 12 +- qctoolkit/pulses/conditions.py | 20 +- qctoolkit/pulses/function_pulse_template.py | 41 +- qctoolkit/pulses/instructions.py | 60 ++- qctoolkit/pulses/loop_pulse_template.py | 8 +- .../pulses/multi_channel_pulse_template.py | 250 +++++------ qctoolkit/pulses/pulse_template.py | 51 +-- .../pulse_template_parameter_mapping.py | 224 +++++----- qctoolkit/pulses/repetition_pulse_template.py | 7 +- qctoolkit/pulses/sequence_pulse_template.py | 148 ++----- qctoolkit/pulses/sequencing.py | 13 +- qctoolkit/pulses/table_pulse_template.py | 154 +++---- tests/pulses/branch_pulse_template_tests.py | 55 +-- tests/pulses/conditions_tests.py | 37 +- tests/pulses/function_pulse_tests.py | 36 +- tests/pulses/instructions_tests.py | 46 +- tests/pulses/loop_pulse_template_tests.py | 6 +- .../multi_channel_pulse_template_tests.py | 397 ++++++------------ tests/pulses/plotting_tests.py | 6 +- .../pulse_template_parameter_mapping_tests.py | 234 +++++------ tests/pulses/pulse_template_tests.py | 27 +- .../pulses/repetition_pulse_template_tests.py | 16 +- tests/pulses/sequence_pulse_template_tests.py | 192 +++++---- tests/pulses/sequencing_dummies.py | 45 +- tests/pulses/sequencing_tests.py | 50 +-- tests/pulses/table_pulse_template_tests.py | 337 +++++++-------- ...e_sequence_sequencer_intergration_tests.py | 27 +- 27 files changed, 1162 insertions(+), 1337 deletions(-) diff --git a/qctoolkit/pulses/branch_pulse_template.py b/qctoolkit/pulses/branch_pulse_template.py index dd398c1b4..56ffbcda4 100644 --- a/qctoolkit/pulses/branch_pulse_template.py +++ b/qctoolkit/pulses/branch_pulse_template.py @@ -4,7 +4,7 @@ from typing import Dict, Set, List, Optional, Any from qctoolkit.pulses.parameters import Parameter -from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow +from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.conditions import Condition, ConditionMissingException from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock from qctoolkit.serialization import Serializer @@ -42,8 +42,8 @@ def __init__(self, identifier (str): A unique identifier for use in serialization. (optional) """ super().__init__(identifier=identifier) - if if_branch.num_channels != else_branch.num_channels: - raise ValueError("The number of channels in the provided pulses differ!") + if if_branch.defined_channels != else_branch.defined_channels: + raise ValueError("The channels defined by the provided pulses differ!") self.__condition = condition self.__if_branch = if_branch self.__else_branch = else_branch @@ -65,8 +65,8 @@ def is_interruptable(self) -> bool: return self.__if_branch.is_interruptable and self.__else_branch.is_interruptable @property - def num_channels(self) -> int: - return self.__if_branch.num_channels + def defined_channels(self) -> Set['ChannelID']: + return self.__if_branch.defined_channels @property def measurement_names(self) -> Set[str]: @@ -83,6 +83,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: self.__obtain_condition_object(conditions).build_sequence_branch(self, self.__if_branch, @@ -91,6 +92,7 @@ def build_sequence(self, parameters, conditions, measurement_mapping, + channel_mapping, instruction_block) def requires_stop(self, diff --git a/qctoolkit/pulses/conditions.py b/qctoolkit/pulses/conditions.py index f57dc63b5..fbdf1e969 100644 --- a/qctoolkit/pulses/conditions.py +++ b/qctoolkit/pulses/conditions.py @@ -47,6 +47,7 @@ def build_sequence_loop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: """Translate a looping SequencingElement using this Condition into an instruction sequence for the given instruction block using sequencer and the given parameter sets. @@ -75,6 +76,7 @@ def build_sequence_branch(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: """Translate a branching SequencingElement using this Condition into an instruction sequence for the given instruction block using sequencer and the given parameter sets. @@ -125,13 +127,14 @@ def build_sequence_loop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: body_block = InstructionBlock() body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block.instructions)) instruction_block.add_instruction_cjmp(self.__trigger, body_block) - sequencer.push(body, parameters, conditions, measurement_mapping, body_block) + sequencer.push(body, parameters, conditions, measurement_mapping, channel_mapping, body_block) def build_sequence_branch(self, delegator: SequencingElement, @@ -141,15 +144,16 @@ def build_sequence_branch(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: if_block = InstructionBlock() else_block = InstructionBlock() instruction_block.add_instruction_cjmp(self.__trigger, if_block) - sequencer.push(if_branch, parameters, conditions, measurement_mapping, if_block) + sequencer.push(if_branch, parameters, conditions, measurement_mapping, channel_mapping, if_block) instruction_block.add_instruction_goto(else_block) - sequencer.push(else_branch, parameters, conditions, measurement_mapping, else_block) + sequencer.push(else_branch, parameters, conditions, measurement_mapping, channel_mapping, else_block) if_block.return_ip = InstructionPointer(instruction_block, len(instruction_block.instructions)) @@ -197,14 +201,15 @@ def build_sequence_loop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: evaluation_result = self.__callback(self.__loop_iteration) if evaluation_result is None: raise ConditionEvaluationException() if evaluation_result is True: - sequencer.push(delegator, parameters, conditions, measurement_mapping, instruction_block) - sequencer.push(body, parameters, conditions, measurement_mapping, instruction_block) + sequencer.push(delegator, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) + sequencer.push(body, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) self.__loop_iteration += 1 # next time, evaluate for next iteration def build_sequence_branch(self, @@ -215,15 +220,16 @@ def build_sequence_branch(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: evaluation_result = self.__callback(self.__loop_iteration) if evaluation_result is None: raise ConditionEvaluationException() if evaluation_result is True: - sequencer.push(if_branch, parameters, conditions, measurement_mapping, instruction_block) + sequencer.push(if_branch, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) else: - sequencer.push(else_branch, parameters, conditions, measurement_mapping, instruction_block) + sequencer.push(else_branch, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) class ConditionEvaluationException(Exception): diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 9bcf0a234..f105e7f3b 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -17,10 +17,11 @@ from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow -from qctoolkit.pulses.sequence_pulse_template import ParameterNotProvidedException -from qctoolkit.pulses.instructions import Waveform +from qctoolkit.pulses.instructions import SingleChannelWaveform +from qctoolkit.pulses.pulse_template_parameter_mapping import ParameterNotProvidedException +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -__all__ = ["FunctionPulseTemplate", "FunctionWaveform"] +__all__ = ["FunctionPulseTemplate", "FunctionSingleChannelWaveform"] class FunctionPulseTemplate(AtomicPulseTemplate): @@ -38,8 +39,8 @@ class FunctionPulseTemplate(AtomicPulseTemplate): def __init__(self, expression: Union[str, Expression], duration_expression: Union[str, Expression], - measurement: bool=False, - identifier: str=None) -> None: + identifier: str=None, + channel: 'ChannelID' = 'default') -> None: """Create a new FunctionPulseTemplate instance. Args: @@ -60,9 +61,9 @@ def __init__(self, self.__duration_expression = duration_expression if not isinstance(self.__duration_expression, Expression): self.__duration_expression = Expression(self.__duration_expression) - self.__is_measurement_pulse = measurement # type: bool self.__parameter_names = set(self.__duration_expression.variables() + self.__expression.variables()) - set(['t']) + self.__channel = channel @property def parameter_names(self) -> Set[str]: @@ -100,15 +101,17 @@ def is_interruptable(self) -> bool: return False @property - def num_channels(self) -> int: - return 1 - - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[Waveform]: - return FunctionWaveform( - {parameter_name: parameter.get_value() - for (parameter_name, parameter) in parameters.items()}, - self.__expression, - self.__duration_expression + def defined_channels(self) -> Set['ChannelID']: + return set(self.__channel) + + def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[MultiChannelWaveform]: + return MultiChannelWaveform( + {self.__channel: FunctionSingleChannelWaveform( + {parameter_name: parameter.get_value() + for (parameter_name, parameter) in parameters.items()}, + self.__expression, + self.__duration_expression + )} ) def requires_stop(self, @@ -123,24 +126,24 @@ def get_serialization_data(self, serializer: Serializer) -> None: return dict( duration_expression=serializer.dictify(self.__duration_expression), expression=serializer.dictify(self.__expression), - measurement=self.__is_measurement_pulse + channel=self.__channel ) @staticmethod def deserialize(serializer: 'Serializer', expression: str, duration_expression: str, - measurement: bool, + channel: 'ChannelID', identifier: Optional[bool]=None) -> 'FunctionPulseTemplate': return FunctionPulseTemplate( serializer.deserialize(expression), serializer.deserialize(duration_expression), - measurement, + channel=channel, identifier=identifier ) -class FunctionWaveform(Waveform): +class FunctionSingleChannelWaveform(SingleChannelWaveform): """Waveform obtained from instantiating a FunctionPulseTemplate.""" def __init__(self, diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index 76f29b312..ee6042ca0 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -16,20 +16,23 @@ - InstructionPointer: References an instruction's location in a sequence. """ +import itertools from abc import ABCMeta, abstractmethod, abstractproperty -from typing import List, Any, Dict, Iterable, Optional, Tuple +from typing import List, Any, Dict, Iterable, Optional, Tuple, Union import numpy from qctoolkit.comparable import Comparable -__all__ = ["Waveform", "Trigger", +__all__ = ["SingleChannelWaveform", "Trigger", "InstructionPointer", "Instruction", "CJMPInstruction", "EXECInstruction", "GOTOInstruction", "STOPInstruction", "REPJInstruction", "AbstractInstructionBlock", "InstructionBlock", - "ImmutableInstructionBlock", "InstructionSequence" - ] + "ImmutableInstructionBlock", "InstructionSequence", "ChannelID" + ] +ChannelID = Union[str,int] -class Waveform(Comparable, metaclass=ABCMeta): + +class SingleChannelWaveform(Comparable, metaclass=ABCMeta): """Represents an instantiated PulseTemplate which can be sampled to retrieve arrays of voltage values for the hardware.""" @@ -59,10 +62,6 @@ def sample(self, sample_times: numpy.ndarray, first_offset: float=0) -> numpy.nd [ [channel 0 values] [channel 1 values] .... [channel n values] ]. """ - @abstractproperty - def num_channels(self) -> int: - """The number of channels this waveform is defined for.""" - class Trigger(Comparable): """Abstract representation of a hardware trigger for hardware based branching decisions.""" @@ -214,11 +213,11 @@ def __str__(self) -> str: class EXECInstruction(Instruction): """An instruction to execute/play back a waveform.""" - def __init__(self, waveform: Waveform, measurement_windows: List[Tuple[str,List['MeasurementWindow']]] = []) -> None: + def __init__(self, waveform: SingleChannelWaveform, measurement_windows: List[Tuple[str, List['MeasurementWindow']]] = []) -> None: """Create a new EXECInstruction object. Args: - waveform (Waveform): The waveform that will be executed by this instruction. + waveform (SingleChannelWaveform): The waveform that will be executed by this instruction. """ super().__init__() self.waveform = waveform @@ -245,7 +244,29 @@ def compare_key(self) -> Any: def __str__(self) -> str: return "stop" - + + +class CHANInstruction(Instruction): + """Split the control flow for different channels. + + There is no guarantee at this point that the instruction blocks have the same length. This is basically a + switch statement. + """ + + def __init__(self, channel_to_instruction_block: Dict[ChannelID,InstructionPointer]): + self.channel_to_instruction_block = channel_to_instruction_block + + @property + def compare_key(self): + return self.channel_to_instruction_block + + def __str__(self): + return "chan " + ",".join("{target} for {channel}" + .format(target=v,channel=k) for k,v in self.channel_to_instruction_block.items()) + + def __getitem__(self, item) -> InstructionPointer: + return self.channel_to_instruction_block[item] + InstructionSequence = List[Instruction] # pylint: disable=invalid-name,invalid-sequence-index @@ -303,7 +324,10 @@ def __iter__(self) -> Iterable[Instruction]: else: yield GOTOInstruction(self.return_ip) - def __getitem__(self, index: int) -> Instruction: + def __getitem__(self, index: Union[int,slice]) -> Union[Instruction,Iterable[Instruction]]: + if isinstance(index, slice): + return (self[i] for i in range(*index.indices(len(self)))) + if index > len(self.instructions) or index < -(len(self.instructions) + 1): raise IndexError() if index < 0: @@ -355,14 +379,14 @@ def add_instruction(self, instruction: Instruction) -> None: """ self.__instruction_list.append(instruction) - def add_instruction_exec(self, waveform: Waveform, measurement_windows: List[Tuple[str,List['MeasurementWindows']]] = None) -> None: + def add_instruction_exec(self, waveform: 'MultiChannelWaveform', measurement_windows: List[Tuple[str, List['MeasurementWindows']]] = None) -> None: """Create and append a new EXECInstruction object for the given waveform at the end of this instruction block. Args: - waveform (Waveform): The Waveform object referenced by the new EXECInstruction. + waveform (SingleChannelWaveform): The Waveform object referenced by the new EXECInstruction. """ - self.add_instruction(EXECInstruction(waveform,measurement_windows)) + self.add_instruction(EXECInstruction(waveform, measurement_windows)) def add_instruction_goto(self, target_block: 'InstructionBlock') -> None: """Create and append a new GOTOInstruction object with a given target block at the end of @@ -405,6 +429,10 @@ def add_instruction_stop(self) -> None: """Create and append a new STOPInstruction object at the end of this instruction block.""" self.add_instruction(STOPInstruction()) + def add_instruction_chan(self, channel_to_instruction: Dict[ChannelID,'InstructionBlock'] ): + """Create and append a new CHANInstruction at the end of this instruction block.""" + self.add_instruction(CHANInstruction({ch: InstructionPointer(block) for ch, block in channel_to_instruction.items()})) + @property def instructions(self) -> InstructionSequence: return self.__instruction_list.copy() diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index 8186127e5..721dd94e2 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -7,7 +7,7 @@ from qctoolkit.serialization import Serializer from qctoolkit.pulses.parameters import Parameter -from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow +from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.conditions import Condition, ConditionMissingException from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.pulses.sequencing import Sequencer @@ -62,8 +62,8 @@ def is_interruptable(self) -> bool: return self.__body.is_interruptable @property - def num_channels(self) -> int: - return self.__body.num_channels + def defined_channels(self) -> Set['ChannelID']: + return self.__body.defined_channels @property def measurement_names(self) -> Set[str]: @@ -80,6 +80,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: self.__obtain_condition_object(conditions).build_sequence_loop(self, self.__body, @@ -87,6 +88,7 @@ def build_sequence(self, parameters, conditions, measurement_mapping, + channel_mapping, instruction_block) def requires_stop(self, diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 9b33c5033..b8bbc5a6a 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -13,18 +13,19 @@ from qctoolkit.serialization import Serializer -from qctoolkit.pulses.instructions import Waveform -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, SubTemplate -from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, \ - MissingMappingException, get_measurement_name_mappings +from qctoolkit.pulses.instructions import InstructionBlock, SingleChannelWaveform, InstructionPointer +from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingTemplate,\ + MissingParameterDeclarationException, ChannelID from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ ParameterNotProvidedException from qctoolkit.pulses.conditions import Condition +from qctoolkit.comparable import Comparable __all__ = ["MultiChannelWaveform", "MultiChannelPulseTemplate"] -class MultiChannelWaveform(Waveform): +class MultiChannelWaveform(Comparable): """A MultiChannelWaveform is a Waveform object that allows combining arbitrary Waveform objects to into a single waveform defined for several channels. @@ -43,7 +44,7 @@ class MultiChannelWaveform(Waveform): assigned more than one channel of any Waveform object it consists of """ - def __init__(self, subwaveforms: List[Tuple[Waveform, List[int]]]) -> None: + def __init__(self, subwaveforms: Dict[ChannelID, SingleChannelWaveform]) -> None: """Create a new MultiChannelWaveform instance. Requires a list of subwaveforms in the form (Waveform, List(int)) where the list defines @@ -67,55 +68,53 @@ def __init__(self, subwaveforms: List[Tuple[Waveform, List[int]]]) -> None: ) self.__channel_waveforms = subwaveforms - num_channels = self.num_channels - duration = subwaveforms[0][0].duration - assigned_channels = set() - for waveform, channel_mapping in self.__channel_waveforms: - if waveform.duration != duration: - raise ValueError( - "MultiChannelWaveform cannot be constructed from channel waveforms of different" - "lengths." - ) - # ensure that channel mappings stay within bounds - out_of_bounds_channel = [channel for channel in channel_mapping - if channel >= num_channels or channel < 0] - if out_of_bounds_channel: - raise ValueError( - "The channel mapping {}, assigned to a channel waveform, is not valid (must be " - "greater than 0 and less than {}).".format(out_of_bounds_channel.pop(), - num_channels) - ) - # ensure that only a single waveform is mapped to each channel - for channel in channel_mapping: - if channel in assigned_channels: - raise ValueError("The channel {} has multiple channel waveform assignments" - .format(channel)) - else: - assigned_channels.add(channel) - + duration = next(iter(self.__channel_waveforms.values())).duration + if not all(waveform.duration == duration for waveform in self.__channel_waveforms.values()): + raise ValueError( + "MultiChannelWaveform cannot be constructed from channel waveforms of different" + "lengths." + ) @property def duration(self) -> float: - return self.__channel_waveforms[0][0].duration - - def sample(self, sample_times: numpy.ndarray, first_offset: float=0) -> numpy.ndarray: - voltages_transposed = numpy.empty((self.num_channels, len(sample_times))) - for waveform, channel_mapping in self.__channel_waveforms: - waveform_voltages_transposed = waveform.sample(sample_times, first_offset) - for old_c, new_c in enumerate(channel_mapping): - voltages_transposed[new_c] = waveform_voltages_transposed[old_c] - return voltages_transposed + return next(iter(self.__channel_waveforms.values())).duration + + def __getitem__(self, key: Union[Set[ChannelID], ChannelID]) -> Union[SingleChannelWaveform, 'MultiChannelWaveform']: + try: + if not isinstance(key, set): + return self.__channel_waveforms[key] + else: + return MultiChannelWaveform( dict( (chID, self.__channel_waveforms[chID]) for chID in key ) ) + except KeyError as err: + raise KeyError('Unknown channel ID: {}'.format(err.args[0]),*err.args) + + def __add__(self, other: 'MultiChannelWaveform') -> 'MultiChannelWaveform': + if set(self.__channel_waveforms) | set(other.__channel_waveforms): + raise ChannelMappingException(set(self.__channel_waveforms) | set(other.__channel_waveforms)) + return MultiChannelWaveform(dict(**self.__channel_waveforms,**other.__channel_waveforms)) + + def __iadd__(self, other: 'MultiChannelWaveform'): + if set(self.__channel_waveforms) | set(other.__channel_waveforms): + raise ChannelMappingException(set(self.__channel_waveforms) | set(other.__channel_waveforms)) + if self.duration != other.duration: + raise ValueError('Waveforms not combinable as they have different durations.') + self.__channel_waveforms = dict(**self.__channel_waveforms,**other.__channel_waveforms) @property - def num_channels(self) -> int: - return sum([waveform.num_channels for waveform, _ in self.__channel_waveforms]) + def defined_channels(self) -> Set[ChannelID]: + return set(self.__channel_waveforms.keys()) @property def compare_key(self) -> Any: return self.__channel_waveforms + def get_remapped(self, channel_mapping): + return MultiChannelWaveform( + {channel_mapping[old_channel]: waveform for old_channel, waveform in self.__channel_waveforms.items()} + ) -class MultiChannelPulseTemplate(AtomicPulseTemplate): + +class MultiChannelPulseTemplate(PulseTemplate): """A multi-channel group of several AtomicPulseTemplate objects. While SequencePulseTemplate combines several subtemplates (with an identical number of channels) @@ -144,10 +143,10 @@ class MultiChannelPulseTemplate(AtomicPulseTemplate): - MultiChannelWaveform """ - SimpleSubTemplate = Tuple[AtomicPulseTemplate, Dict[str, str], Dict[str,str], List[int]] + SimpleSubTemplate = Tuple[PulseTemplate, Dict[str, str], Dict[ChannelID, ChannelID]] def __init__(self, - subtemplates: Iterable[Union[SubTemplate,SimpleSubTemplate]], + subtemplates: Iterable[Union[PulseTemplate, SimpleSubTemplate]], external_parameters: Set[str], identifier: str=None) -> None: """Creates a new MultiChannelPulseTemplate instance. @@ -178,120 +177,97 @@ def __init__(self, MissingParameterDeclarationException, if a parameter mapping requires a parameter that was not declared in the external parameters of this MultiChannelPulseTemplate. """ - super().__init__(identifier=identifier) - - subtemplates = [st if isinstance(st,SubTemplate) else SubTemplate(st[0],st[1],channel_mapping=st[2]) for st in subtemplates ] - - self.__parameter_mapping = PulseTemplateParameterMapping(external_parameters) - self.__subtemplates = [(st.template, st.channel_mapping) for st in subtemplates] - self.__measurement_window_mappings = get_measurement_name_mappings(subtemplates) - - assigned_channels = set() - num_channels = self.num_channels - for template, mapping_functions, _, channel_mapping in subtemplates: - # Consistency checks - for parameter, mapping_function in mapping_functions.items(): - self.__parameter_mapping.add(template, parameter, mapping_function) - - remaining = self.__parameter_mapping.get_remaining_mappings(template) - if remaining: - raise MissingMappingException(template, - remaining.pop()) - - - # ensure that channel mappings stay within bounds - out_of_bounds_channel = [channel for channel in channel_mapping - if channel >= num_channels or channel < 0] - if out_of_bounds_channel: - raise ValueError( - "The channel mapping {}, assigned to a channel waveform, is not valid (must be " - "greater than 0 and less than {}).".format(out_of_bounds_channel.pop(), - num_channels) - ) - # ensure that only a single waveform is mapped to each channel - for channel in channel_mapping: - if channel in assigned_channels: - raise ValueError("The channel {} has multiple channel waveform assignments" - .format(channel)) - else: - assigned_channels.add(channel) + super().__init__(identifier) + + def to_mapping_template(template, parameter_mapping, channel_mapping): + if not (isinstance(channel_mapping, dict) and all( + isinstance(ch1,(int,str)) and isinstance(ch2,(int,str)) for ch1, ch2 in channel_mapping.items())): + raise ValueError('{} is not a valid channel mapping.'.format(channel_mapping)) + return MappingTemplate(template, parameter_mapping, channel_mapping=channel_mapping) + self.__subtemplates = [st if isinstance(st, PulseTemplate) else to_mapping_template(*st) for st in subtemplates] + + defined_channels = [st.defined_channels for st in self.__subtemplates] + # check there are no intersections between channels + for i, chans1 in enumerate(defined_channels): + for j, chans2 in enumerate(defined_channels[i+1:]): + if chans1 & chans2: + raise ChannelMappingException(self.__subtemplates[i], + self.__subtemplates[i+1+j], + (chans1 | chans2).pop()) + + remaining = external_parameters.copy() + for subtemplate in self.__subtemplates: + missing = subtemplate.parameter_names - external_parameters + if missing: + raise MissingParameterDeclarationException(subtemplate.template, missing.pop()) + remaining -= subtemplate.parameter_names + if remaining: + raise MissingMappingException(subtemplate.template, remaining.pop()) @property def parameter_names(self) -> Set[str]: - return self.__parameter_mapping.external_parameters + return set.union(*(st.parameter_names for st in self.__subtemplates)) @property def parameter_declarations(self) -> Set[ParameterDeclaration]: # TODO: min, max, default values not mapped (required?) - return {ParameterDeclaration(parameter_name) for parameter_name in self.parameter_names} + return {ParameterDeclaration(name) for name in self.parameter_names} - def get_measurement_windows(self, parameters: Dict[str, Parameter]) -> List['MeasurementWindow']: - mapped_window = lambda window : (self.__measurement_window_mappings[window[0]],window[1],window[2]) if \ - window[0] in self.__measurement_window_mappings else window - return [ mapped_window(window) for st, _ in self.__subtemplates for window in st.get_measurement_windows(parameters) ] + @property + def subtemplates(self) -> Iterable[MappingTemplate]: + return iter(self.__subtemplates) @property def is_interruptable(self) -> bool: - return all(t.is_interruptable for t, _ in self.__subtemplates) + return all(st.is_interruptable for st in self.subtemplates) @property - def num_channels(self) -> int: - return sum(t.num_channels for t, _ in self.__subtemplates) + def defined_channels(self) -> Set[ChannelID]: + return set.union(*(st.defined_channels for st in self.__subtemplates)) @property def measurement_names(self) -> Set[str]: - return {measurement_mapping[name] if name in measurement_mapping else name - for template, measurement_mapping in zip(self.__subtemplates, self.__measurement_window_mappings) - for name in template[0].measurement_names} + return set.union(*(st.measurement_names for st in self.__subtemplates)) + + def build_sequence(self, + sequencer: 'Sequencer', + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], + instruction_block: InstructionBlock) -> None: + channel_to_instruction_block = dict() + for subtemplate in self.subtemplates: + block = InstructionBlock() + sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, block) + channel_to_instruction_block.update(dict.fromkeys(subtemplate.defined_channels, block)) + instruction_block.add_instruction_chan(channel_to_instruction=channel_to_instruction_block) def requires_stop(self, parameters: Dict[str, Parameter], - conditions: Dict[str, Condition]) -> bool: - return any(t.requires_stop(self.__parameter_mapping.map_parameters(t, parameters), - conditions) - for t, _ in self.__subtemplates) - - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[Waveform]: - missing = self.parameter_names - parameters.keys() - if missing: - raise ParameterNotProvidedException(missing.pop()) - - channel_waveforms = [] - for template, channel_mapping in self.__subtemplates: - inner_parameters = self.__parameter_mapping.map_parameters(template, parameters) - waveform = template.build_waveform(inner_parameters) - channel_waveforms.append((waveform, channel_mapping)) - waveform = MultiChannelWaveform(channel_waveforms) - return waveform + conditions: Dict[str, 'Condition']) -> bool: + return any(st.requires_stop(parameters, conditions) for st in self.__subtemplates) + def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - subtemplates = [] - for subtemplate, channel_mapping, measurement_mapping in zip(*zip(*self.__subtemplates) ,self.__measurement_window_mappings): - mapping_functions = self.__parameter_mapping.get_template_map(subtemplate) - mapping_function_strings = \ - {k: serializer.dictify(m) for k, m in mapping_functions.items()} - subtemplate = serializer.dictify(subtemplate) - subtemplates.append(dict(template=subtemplate, - parameter_mappings=mapping_function_strings, - measurement_mapping=measurement_mapping, - channel_mappings=channel_mapping)) - return dict(subtemplates=subtemplates, - external_parameters=sorted(list(self.parameter_names))) + data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], + type=serializer.get_type_identifier(self)) + return data @staticmethod def deserialize(serializer: Serializer, subtemplates: Iterable[Dict[str, Any]], - external_parameters: Iterable[str], identifier: Optional[str]=None) -> 'MultiChannelPulseTemplate': - subtemplates = \ - [SubTemplate( - serializer.deserialize(subt['template']), - {k: str(serializer.deserialize(m)) for k, m in subt['parameter_mappings'].items()}, - measurement_mapping=subt['measurement_mapping'], - channel_mapping=subt['channel_mappings']) - for subt in subtemplates] - - template = MultiChannelPulseTemplate(subtemplates, - external_parameters, - identifier=identifier) - return template \ No newline at end of file + subtemplates = [serializer.deserialize(st) for st in subtemplates] + external_parameters = set.union(*(st.parameter_names for st in subtemplates)) + mul_template = MultiChannelPulseTemplate(subtemplates, external_parameters, identifier=identifier) + return mul_template + + +class ChannelMappingException(Exception): + def __init__(self, obj1, obj2, intersect_set): + self.intersect_set = intersect_set + self.obj1 = obj1 + self.obj2 = obj2 + def __str__(self): + return 'Channels {chs} defined in {} and {}'.format(self.intersect_set, self.obj1, self.obj2) \ No newline at end of file diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index c9775c9e2..f58d3c68a 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -7,16 +7,19 @@ directly translated into a waveform. """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict, List, Tuple, Set, Optional, NamedTuple +from typing import Dict, List, Tuple, Set, Optional, Union +import itertools + +MeasurementWindow = Tuple[str, float, float] +ChannelID = Union[str,int] from qctoolkit.serialization import Serializable from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.sequencing import SequencingElement, InstructionBlock -__all__ = ["MeasurementWindow", "PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException"] +__all__ = ["MeasurementWindow", "PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException", "ChannelID"] -MeasurementWindow = Tuple[str, float, float] class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): @@ -53,30 +56,27 @@ def is_interruptable(self) -> bool: """ @abstractproperty - def num_channels(self) -> int: + def defined_channels(self) -> Set['ChannelID']: """Returns the number of hardware output channels this PulseTemplate defines.""" - - def __matmul__(self, other) -> 'SequencePulseTemplate': + def __matmul__(self, other: 'PulseTemplate') -> 'SequencePulseTemplate': """This method enables us to use the @-operator (intended for matrix multiplication) for - concatenating pulses""" + concatenating pulses. If one of the pulses is a SequencePulseTemplate the other pulse gets merged into it""" + from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate - # check if parameter names of the subpulses clash, otherwise construct a default mapping - double_parameters = self.parameter_names & other.parameter_names # intersection - if double_parameters: - # if there are parameter name conflicts, throw an exception - raise DoubleParameterNameException(self, other, double_parameters) - else: - subtemplates = [(self, {p:p for p in self.parameter_names}, {}), - (other, {p:p for p in other.parameter_names}, {})] - external_parameters = self.parameter_names | other.parameter_names # union - return SequencePulseTemplate(subtemplates, external_parameters) - -SubTemplate = NamedTuple('SubTemplate',[('template',PulseTemplate), - ('parameter_mapping',Dict[str,str]), - ('measurement_mapping',Optional[Dict[str,str]]), - ('channel_mapping',Optional[List[int]])] ) -SubTemplate.__new__.__defaults__ = (dict(),None) + + if self != other: + # check if parameter names of the subpulses intersect and raise an exception if so + double_parameters = self.parameter_names & other.parameter_names + if double_parameters: + raise DoubleParameterNameException(self, other, double_parameters) + + external_parameters = self.parameter_names | other.parameter_names + subtemplates = itertools.chain(self.subtemplates if isinstance(self, SequencePulseTemplate) else [self], + other.subtemplates if isinstance(other, SequencePulseTemplate) else [other]) + return SequencePulseTemplate(subtemplates, external_parameters) + + class AtomicPulseTemplate(PulseTemplate): """A PulseTemplate that does not imply any control flow disruptions and can be directly @@ -92,7 +92,7 @@ def is_interruptable(self) -> bool: return False @abstractmethod - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['Waveform']: + def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['MultiChannelWaveform']: """Translate this AtomicPulseTemplate into a waveform according to the given parameteres. Args: @@ -114,6 +114,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: waveform = self.build_waveform(parameters) if waveform: @@ -121,7 +122,7 @@ def build_sequence(self, meas_windows = [(measurement_mapping[name],begin,end) for name, begin, end in meas_windows] - instruction_block.add_instruction_exec(waveform,meas_windows) + instruction_block.add_instruction_exec(waveform.get_remapped(channel_mapping), meas_windows) diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index 0c066ac28..a1681a1fd 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -1,173 +1,147 @@ """This module defines PulseTemplateParameterMapping, a helper class for pulse templates that offer mapping of parameters of subtemplates.""" -from typing import Optional, Set, Dict, Union, Iterable, List +from typing import Optional, Set, Dict, Union, Iterable, List, Any +import itertools from qctoolkit.expressions import Expression -from qctoolkit.pulses.pulse_template import PulseTemplate, SubTemplate -from qctoolkit.pulses.parameters import Parameter, MappedParameter, ParameterNotProvidedException +from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration, MappedParameter, ParameterNotProvidedException +from qctoolkit.pulses.instructions import ChannelID __all__ = [ + "MappingTemplate", "MissingMappingException", "MissingParameterDeclarationException", "UnnecessaryMappingException", - "PulseTemplateParameterMapping", - "get_measurement_name_mappings" ] -class PulseTemplateParameterMapping: - """A mapping of parameters of a set of pulse templates to a fixed set of so-called external - parameters. - - A helper class used by templates that offer parameter mappings of subtemplates. Automatically - performs sanity checks when a new pulse template parameter mapping is added: - - Do all mapping expression only rely on external parameters as variables? - and offers functionality to query whether all parameters declared by a pulse tempalte are - mapped. - - See Also: - - SequencePulseTemplate - - MultiChannelPulseTemplate - """ - - def __init__(self, - external_parameters: Optional[Set[str]]=None) -> None: - """Create a new PulseTemplateParameterMapping instance. - - Args: - external_parameters (Set(str)): A set of names of external parameters (optional). - """ - super().__init__() - self.__map = dict() - self.__external_parameters = set() - self.set_external_parameters(external_parameters) - - def set_external_parameters(self, external_parameters: Optional[Set[str]]) -> None: - """Sets the set of external parameters names. - - Args: - external_parameters (Set(str)): A set of names of external parameters. Might be None, - which results in no changes. - """ - if external_parameters is not None: - self.__external_parameters = set(external_parameters.copy()) +class MappingTemplate(PulseTemplate): + def __init__(self, template: PulseTemplate, + parameter_mapping: Dict[str, str], + measurement_mapping: Dict[str, str] = dict(), + channel_mapping: Dict[ChannelID, ChannelID] = dict()): + super().__init__(None) + + mapped_internal_parameters = set(parameter_mapping.keys()) + internal_parameters = template.parameter_names + if mapped_internal_parameters - internal_parameters: + raise UnnecessaryMappingException(template, mapped_internal_parameters - internal_parameters) + elif internal_parameters - mapped_internal_parameters: + raise MissingMappingException(template,internal_parameters - mapped_internal_parameters) + parameter_mapping = dict((k, Expression(v)) for k, v in parameter_mapping.items()) + + internal_names = template.measurement_names + mapped_internal_names = set(measurement_mapping.keys()) + if mapped_internal_names - internal_names: + raise UnnecessaryMappingException(template, mapped_internal_names - internal_names) + missing_name_mappings = internal_names - mapped_internal_names + + internal_channels = template.defined_channels + mapped_internal_channels = set(channel_mapping.keys()) + if mapped_internal_channels - internal_channels: + raise UnnecessaryMappingException(template,mapped_internal_channels - internal_channels) + missing_channel_mappings = internal_channels - mapped_internal_channels + + self.__template = template + self.__parameter_mapping = parameter_mapping + self.__external_parameters = set(itertools.chain(*(expr.variables() for expr in self.__parameter_mapping.values()))) + self.__measurement_mapping = dict(((name,name) for name in missing_name_mappings), **measurement_mapping) + self.__channel_mapping = dict(((name,name) for name in missing_channel_mappings), **channel_mapping) @property - def external_parameters(self) -> Set[str]: - """The (names of the) external parameters.""" - return self.__external_parameters.copy() - - def __get_template_map(self, template: PulseTemplate) -> Dict[str, Expression]: - # internal helper function - if template not in self.__map: - return dict() - return self.__map[template] - - def add(self, - template: PulseTemplate, - parameter: str, - mapping_expression: Union[str, Expression]): - """Add a new mapping for a parameter of a pulse template. + def template(self): + return self.__template - Args: - template (PulseTemplate): The pulse template for which a parameter mapping will be - added. - parameter (str): The name of the parameter of the pulse template that will be mapped. - mapping_expression (str or Expression): The mathematical expression that specifies the - mapping from external parameters to the parameter of the pulse template. - Raises: - UnnecessaryMappingException, if parameter is not declared by template. - MissingParameterDeclarationException, if mapping_expression requires a variable that - is not a parameter in the external parameters of this PulseTemplateParameterMapping. - """ - if parameter not in template.parameter_names: - raise UnnecessaryMappingException(template, parameter) - - if isinstance(mapping_expression, str): - mapping_expression = Expression(mapping_expression) - required_externals = set(mapping_expression.variables()) - non_declared_externals = required_externals - self.__external_parameters - if non_declared_externals: - raise MissingParameterDeclarationException(template, - non_declared_externals.pop()) + @property + def measurement_mapping(self): + return self.__measurement_mapping - template_map = self.__get_template_map(template) - template_map[parameter] = mapping_expression - self.__map[template] = template_map + @property + def parameter_names(self) -> Set[str]: + return self.__external_parameters - def get_template_map(self, template: PulseTemplate) -> Dict[str, Expression]: - """Return all parameter mappings defined for a given pulse template. + @property + def parameter_declarations(self) -> Set[ParameterDeclaration]: + # TODO: min, max, default values not mapped (required?) + return {ParameterDeclaration(name) for name in self.parameter_names} - Args: - template (PulseTemplate): The pulse template for which to query the mapping. - Returns: - A dictionary of the form template_parameter -> mapping_expression for all mappings - given for template. - """ - return self.__get_template_map(template).copy() + @property + def measurement_names(self) -> Set[str]: + return set(self.__measurement_mapping.values()) - def is_template_mapped(self, template: PulseTemplate) -> bool: - """Query whether a complete parameter mapping is defined for a given pulse template. + @property + def is_interruptable(self) -> bool: + return self.template.is_interruptable - Args: - template (PulseTemplate): The pulse template for which to query the existence of - mappings. - Returns: - True, if all parameters of template are mapped to external parameters. - """ - return len(self.get_remaining_mappings(template)) == 0 + @property + def defined_channels(self) -> Set[ChannelID]: + return {self.__channel_mapping[k] for k in self.template.defined_channels} - def get_remaining_mappings(self, template: PulseTemplate) -> Set[str]: - """Query all currently unmapped parameters of a given pulse template. + def get_serialization_data(self, serializer: 'Serializer') -> Dict[str,Any]: + return dict(template=serializer.dictify(self.template), + parameter_mapping=self.__parameter_mapping, + measurement_mapping=self.__measurement_mapping, + channel_mapping=self.__channel_mapping) - Args: - template (PulseTemplate): The pulse template for which to query the unmapped parameters. - Returns: - A set of parameter names for which no mappings are defined. - """ - template_map = self.__get_template_map(template) - return template.parameter_names - template_map.keys() + @staticmethod + def deserialize(serializer: 'Serializer', + template: Union[str, Dict[str, Any]], + identifier: Optional[str]=None, **kwargs) -> 'MappingTemplate': + return MappingTemplate(template=serializer.deserialize(template), **kwargs) def map_parameters(self, - template: PulseTemplate, parameters: Dict[str, Parameter]) -> Dict[str, Parameter]: - """Map parameter values according to the defined mappings for a given pulse template. + """Map parameter values according to the defined mappings. Args: - template (PulseTemplate): The pulse template for which to map the parameter values. parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects/values. Returns: A new dictionary which maps parameter names to parameter values which have been mapped according to the mappings defined for template. """ - missing = self.__external_parameters - set(parameters.keys()) + missing = set(self.__external_parameters) - set(parameters.keys()) if missing: raise ParameterNotProvidedException(missing.pop()) - template_map = self.__get_template_map(template) inner_parameters = { parameter: MappedParameter( mapping_function, {name: parameters[name] for name in mapping_function.variables()} ) - for (parameter, mapping_function) in template_map.items() + for (parameter, mapping_function) in self.__parameter_mapping.items() } return inner_parameters - -def get_measurement_name_mappings(subtemplates: Iterable[SubTemplate]) -> List[Dict[str,str]]: - mappings = [template.measurement_mapping.copy() for template in subtemplates] - for subtemplate, measurement_mapping in zip(subtemplates,mappings): - internal_names = subtemplate.template.measurement_names - mapped_names = set(measurement_mapping.keys()) - if mapped_names - internal_names: - raise UnnecessaryMappingException(subtemplate.template,mapped_names - internal_names) - # add the missing identity mappings - for unmapped_name in internal_names - mapped_names: - measurement_mapping[unmapped_name] = unmapped_name - return mappings + def get_updated_measurement_mapping(self, measurement_mapping: Dict[str,str]): + return {k: measurement_mapping[v] for k, v in self.__measurement_mapping.items()} + + def get_updated_channel_mapping(self, channel_mapping: Dict[ChannelID, ChannelID]): + return {inner_ch: channel_mapping[outer_ch] for inner_ch, outer_ch in self.__channel_mapping.items()} + + def build_sequence(self, + sequencer: "Sequencer", + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID], + instruction_block: 'InstructionBlock') -> None: + self.template.build_sequence(sequencer, + parameters=self.map_parameters(parameters), + conditions=conditions, + measurement_mapping=self.get_updated_measurement_mapping(measurement_mapping), + channel_mapping=channel_mapping, + instruction_block=instruction_block) + + def requires_stop(self, + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition']) -> bool: + return self.template.requires_stop( + self.map_parameters(parameters), + conditions + ) class MissingParameterDeclarationException(Exception): diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index 7f1c3f744..8f32661a4 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -65,8 +65,8 @@ def is_interruptable(self) -> bool: return self.__body.is_interruptable @property - def num_channels(self) -> int: - return self.__body.num_channels + def defined_channels(self) -> Set['ChannelID']: + return self.__body.defined_channels @property def measurement_names(self) -> Set[str]: @@ -77,6 +77,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: repetition_count = self.__repetition_count if isinstance(repetition_count, ParameterDeclaration): @@ -88,7 +89,7 @@ def build_sequence(self, body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) instruction_block.add_instruction_repj(int(repetition_count), body_block) - sequencer.push(self.body, parameters, conditions, measurement_mapping, body_block) + sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) def requires_stop(self, parameters: Dict[str, Parameter], diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index cf523402b..643041864 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -6,14 +6,12 @@ from qctoolkit.serialization import Serializer -from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow, \ - DoubleParameterNameException, SubTemplate -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ - ParameterNotProvidedException +from qctoolkit.pulses.pulse_template import PulseTemplate, DoubleParameterNameException +from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.sequencing import InstructionBlock, Sequencer from qctoolkit.pulses.conditions import Condition -from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, \ - MissingMappingException, get_measurement_name_mappings +from qctoolkit.pulses.pulse_template_parameter_mapping import \ + MissingMappingException, MappingTemplate, ChannelID, MissingParameterDeclarationException __all__ = ["SequencePulseTemplate"] @@ -35,8 +33,8 @@ class SequencePulseTemplate(PulseTemplate): SimpleSubTemplate = Tuple[PulseTemplate, Dict[str, str]] # pylint: disable=invalid-name def __init__(self, - subtemplates: Iterable[Union[SimpleSubTemplate,SubTemplate]], - external_parameters: List[str], # pylint: disable=invalid-sequence-index + subtemplates: Iterable[Union[SimpleSubTemplate, MappingTemplate]], + external_parameters: Union[Iterable[str], Set[str]], # pylint: disable=invalid-sequence-index identifier: Optional[str]=None) -> None: """Create a new SequencePulseTemplate instance. @@ -66,37 +64,27 @@ def __init__(self, """ super().__init__(identifier) - num_channels = 0 - if subtemplates: - num_channels = subtemplates[0][0].num_channels + self.__subtemplates = [st if not isinstance(st, tuple) else MappingTemplate(*st) for st in subtemplates] + external_parameters = external_parameters if isinstance(external_parameters,set) else set(external_parameters) - subtemplates = [ st if isinstance(st,SubTemplate) else SubTemplate(*st) for st in subtemplates ] + # check that all subtempaltes live on the same channels + defined_channels = self.__subtemplates[0].defined_channels + for subtemplate in self.__subtemplates[1:]: + if subtemplate.defined_channels != defined_channels: + raise ValueError('The subtemplates are defined for different channels') - self.__parameter_mapping = PulseTemplateParameterMapping(external_parameters) - - for template, parameter_mapping, _, channel_mapping in subtemplates: - if channel_mapping: - raise ValueError('Channel mapping not allowed (yet) in SequencePulseTemplate') - - # Consistency checks - if template.num_channels != num_channels: - raise ValueError("Subtemplates have different number of channels!") - - for parameter, parameter_mapping_function in parameter_mapping.items(): - self.__parameter_mapping.add(template, parameter, parameter_mapping_function) - - remaining = self.__parameter_mapping.get_remaining_mappings(template) - if remaining: - raise MissingMappingException(template, - remaining.pop()) - - self.__measurement_window_mappings = get_measurement_name_mappings(subtemplates) - self.__subtemplates = [st.template for st in subtemplates] - self.__is_interruptable = True + remaining = external_parameters.copy() + for subtemplate in self.__subtemplates: + missing = subtemplate.parameter_names - external_parameters + if missing: + raise MissingParameterDeclarationException(subtemplate.template,missing.pop()) + remaining = remaining - subtemplate.parameter_names + if remaining: + MissingMappingException(subtemplate.template,remaining.pop()) @property def parameter_names(self) -> Set[str]: - return self.__parameter_mapping.external_parameters + return set.union(*(st.parameter_names for st in self.__subtemplates)) @property def parameter_declarations(self) -> Set[ParameterDeclaration]: @@ -104,107 +92,53 @@ def parameter_declarations(self) -> Set[ParameterDeclaration]: return {ParameterDeclaration(name) for name in self.parameter_names} @property - def subtemplates(self) -> List[SubTemplate]: - return [SubTemplate(template, - self.__parameter_mapping.get_template_map(template), - measurement_mapping=name_mapping) - for template, name_mapping in zip(self.__subtemplates, self.__measurement_window_mappings)] + def subtemplates(self) -> List[MappingTemplate]: + return self.__subtemplates @property def is_interruptable(self) -> bool: - return self.__is_interruptable - - @is_interruptable.setter - def is_interruptable(self, new_value: bool) -> None: - self.__is_interruptable = new_value + return any(st.is_interruptable for st in self.subtemplates) @property - def num_channels(self) -> int: - return self.__subtemplates[0].num_channels + def defined_channels(self) -> Set[ChannelID]: + return self.__subtemplates[0].defined_channels if self.__subtemplates else set() @property def measurement_names(self) -> Set[str]: - return set.union(*(set(measurement_mapping.values()) - for measurement_mapping in self.__measurement_window_mappings)) + return set.union(*(st.measurement_names for st in self.subtemplates)) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: - return False + """Returns the stop requirement of the first subtemplate. If a later subtemplate requires a stop the + SequencePulseTemplate can be partially sequenced.""" + return self.__subtemplates[0].requires_stop(parameters,conditions) if self.__subtemplates else False def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping : Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: # todo: currently ignores is_interruptable - # detect missing or unnecessary parameters - missing = self.parameter_names - parameters.keys() - if missing: - raise ParameterNotProvidedException(missing.pop()) - - def concatenate_dicts(d1, d2): - return dict(((key, d1[value]) for key, value in d2.items())) - - # push subtemplates to sequencing stack with mapped parameters - for template, local_window_mapping in zip(reversed(self.__subtemplates),reversed(self.__measurement_window_mappings)): - inner_parameters = self.__parameter_mapping.map_parameters(template, parameters) - inner_names = concatenate_dicts(measurement_mapping, local_window_mapping) - sequencer.push(template, inner_parameters, conditions, - inner_names, instruction_block) + for subtemplate in reversed(self.subtemplates): + sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict() - data['external_parameters'] = sorted(list(self.parameter_names)) - data['is_interruptable'] = self.is_interruptable - - subtemplates = [] - for subtemplate,window_name_mappings in zip(self.__subtemplates,self.__measurement_window_mappings): - mapping_functions = self.__parameter_mapping.get_template_map(subtemplate) - mapping_functions_strings = \ - {k: serializer.dictify(m) for k, m in mapping_functions.items()} - subtemplate = serializer.dictify(subtemplate) - subtemplates.append(dict(template=subtemplate, parameter_mappings=mapping_functions_strings, - measurement_mappings=window_name_mappings)) - data['subtemplates'] = subtemplates + data['subtemplates'] = [serializer.dictify(subtemplate) for subtemplate in self.subtemplates] data['type'] = serializer.get_type_identifier(self) + return data @staticmethod def deserialize(serializer: Serializer, - is_interruptable: bool, - subtemplates: Iterable[Dict[str, Union[str, Dict[str, Any]]]], - external_parameters: Iterable[str], + subtemplates: Iterable[Dict[str, Any]], identifier: Optional[str]=None) -> 'SequencePulseTemplate': - subtemplates = \ - [SubTemplate( - serializer.deserialize(d['template']), - {k: str(serializer.deserialize(m)) for k, m in d['parameter_mappings'].items()}, - measurement_mapping=d['measurement_mappings'] - ) for d in subtemplates] - - template = SequencePulseTemplate(subtemplates, external_parameters, identifier=identifier) - template.is_interruptable = is_interruptable - return template - - def __matmult__(self, other) -> 'SequencePulseTemplate': - """Like in the general PulseTemplate implementation this method enables using the - @-operator for concatenating pulses. We need an overloaded method for SequencePulseTemplate - to avoid creating unnecessarily nested pulse structures.""" - if not type(other) == SequencePulseTemplate: - return SequencePulseTemplate.__matmult__(self, other) - else: - # this section is copy-pasted from the PulseTemplate implementation - double_parameters = self.parameter_names & other.parameter_names # intersection - if double_parameters: - raise DoubleParameterNameException(self, other, double_parameters) - else: - # this branch differs from what happens in PulseTemplate - subtemplates = self.subtemplates + other.subtemplates - # the check for conflicting external parameters has already been carried out - external_parameters = self.parameter_names | other.parameter_names # union - return SequencePulseTemplate(subtemplates, external_parameters) # no identifier - + subtemplates = [serializer.deserialize(st) for st in subtemplates] + external_parameters = set.union( *(st.parameter_names for st in subtemplates) ) + seq_template = SequencePulseTemplate(subtemplates, external_parameters, identifier=identifier) + return seq_template diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index ebc194a1e..700d2262d 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -34,6 +34,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: """Translate this SequencingElement into an instruction sequence for the given instruction_block using sequencer and the given parameter and condition sets. @@ -110,7 +111,8 @@ def push(self, sequencing_element: SequencingElement, parameters: Optional[Dict[str, Union[Parameter, float]]]=None, conditions: Optional[Dict[str, 'Condition']]=None, - window_mapping: Optional[Dict[str,str]] = None, + window_mapping: Optional[Dict[str,str]]=None, + channel_mapping: Optional[Dict['ChannelID','ChannelID']]=None, target_block: Optional[InstructionBlock]=None) -> None: """Add an element to the translation stack of the target_block with the given set of parameters. @@ -139,6 +141,8 @@ def push(self, target_block = self.__main_block if window_mapping is None: window_mapping = dict() + if channel_mapping is None: + channel_mapping = dict() for (key, value) in parameters.items(): if isinstance(value, numbers.Real): @@ -147,7 +151,7 @@ def push(self, if target_block not in self.__sequencing_stacks: self.__sequencing_stacks[target_block] = [] - self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping)) + self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping, channel_mapping)) def build(self) -> InstructionBlock: """Start the translation process. Translate all elements currently on the translation stacks @@ -169,11 +173,12 @@ def build(self) -> InstructionBlock: shall_continue = False for target_block, sequencing_stack in self.__sequencing_stacks.copy().items(): while sequencing_stack: - (element, parameters, conditions, window_mapping) = sequencing_stack[-1] + (element, parameters, conditions, window_mapping, channel_mapping) = sequencing_stack[-1] if not element.requires_stop(parameters, conditions): shall_continue |= True sequencing_stack.pop() - element.build_sequence(self, parameters, conditions, window_mapping, target_block) + element.build_sequence(self, parameters, conditions, window_mapping, + channel_mapping, target_block) else: break return ImmutableInstructionBlock(self.__main_block, dict()) diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 342913f1b..c2f82ff0b 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -19,11 +19,12 @@ from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow from qctoolkit.pulses.interpolation import InterpolationStrategy, LinearInterpolationStrategy, \ HoldInterpolationStrategy, JumpInterpolationStrategy -from qctoolkit.pulses.instructions import Waveform +from qctoolkit.pulses.instructions import SingleChannelWaveform from qctoolkit.pulses.conditions import Condition from qctoolkit.expressions import Expression +from qctoolkit.pulses.multi_channel_pulse_template import ChannelID, MultiChannelWaveform -__all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] +__all__ = ["TablePulseTemplate", "TableSingleChannelWaveform", "WaveformTableEntry"] WaveformTableEntry = NamedTuple( # pylint: disable=invalid-name "WaveformTableEntry", @@ -31,50 +32,37 @@ ) -class TableWaveform(Waveform): +class TableSingleChannelWaveform(SingleChannelWaveform): """Waveform obtained from instantiating a TablePulseTemplate.""" - def __init__(self, waveform_tables: List[Tuple[WaveformTableEntry]]) -> None: + def __init__(self, waveform_table: Tuple[WaveformTableEntry]) -> None: """Create a new TableWaveform instance. Args: waveform_table (ImmutableList(WaveformTableEntry)): A list of instantiated table entries of the form (time as float, voltage as float, interpolation strategy). """ - for table in waveform_tables: - if len(table) < 2: - raise ValueError("A given waveform table has less than two entries.") + if len(waveform_table) < 2: + raise ValueError("A given waveform table has less than two entries.") super().__init__() - self.__tables = waveform_tables + self.__table = waveform_table @property def compare_key(self) -> Any: - return tuple(map(tuple, self.__tables)) - - @property - def num_channels(self) -> int: - return len(self.__tables) + return self.__table @property def duration(self) -> float: - if len(self.__tables): - return max([table[-1].t for table in self.__tables]) - else: - return 0.0 + return self.__table[-1].t def sample(self, sample_times: np.ndarray, first_offset: float=0) -> np.ndarray: - channels = len(self.__tables) sample_times = sample_times.copy() sample_times -= (sample_times[0] - first_offset) - #voltages = np.empty((len(sample_times), channels)) - voltages = np.empty((channels, len(sample_times))) - for channel, table in enumerate(self.__tables): - for entry1, entry2 in zip(table[:-1], table[1:]): - indices = np.logical_and(sample_times >= entry1.t, sample_times <= entry2.t) - #voltages[indices, channel] = \ - voltages[channel, indices] = \ - entry2.interp((entry1.t, entry1.v), (entry2.t, entry2.v), sample_times[indices]) - + voltages = np.empty(len(sample_times)) + for entry1, entry2 in zip(self.__table[:-1], self.__table[1:]): + indices = np.logical_and(sample_times >= entry1.t, sample_times <= entry2.t) + voltages[indices] = \ + entry2.interp((entry1.t, entry1.v), (entry2.t, entry2.v), sample_times[indices]) return voltages @@ -100,59 +88,63 @@ class TablePulseTemplate(AtomicPulseTemplate): Each TablePulseTemplate contains at least an entry at time 0. """ - def __init__(self, channels: int=1, identifier: Optional[str]=None) -> None: + def __init__(self, channels: List[ChannelID] = ['default'], identifier: Optional[str]=None) -> None: """Create a new TablePulseTemplate. Args: - channels (int): The number of channels defined in this TablePulseTemplate (default = 1). + channels (int): The list of channel identifiers defined in this TablePulseTemplate (default = 1). measurement (bool): True, if this TablePulseTemplate shall define a measurement window. (optional, default = False). identifier (str): A unique identifier for use in serialization. (optional) """ + if len(set(channels)) != len(channels): + raise ValueError('ChannelIDs must be unique') + super().__init__(identifier) self.__identifier = identifier self.__interpolation_strategies = {'linear': LinearInterpolationStrategy(), 'hold': HoldInterpolationStrategy(), 'jump': JumpInterpolationStrategy() } - self.__entries = [] - for _ in range(channels): - self.__entries.append([TableEntry(0, 0, self.__interpolation_strategies['hold'])]) + self.__entries = dict((channel, [TableEntry(0, 0, self.__interpolation_strategies['hold'])]) + for channel in channels) self.__time_parameter_declarations = {} # type: Dict[str, ParameterDeclaration] self.__voltage_parameter_declarations = {} # type: Dict[str, ParameterDeclaration] self.__measurement_windows = {} # type: Dict[str,List[MeasurementDeclaration]] - self.__channels = channels # type: int @staticmethod - def from_array(times: np.ndarray, voltages: np.ndarray) \ + def from_array(times: np.ndarray, voltages: np.ndarray, channels: Optional[List[ChannelID]] = None) \ -> 'TablePulseTemplate': """Static constructor to build a TablePulse from numpy arrays. Args: times: 1D numpy array with time values voltages: 1D or 2D numpy array with voltage values + channels: list of channel IDs. Mandatory if voltages is 2D Returns: TablePulseTemplate with the given values, hold interpolation everywhere and no free parameters. """ + res = TablePulseTemplate(channels=channels) if channels else TablePulseTemplate() if voltages.ndim == 1: - res = TablePulseTemplate(channels=1) for time, voltage in zip(times, voltages): res.add_entry(time, voltage, interpolation='hold') elif voltages.ndim == 2: - channels = voltages.shape[1] - res = TablePulseTemplate(channels=channels) - for channel in range(channels): - for time, voltage in zip(times, voltages[:, channel]): - res.add_entry(time, voltage, interpolation='hold', channel=channel) + if not channels: + raise ValueError('For a multi channel table pulse template a list of channel IDs mut be provided.') + if len(channels) != voltages.shape[1]: + raise ValueError('There has to be exactly one channel ID for each channel.') + for channel_index in range(len(channels)): + for time, voltage in zip(times, voltages[:, channel_index]): + res.add_entry(time, voltage, interpolation='hold', channel=channels[channel_index]) return res def add_entry(self, time: Union[float, str, ParameterDeclaration], voltage: Union[float, str, ParameterDeclaration], interpolation: str='hold', - channel: int=0) -> None: + channel: Optional[ChannelID]=None) -> None: """Add an entry to the end of this TablePulseTemplate. The arguments time and voltage may either be real numbers or a string which @@ -199,9 +191,14 @@ def add_entry(self, ValueError if the constraints listed above are violated. """ # Check if channel is valid - if channel < 0 or channel > self.__channels - 1: - raise ValueError("Channel number out of bounds. Allowed values: {}".format( - ', '.join(map(str, range(self.__channels)))) + if channel is None: + if len(self.__entries) == 1: + channel = next(iter(self.__entries.keys())) + else: + raise ValueError('Channel ID has to be specified if more than one channel is present') + elif channel not in self.__entries: + raise ValueError("Channel ID not known. Allowed values: {}".format( + ', '.join(self.__entries.keys())) ) # Check if interpolation value is valid @@ -360,9 +357,12 @@ def __add_entry_check_and_modify_time(self, return time @property - def entries(self) -> List[List[TableEntry]]: + def entries(self) -> Union[List[TableEntry],Dict[ChannelID,List[TableEntry]]]: """Immutable copies of this TablePulseTemplate's entries.""" - return copy.deepcopy(self.__entries) + if len(self.__entries) == 1: + return copy.deepcopy(next(iter(self.__entries.values()))) + else: + return copy.deepcopy(self.__entries) @property def parameter_names(self) -> Set[str]: @@ -382,7 +382,7 @@ def get_val(v): **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] for name_ in v.variables()}) - t_max = [entry[-1][0] for entry in self.__entries] + t_max = [entry[-1][0] for entry in self.__entries.values()] t_max = max([t if isinstance(t,numbers.Number) else t.get_value(parameters) for t in t_max]) resulting_windows = [] @@ -400,7 +400,9 @@ def measurement_declarations(self): :return: Measurement declarations as added by the add_measurement_declaration method """ as_builtin = lambda x: str(x) if isinstance(x, Expression) else x - return { name: [(as_builtin(begin), as_builtin(end)) for begin, end in windows] for name, windows in self.__measurement_windows.items() } + return {name: [(as_builtin(begin), as_builtin(end)) + for begin, end in windows] + for name, windows in self.__measurement_windows.items() } @property @@ -437,12 +439,16 @@ def add_measurement_declaration(self, name: str, begin: Union[float,str], end: U def is_interruptable(self) -> bool: return False + @property + def defined_channels(self) -> Set[ChannelID]: + return set(self.__entries.keys()) + @property def num_channels(self) -> int: - return self.__channels - + return len(self.__entries) + def get_entries_instantiated(self, parameters: Dict[str, Parameter]) \ - -> List[List[Tuple[float, float]]]: + -> Dict[ChannelID,List[Tuple[float, float]]]: """Compute an instantiated list of the table's entries. Args: @@ -451,15 +457,15 @@ def get_entries_instantiated(self, parameters: Dict[str, Parameter]) \ (float, float)-list of all table entries with concrete values provided by the given parameters. """ - instantiated_entries = [] # type: List[List[TableEntry]] + instantiated_entries = dict() # type: Dict[ChannelID,List[Tuple[float, float]]] max_time = 0 - for channel in range(self.__channels): - entries = self.__entries[channel] + + for channel, channel_entries in self.__entries.items(): instantiated = [] - if not entries: + if not channel_entries: instantiated.append(TableEntry(0, 0, self.__interpolation_strategies['hold'])) else: - for entry in entries: + for entry in channel_entries: # resolve time parameter references if isinstance(entry.t, ParameterDeclaration): time_value = entry.t.get_value(parameters) @@ -481,17 +487,17 @@ def get_entries_instantiated(self, parameters: Dict[str, Parameter]) \ .format(time, previous_time)) previous_time = time - instantiated_entries.append(instantiated) + instantiated_entries[channel] = instantiated # ensure that all channels have equal duration - for instantiated in instantiated_entries: + for channel, instantiated in instantiated_entries.items(): final_entry = instantiated[-1] if final_entry.t != max_time: instantiated.append(TableEntry(max_time, final_entry.v, - self.__interpolation_strategies['hold']) - ) - return list(map(TablePulseTemplate.__clean_entries, instantiated_entries)) + self.__interpolation_strategies['hold'])) + instantiated_entries[channel] = TablePulseTemplate.__clean_entries(instantiated) + return instantiated_entries @staticmethod def __clean_entries(entries: List[Tuple[float, float]]) -> List[Tuple[float, float]]: @@ -515,11 +521,10 @@ def __clean_entries(entries: List[Tuple[float, float]]) -> List[Tuple[float, flo entries.pop(index) return entries - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[Waveform]: + def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[MultiChannelWaveform]: instantiated = self.get_entries_instantiated(parameters) - if instantiated[0][-1].t > 0: - return TableWaveform(instantiated) - return None + return MultiChannelWaveform({channel: TableSingleChannelWaveform(instantiated_channel) + for channel, instantiated_channel in instantiated.items()}) def requires_stop(self, parameters: Dict[str, Parameter], @@ -541,16 +546,17 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data['voltage_parameter_declarations'] = \ [serializer.dictify(self.__voltage_parameter_declarations[key]) for key in sorted(self.__voltage_parameter_declarations.keys())] - serialized_entries = [] - for channel in self.__entries: - entries = [] - for (time, voltage, interpolation) in channel: + + serialized_entries = dict() + for channel, channel_entries in self.__entries.items(): + serialized_channel_entries = [] + for (time, voltage, interpolation) in channel_entries: if isinstance(time, ParameterDeclaration): time = time.name if isinstance(voltage, ParameterDeclaration): voltage = voltage.name - entries.append((time, voltage, str(interpolation))) - serialized_entries.append(entries) + serialized_channel_entries.append((time, voltage, str(interpolation))) + serialized_entries[channel] = serialized_channel_entries data['entries'] = serialized_entries data['measurement_declarations'] = self.measurement_declarations data['type'] = serializer.get_type_identifier(self) @@ -560,7 +566,7 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: def deserialize(serializer: Serializer, time_parameter_declarations: Iterable[Any], voltage_parameter_declarations: Iterable[Any], - entries: Iterable[Any], + entries: Dict[ChannelID,Any], measurement_declarations: Dict[str,Iterable[Any]], identifier: Optional[str]=None) -> 'TablePulseTemplate': time_parameter_declarations = \ @@ -570,10 +576,10 @@ def deserialize(serializer: Serializer, {declaration['name']: serializer.deserialize(declaration) for declaration in voltage_parameter_declarations} - template = TablePulseTemplate(channels=len(entries), + template = TablePulseTemplate(channels=list(entries.keys()), identifier=identifier) - for channel, channel_entries in enumerate(entries): + for channel, channel_entries in entries.items(): for (time, voltage, interpolation) in channel_entries: if isinstance(time, str): time = time_parameter_declarations[time] diff --git a/tests/pulses/branch_pulse_template_tests.py b/tests/pulses/branch_pulse_template_tests.py index 9884ab689..105640bbe 100644 --- a/tests/pulses/branch_pulse_template_tests.py +++ b/tests/pulses/branch_pulse_template_tests.py @@ -10,30 +10,30 @@ class BranchPulseTemplateTest(unittest.TestCase): - def test_wrong_num_channel_composition(self) -> None: - if_dummy = DummyPulseTemplate(num_channels=2) - else_dummy = DummyPulseTemplate(num_channels=5) + def test_wrong_channel_composition(self) -> None: + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'C'}) with self.assertRaises(ValueError): BranchPulseTemplate('foo_condition', if_dummy, else_dummy) def test_identifier(self) -> None: - if_dummy = DummyPulseTemplate(num_channels=3) - else_dummy = DummyPulseTemplate(num_channels=3) + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy, identifier='hugo') self.assertEqual('hugo', template.identifier) def test_parameter_names_and_declarations(self) -> None: - if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}) - else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}) + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) self.assertEqual({'foo', 'bar', 'hugo'}, template.parameter_names) self.assertEqual({ParameterDeclaration(name) for name in {'foo', 'bar', 'hugo'}}, template.parameter_declarations) - def test_num_channels(self) -> None: - if_dummy = DummyPulseTemplate(num_channels=3) - else_dummy = DummyPulseTemplate(num_channels=3) + def test_defined_channels(self) -> None: + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertEqual(3, template.num_channels) + self.assertEqual({'A', 'B'}, template.defined_channels) def test_measurement_names(self) -> None: if_dummy = DummyPulseTemplate(measurement_names={'if_meas'}) @@ -42,19 +42,19 @@ def test_measurement_names(self) -> None: self.assertEqual({'if_meas','else_meas'}, template.measurement_names) def test_is_interruptable(self) -> None: - if_dummy = DummyPulseTemplate(num_channels=3, is_interruptable=True) - else_dummy = DummyPulseTemplate(num_channels=3) + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) self.assertFalse(template.is_interruptable) - if_dummy = DummyPulseTemplate(num_channels=3, is_interruptable=True) - else_dummy = DummyPulseTemplate(num_channels=3, is_interruptable=True) + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) self.assertTrue(template.is_interruptable) def test_str(self) -> None: - if_dummy = DummyPulseTemplate(num_channels=3, is_interruptable=True) - else_dummy = DummyPulseTemplate(num_channels=3) + if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) + else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) self.assertIsInstance(str(template), str) @@ -63,9 +63,9 @@ class BranchPulseTemplateSequencingTests(unittest.TestCase): def setUp(self) -> None: self.maxDiff = None - self.if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}, + self.if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}, measurement_names={'if_meas'}) - self.else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}, + self.else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}, measurement_names={'else_meas'}) self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) self.sequencer = DummySequencer() @@ -120,8 +120,9 @@ def test_build_sequence(self) -> None: bar=DummyParameter(-2624.23), hugo=DummyParameter(3.532)) window_mapping = dict(else_meas='my_meas',if_meas='thy_meas') + channel_mapping = dict() - self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, channel_mapping, self.block) self.assertFalse(foo_condition.loop_call_data) self.assertEqual( dict( @@ -132,6 +133,7 @@ def test_build_sequence(self) -> None: parameters=parameters, conditions=conditions, measurement_mapping=window_mapping, + channel_mapping=channel_mapping, instruction_block=self.block ), foo_condition.branch_call_data @@ -144,9 +146,10 @@ def test_build_sequence_condition_missing(self) -> None: parameters = dict(foo=DummyParameter(326.272), bar=DummyParameter(-2624.23), hugo=DummyParameter(3.532)) - window_mapping = {} + window_mapping = dict() + channel_mapping = dict() with self.assertRaises(ConditionMissingException): - self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, channel_mapping, self.block) def test_build_sequence_parameter_missing(self) -> None: foo_condition = DummyCondition() @@ -154,7 +157,8 @@ def test_build_sequence_parameter_missing(self) -> None: parameters = dict(foo=DummyParameter(326.272), bar=DummyParameter(-2624.23)) window_mapping = dict(else_meas='my_meas',if_meas='thy_meas') - self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, self.block) + channel_mapping = dict() + self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, channel_mapping, self.block) self.assertFalse(foo_condition.loop_call_data) self.assertEqual( dict( @@ -165,6 +169,7 @@ def test_build_sequence_parameter_missing(self) -> None: parameters=parameters, conditions=conditions, measurement_mapping=window_mapping, + channel_mapping=channel_mapping, instruction_block=self.block ), foo_condition.branch_call_data @@ -177,8 +182,8 @@ class BranchPulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: self.maxDiff = None - self.if_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'bar'}, measurement_names={'if_mease'}) - self.else_dummy = DummyPulseTemplate(num_channels=3, parameter_names={'foo', 'hugo'}, measurement_names={'else_meas'}) + self.if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}, measurement_names={'if_mease'}) + self.else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}, measurement_names={'else_meas'}) self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) def test_get_serialization_data(self) -> None: diff --git a/tests/pulses/conditions_tests.py b/tests/pulses/conditions_tests.py index 24b2eb6dd..6daac3ea2 100644 --- a/tests/pulses/conditions_tests.py +++ b/tests/pulses/conditions_tests.py @@ -8,6 +8,9 @@ class HardwareConditionTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args,**kwargs) + self.maxDiff = None def test_build_sequence_loop(self) -> None: sequencer = DummySequencer() @@ -19,14 +22,14 @@ def test_build_sequence_loop(self) -> None: trigger = Trigger() condition = HardwareCondition(trigger) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(1, len(block.embedded_blocks)) body_block = block.embedded_blocks[0] self.assertEqual([DummyInstruction(), CJMPInstruction(trigger, InstructionPointer(body_block))], block.instructions, "The expected conditional jump was not generated by HardwareConditon.") self.assertEqual(InstructionPointer(block, 1), body_block.return_ip, "The return address of the loop body block was set wrongly by HardwareCondition.") - self.assertEqual({body_block: [(body, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the body element to the stack") + self.assertEqual({body_block: [(body, {}, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the body element to the stack") self.assertFalse(condition.requires_stop()) def test_build_sequence_branch(self) -> None: @@ -39,7 +42,7 @@ def test_build_sequence_branch(self) -> None: trigger = Trigger() condition = HardwareCondition(trigger) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, {}, instruction_block=block) self.assertEqual(2, len(block.embedded_blocks)) if_block = block.embedded_blocks[0] @@ -48,7 +51,7 @@ def test_build_sequence_branch(self) -> None: self.assertEqual([CJMPInstruction(trigger, InstructionPointer(if_block)), GOTOInstruction(InstructionPointer(else_block))], block.instructions, "The expected jump instruction were not generated by HardwareConditon.") self.assertEqual(InstructionPointer(block, 2), if_block.return_ip, "The return address of the if branch block was set wrongly by HardwareConditon.") self.assertEqual(InstructionPointer(block, 2), else_block.return_ip, "The return address of the else branch block was set wrongly by HardwareConditon.") - self.assertEqual({if_block: [(if_branch, {}, {}, {})], else_block: [(else_branch, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the branch elements to the stack") + self.assertEqual({if_block: [(if_branch, {}, {}, {}, {})], else_block: [(else_branch, {}, {}, {}, {})]}, sequencer.sequencing_stacks, "HardwareCondition did not correctly push the branch elements to the stack") class IterationCallbackDummy: @@ -76,9 +79,9 @@ def test_build_cannot_evaluate(self) -> None: self.assertTrue(condition.requires_stop()) with self.assertRaises(ConditionEvaluationException): - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) with self.assertRaises(ConditionEvaluationException): - condition.build_sequence_branch(delegator, body, body, sequencer, {}, {}, {}, block) + condition.build_sequence_branch(delegator, body, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(str(ConditionEvaluationException()), "The Condition can currently not be evaluated.") def test_build_sequence_loop_true(self) -> None: @@ -90,13 +93,13 @@ def test_build_sequence_loop_true(self) -> None: callback = IterationCallbackDummy(True) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) - self.assertEqual({block: [(delegator, {}, {}, {}), (body, {}, {}, {})]}, sequencer.sequencing_stacks) + self.assertEqual({block: [(delegator, {}, {}, {}, {}), (body, {}, {}, {}, {})]}, sequencer.sequencing_stacks) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(1, callback.loop_iteration) def test_build_sequence_loop_false(self): @@ -108,13 +111,13 @@ def test_build_sequence_loop_false(self): callback = IterationCallbackDummy(False) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) self.assertFalse(sequencer.sequencing_stacks) - condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, block) + condition.build_sequence_loop(delegator, body, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) def test_build_sequence_branch_true(self): @@ -127,13 +130,13 @@ def test_build_sequence_branch_true(self): callback = IterationCallbackDummy(True) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) - self.assertEqual({block: [(if_branch, {}, {}, {})]}, sequencer.sequencing_stacks) + self.assertEqual({block: [(if_branch, {}, {}, {}, {})]}, sequencer.sequencing_stacks) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) @@ -147,13 +150,13 @@ def test_build_sequence_branch_false(self): callback = IterationCallbackDummy(False) condition = SoftwareCondition(lambda loop_iteration: callback.callback(loop_iteration)) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) self.assertFalse(block.instructions) - self.assertEqual({block: [(else_branch, {}, {}, {})]}, sequencer.sequencing_stacks) + self.assertEqual({block: [(else_branch, {}, {}, {}, {})]}, sequencer.sequencing_stacks) - condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, block) + condition.build_sequence_branch(delegator, if_branch, else_branch, sequencer, {}, {}, {}, {}, block) self.assertEqual(0, callback.loop_iteration) if __name__ == "__main__": diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index c4e10b641..c4e19a660 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -1,9 +1,10 @@ import unittest from qctoolkit.pulses.function_pulse_template import FunctionPulseTemplate,\ - FunctionWaveform + FunctionSingleChannelWaveform from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException from qctoolkit.expressions import Expression +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform import numpy as np from tests.serialization_dummies import DummySerializer @@ -16,7 +17,7 @@ def setUp(self) -> None: self.maxDiff = None self.s = 'a + b * t' self.s2 = 'c' - self.fpt = FunctionPulseTemplate(self.s, self.s2) + self.fpt = FunctionPulseTemplate(self.s, self.s2,channel='A') self.pars = dict(a=DummyParameter(1), b=DummyParameter(2), c=DummyParameter(136.78)) def test_get_pulse_length(self) -> None: @@ -29,8 +30,9 @@ def test_get_pulse_length_missing_parameter(self) -> None: def test_is_interruptable(self) -> None: self.assertFalse(self.fpt.is_interruptable) - def test_num_channels(self) -> None: - self.assertEqual(1, self.fpt.num_channels) + + def test_defined_channels(self) -> None: + self.assertEqual({'A'}, self.fpt.defined_channels) def test_parameter_names_and_declarations_expression_input(self) -> None: template = FunctionPulseTemplate(Expression("3 * foo + bar * t"), Expression("5 * hugo")) @@ -39,7 +41,7 @@ def test_parameter_names_and_declarations_expression_input(self) -> None: self.assertEqual({ParameterDeclaration(name) for name in expected_parameter_names}, template.parameter_declarations) def test_parameter_names_and_declarations_string_input(self) -> None: - template = FunctionPulseTemplate("3 * foo + bar * t", "5 * hugo") + template = FunctionPulseTemplate("3 * foo + bar * t", "5 * hugo",channel='A') expected_parameter_names = {'foo', 'bar', 'hugo'} self.assertEqual(expected_parameter_names, template.parameter_names) self.assertEqual({ParameterDeclaration(name) for name in expected_parameter_names}, @@ -48,14 +50,14 @@ def test_parameter_names_and_declarations_string_input(self) -> None: def test_serialization_data(self) -> None: expected_data = dict(duration_expression=str(self.s2), expression=str(self.s), - measurement=False) + channel='A') self.assertEqual(expected_data, self.fpt.get_serialization_data( DummySerializer(serialize_callback=lambda x: str(x)))) def test_deserialize(self) -> None: basic_data = dict(duration_expression=str(self.s2), expression=str(self.s), - measurement=False, + channel='A', identifier='hugo') serializer = DummySerializer(serialize_callback=lambda x: str(x)) serializer.subelements[str(self.s2)] = Expression(self.s2) @@ -82,8 +84,8 @@ def setUp(self) -> None: def test_build_waveform(self) -> None: wf = self.fpt.build_waveform(self.args) self.assertIsNotNone(wf) - self.assertIsInstance(wf, FunctionWaveform) - expected_waveform = FunctionWaveform(dict(a=3, y=1), Expression(self.f), Expression(self.duration)) + self.assertIsInstance(wf, MultiChannelWaveform) + expected_waveform = MultiChannelWaveform({'default':FunctionSingleChannelWaveform(dict(a=3, y=1), Expression(self.f), Expression(self.duration))}) self.assertEqual(expected_waveform, wf) def test_requires_stop(self) -> None: @@ -96,11 +98,11 @@ def test_requires_stop(self) -> None: class FunctionWaveformTest(unittest.TestCase): def test_equality(self) -> None: - wf1a = FunctionWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b')) - wf1b = FunctionWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b')) - wf2 = FunctionWaveform(dict(a=3, b=1), Expression('a*t'), Expression('b')) - wf3 = FunctionWaveform(dict(a=2, b=1), Expression('a*t+2'), Expression('b')) - wf4 = FunctionWaveform(dict(a=2, c=2), Expression('a*t'), Expression('c')) + wf1a = FunctionSingleChannelWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b')) + wf1b = FunctionSingleChannelWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b')) + wf2 = FunctionSingleChannelWaveform(dict(a=3, b=1), Expression('a*t'), Expression('b')) + wf3 = FunctionSingleChannelWaveform(dict(a=2, b=1), Expression('a*t+2'), Expression('b')) + wf4 = FunctionSingleChannelWaveform(dict(a=2, c=2), Expression('a*t'), Expression('c')) self.assertEqual(wf1a, wf1a) self.assertEqual(wf1a, wf1b) self.assertNotEqual(wf1a, wf2) @@ -108,18 +110,18 @@ def test_equality(self) -> None: self.assertNotEqual(wf1a, wf4) def test_num_channels(self) -> None: - wf = FunctionWaveform(dict(), Expression('t'), Expression('4')) + wf = FunctionSingleChannelWaveform(dict(), Expression('t'), Expression('4')) self.assertEqual(1, wf.num_channels) def test_duration(self) -> None: - wf = FunctionWaveform(dict(foo=2.5), Expression('2*t'), Expression('4*foo/5')) + wf = FunctionSingleChannelWaveform(dict(foo=2.5), Expression('2*t'), Expression('4*foo/5')) self.assertEqual(2, wf.duration) def test_sample(self) -> None: f = Expression("(t+1)**b") length = Expression("c**b") par = {"b":2,"c":10} - fw = FunctionWaveform(par,f,length) + fw = FunctionSingleChannelWaveform(par, f, length) a = np.arange(4) expected_result = [[1, 4, 9, 16]] result = fw.sample(a) diff --git a/tests/pulses/instructions_tests.py b/tests/pulses/instructions_tests.py index 47a3e6b45..07a0dd23b 100644 --- a/tests/pulses/instructions_tests.py +++ b/tests/pulses/instructions_tests.py @@ -6,7 +6,7 @@ Trigger, CJMPInstruction, REPJInstruction, GOTOInstruction, EXECInstruction, STOPInstruction,\ InstructionSequence, AbstractInstructionBlock, ImmutableInstructionBlock, Instruction -from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummyInstructionBlock class InstructionPointerTest(unittest.TestCase): @@ -172,13 +172,13 @@ def test_str(self) -> None: class EXECInstructionTest(unittest.TestCase): def test_initialization(self): - waveform = DummyWaveform() + waveform = DummySingleChannelWaveform() instr = EXECInstruction(waveform) self.assertIs(waveform, instr.waveform) def test_equality(self): - wf1 = DummyWaveform() - wf2 = DummyWaveform() + wf1 = DummySingleChannelWaveform() + wf2 = DummySingleChannelWaveform() instr11 = EXECInstruction(wf1) instr12 = EXECInstruction(wf1) instr20 = EXECInstruction(wf2) @@ -191,7 +191,7 @@ def test_equality(self): self.assertNotEqual(hash(instr11), hash(instr20)) def test_str(self) -> None: - wf = DummyWaveform() + wf = DummySingleChannelWaveform() instr = EXECInstruction(wf) self.assertEqual("exec {}".format(str(wf)), str(instr)) @@ -239,7 +239,7 @@ def test_len_empty(self) -> None: self.assertEqual(0, len(block.instructions)) def test_len(self) -> None: - block = AbstractInstructionBlockStub([EXECInstruction(DummyWaveform())], None) + block = AbstractInstructionBlockStub([EXECInstruction(DummySingleChannelWaveform())], None) self.assertEqual(2, len(block)) self.assertEqual(1, len(block.instructions)) @@ -262,7 +262,7 @@ def test_iterable_empty_return(self) -> None: count += 1 def test_iterable_no_return(self) -> None: - wf = DummyWaveform() + wf = DummySingleChannelWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], None) count = 0 for expected_instruction, instruction in zip([EXECInstruction(wf), STOPInstruction()], block): @@ -272,7 +272,7 @@ def test_iterable_no_return(self) -> None: def test_iterable_return(self) -> None: parent_block = InstructionBlock() - wf = DummyWaveform() + wf = DummySingleChannelWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], InstructionPointer(parent_block, 11)) count = 0 for expected_instruction, instruction in zip([EXECInstruction(wf), GOTOInstruction(InstructionPointer(parent_block, 11))], block): @@ -300,7 +300,7 @@ def test_item_access_empty_return(self) -> None: block[-2] def test_item_access_no_return(self) -> None: - wf = DummyWaveform() + wf = DummySingleChannelWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], None) self.assertEqual(EXECInstruction(wf), block[0]) self.assertEqual(STOPInstruction(), block[1]) @@ -312,7 +312,7 @@ def test_item_access_no_return(self) -> None: block[-3] def test_item_access_return(self) -> None: - wf = DummyWaveform() + wf = DummySingleChannelWaveform() parent_block = InstructionBlock() block = AbstractInstructionBlockStub([EXECInstruction(wf)], InstructionPointer(parent_block, 29)) self.assertEqual(EXECInstruction(wf), block[0]) @@ -324,6 +324,24 @@ def test_item_access_return(self) -> None: with self.assertRaises(IndexError): block[-3] + def test_sliced_item_access(self) -> None: + wf = DummySingleChannelWaveform() + parent_block = InstructionBlock() + block = AbstractInstructionBlockStub([EXECInstruction(wf), EXECInstruction(wf)], InstructionPointer(parent_block, 29)) + for instruction in block[:-1]: + self.assertEqual(EXECInstruction(wf), instruction) + + expections = [EXECInstruction(wf), EXECInstruction(wf), GOTOInstruction(InstructionPointer(parent_block, 29))] + + for expected, instruction in zip(expections,block[:4]): + self.assertEqual(expected, instruction) + + for instruction, expected in zip(block[::-1], reversed(expections)): + self.assertEqual(expected, instruction) + + with self.assertRaises(StopIteration): + next(iter(block[3:])) + class InstructionBlockTest(unittest.TestCase): @@ -362,7 +380,7 @@ def test_add_instruction_exec(self) -> None: block = InstructionBlock() expected_instructions = [] - waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] + waveforms = [DummySingleChannelWaveform(), DummySingleChannelWaveform(), DummySingleChannelWaveform()] LOOKUP = [0, 1, 1, 0, 2, 1, 0, 0, 0, 1, 2, 2] for id in LOOKUP: waveform = waveforms[id] @@ -436,7 +454,7 @@ def test_nested_block_construction(self) -> None: blocks = [] - waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] + waveforms = [DummySingleChannelWaveform(), DummySingleChannelWaveform(), DummySingleChannelWaveform()] main_block.add_instruction_exec(waveforms[0]) expected_instructions[0].append(EXECInstruction(waveforms[0])) @@ -589,7 +607,7 @@ def test_nested_goto(self) -> None: def test_multiple_nested_block_construction(self) -> None: main_block = InstructionBlock() blocks = [] - waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] + waveforms = [DummySingleChannelWaveform(), DummySingleChannelWaveform(), DummySingleChannelWaveform()] main_block.add_instruction_exec(waveforms[0]) @@ -634,7 +652,7 @@ class InstructionStringRepresentation(unittest.TestCase): def test_str(self) -> None: IB = InstructionBlock() T = Trigger() - W = DummyWaveform() + W = DummySingleChannelWaveform() a = [W, T, diff --git a/tests/pulses/loop_pulse_template_tests.py b/tests/pulses/loop_pulse_template_tests.py index 903fc1255..4398a2fd6 100644 --- a/tests/pulses/loop_pulse_template_tests.py +++ b/tests/pulses/loop_pulse_template_tests.py @@ -58,8 +58,9 @@ def test_build_sequence(self) -> None: block = DummyInstructionBlock() parameters = {} conditions = {'foo_cond': condition} - measurement_mapping = {'swag':'aufdrehen'} - t.build_sequence(sequencer, parameters, conditions, measurement_mapping, block) + measurement_mapping = {'swag': 'aufdrehen'} + channel_mapping = {} + t.build_sequence(sequencer, parameters, conditions, measurement_mapping, channel_mapping, block) expected_data = dict( delegator=t, body=body, @@ -67,6 +68,7 @@ def test_build_sequence(self) -> None: parameters=parameters, conditions=conditions, measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, instruction_block=block ) self.assertEqual(expected_data, condition.loop_call_data) diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index e7dfbb849..3dd499226 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -5,11 +5,11 @@ from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ MissingParameterDeclarationException, UnnecessaryMappingException from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, MappedParameter, ConstantParameter -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelPulseTemplate, MultiChannelWaveform +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelPulseTemplate, MultiChannelWaveform, MappingTemplate, ChannelMappingException from qctoolkit.expressions import Expression -from qctoolkit.pulses.pulse_template import SubTemplate +from qctoolkit.pulses.instructions import CHANInstruction, EXECInstruction -from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform +from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummySingleChannelWaveform from tests.serialization_dummies import DummySerializer @@ -17,38 +17,30 @@ class MultiChannelWaveformTest(unittest.TestCase): def test_init_no_args(self) -> None: with self.assertRaises(ValueError): - MultiChannelWaveform([]) + MultiChannelWaveform(dict()) with self.assertRaises(ValueError): MultiChannelWaveform(None) def test_init_single_channel(self) -> None: - dwf = DummyWaveform(duration=1.3) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwf, [1])]) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwf, [-1])]) - waveform = MultiChannelWaveform([(dwf, [0])]) - self.assertEqual(1, waveform.num_channels) + dwf = DummySingleChannelWaveform(duration=1.3) + + waveform = MultiChannelWaveform({0: dwf}) + self.assertEqual({0}, waveform.defined_channels) self.assertEqual(1.3, waveform.duration) def test_init_several_channels(self) -> None: - dwfa = DummyWaveform(duration=4.2, num_channels=2) - dwfb = DummyWaveform(duration=4.2, num_channels=3) - dwfc = DummyWaveform(duration=2.3) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, 5, 1])]) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, -1, 1])]) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwfa, [0, 1]), (dwfc, [2])]) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwfa, [0, 0]), (dwfb, [3, 4, 1])]) - with self.assertRaises(ValueError): - MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, 4, 1])]) - waveform = MultiChannelWaveform([(dwfa, [2, 4]), (dwfb, [3, 0, 1])]) - self.assertEqual(5, waveform.num_channels) + dwfa = DummySingleChannelWaveform(duration=4.2) + dwfb = DummySingleChannelWaveform(duration=4.2) + dwfc = DummySingleChannelWaveform(duration=2.3) + + waveform = MultiChannelWaveform({0: dwfa, 1: dwfb}) + self.assertEqual({0, 1}, waveform.defined_channels) self.assertEqual(4.2, waveform.duration) + with self.assertRaises(ValueError): + MultiChannelWaveform({0: dwfa, 1: dwfc}) + + @unittest.skip("Think where to put equivalent code.") def test_sample(self) -> None: sample_times = numpy.linspace(98.5, 103.5, num=11) samples_a = numpy.array([ @@ -61,8 +53,8 @@ def test_sample(self) -> None: [-10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20] # [-10], [-11], [-12], [-13], [-14], [-15], [-16], [-17], [-18], [-19], [-20] ]) - dwf_a = DummyWaveform(duration=3.2, sample_output=samples_a, num_channels=2) - dwf_b = DummyWaveform(duration=3.2, sample_output=samples_b, num_channels=1) + dwf_a = DummySingleChannelWaveform(duration=3.2, sample_output=samples_a, defined_channels={'A'}) + dwf_b = DummySingleChannelWaveform(duration=3.2, sample_output=samples_b, defined_channels={'A'}) waveform = MultiChannelWaveform([(dwf_a, [2, 0]), (dwf_b, [1])]) self.assertEqual(3, waveform.num_channels) self.assertEqual(3.2, waveform.duration) @@ -92,17 +84,30 @@ def test_sample(self) -> None: self.assertTrue(numpy.all(expected == result)) def test_equality(self) -> None: - dwf_a = DummyWaveform(duration=246.2, num_channels=2) - waveform_a1 = MultiChannelWaveform([(dwf_a, [0, 1])]) - waveform_a2 = MultiChannelWaveform([(dwf_a, [0, 1])]) - waveform_a3 = MultiChannelWaveform([(dwf_a, [1, 0])]) + dwf_a = DummySingleChannelWaveform(duration=246.2) + waveform_a1 = MultiChannelWaveform({0: dwf_a, 1: dwf_a}) + waveform_a2 = MultiChannelWaveform({0: dwf_a, 1: dwf_a}) + waveform_a3 = MultiChannelWaveform({0: dwf_a, 2: dwf_a}) self.assertEqual(waveform_a1, waveform_a1) self.assertEqual(waveform_a1, waveform_a2) self.assertNotEqual(waveform_a1, waveform_a3) class MultiChannelPulseTemplateTest(unittest.TestCase): - + def __init__(self,*args,**kwargs): + super().__init__(*args,**kwargs) + + self.subtemplates = [DummyPulseTemplate(parameter_names={'p1'}, measurement_names={'m1'}, defined_channels={'c1'}, + requires_stop=True, is_interruptable=False), + DummyPulseTemplate(parameter_names={'p2'}, measurement_names={'m2'}, defined_channels={'c2'}, + requires_stop=False, is_interruptable=True), + DummyPulseTemplate(parameter_names={'p3'}, measurement_names={'m3'}, defined_channels={'c3'}, + requires_stop=False, is_interruptable=True)] + self.no_param_maps = [{'p1': '1'}, {'p2': '2'}, {'p3': '3'}] + self.param_maps = [{'p1': 'pp1'}, {'p2': 'pp2'}, {'p3': 'pp3'}] + self.chan_maps = [{'c1': 'cc1'}, {'c2': 'cc2'}, {'c3': 'cc3'}] + + @unittest.skip('Consider forbidding empty multi channel templates') def test_init_empty(self) -> None: template = MultiChannelPulseTemplate([], {}, identifier='foo') self.assertEqual('foo', template.identifier) @@ -112,180 +117,72 @@ def test_init_empty(self) -> None: self.assertFalse(template.requires_stop(dict(), dict())) self.assertEqual(0, template.num_channels) - def test_init_single_subtemplate_no_external_params(self) -> None: - subtemplate = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, duration=1.3) - template = MultiChannelPulseTemplate([(subtemplate, {'foo': "2.3"}, [1, 0])], {}) - self.assertFalse(template.parameter_names) - self.assertFalse(template.parameter_declarations) - self.assertFalse(template.is_interruptable) - self.assertFalse(template.requires_stop(dict(), dict())) - self.assertEqual(2, template.num_channels) - - def test_init_single_subtemplate_requires_stop_external_params(self) -> None: - subtemplate = DummyPulseTemplate(parameter_names={'foo'}, requires_stop=True, num_channels=2, duration=1.3) - template = MultiChannelPulseTemplate([(subtemplate, {'foo': "2.3 ** bar"}, [1, 0])], {'bar'}) - self.assertEqual({'bar'}, template.parameter_names) - self.assertEqual({ParameterDeclaration('bar')}, template.parameter_declarations) - self.assertFalse(template.is_interruptable) - self.assertTrue(template.requires_stop(dict(bar=ConstantParameter(3.5)), dict())) - self.assertEqual(2, template.num_channels) - - def test_init_single_subtemplate_invalid_channel_mapping(self) -> None: - subtemplate = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, duration=1.3) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate([(subtemplate, {'foo': "2.3"}, [3, 0])], {}) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate([(subtemplate, {'foo': "2.3"}, [-1, 0])], {}) - - def test_init_multi_subtemplates_not_interruptable_requires_stop(self) -> None: - st1 = DummyPulseTemplate(parameter_names={'foo'}, requires_stop=True, num_channels=2, - duration=1.3) - st2 = DummyPulseTemplate(parameter_names={'bar'}, is_interruptable=True, num_channels=1, - duration=6.34) - template = MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 2]), - (st2, {'bar': "bar"}, [1]) - ], - {'bar'} - ) - self.assertEqual({'bar'}, template.parameter_names) - self.assertEqual({ParameterDeclaration('bar')}, template.parameter_declarations) - self.assertFalse(template.is_interruptable) - self.assertTrue(template.requires_stop(dict(bar=ConstantParameter(52.6)), dict())) - self.assertEqual(3, template.num_channels) - - def test_init_multi_subtemplates_interruptable_no_requires_stop(self) -> None: - st1 = DummyPulseTemplate(parameter_names={'foo'}, is_interruptable=True, num_channels=2, - duration=1.3) - st2 = DummyPulseTemplate(parameter_names={'bar'}, is_interruptable=True, num_channels=1, - duration=6.34) - template = MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 2]), - (st2, {'bar': "bar"}, [1]) - ], - {'bar'} - ) - self.assertEqual({'bar'}, template.parameter_names) - self.assertEqual({ParameterDeclaration('bar')}, template.parameter_declarations) - self.assertTrue(template.is_interruptable) - self.assertFalse(template.requires_stop(dict(bar=ConstantParameter(4.1)), dict())) - self.assertEqual(3, template.num_channels) - - def test_init_multi_subtemplates_wrong_channel_mapping(self) -> None: - st1 = DummyPulseTemplate(parameter_names={'foo'}, is_interruptable=True, num_channels=2, - duration=1.3) - st2 = DummyPulseTemplate(parameter_names={'bar'}, is_interruptable=True, num_channels=1, - duration=6.34) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 3]), - (st2, {'bar': "bar"}, [1]) - ], - {'bar'} - ) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, -1]), - (st2, {'bar': "bar"}, [1]) - ], - {'bar'} - ) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 2]), - (st2, {'bar': "bar"}, [-1]) - ], - {'bar'} - ) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 2]), - (st2, {'bar': "bar"}, [3]) - ], - {'bar'} - ) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 0]), - (st2, {'bar': "bar"}, [1]) - ], - {'bar'} - ) - with self.assertRaises(ValueError): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 2]), - (st2, {'bar': "bar"}, [2]) - ], - {'bar'} - ) - - def test_init_broken_parameter_mappings(self) -> None: - st1 = DummyPulseTemplate(parameter_names={'foo'}, is_interruptable=True, num_channels=2, - duration=1.3) - st2 = DummyPulseTemplate(parameter_names={'bar'}, is_interruptable=True, num_channels=1, - duration=6.34) - with self.assertRaises(MissingMappingException): - MultiChannelPulseTemplate([(st1, {'foo': "bar"}, [0, 2]), (st2, {}, [1])], {'bar'}) - with self.assertRaises(MissingMappingException): - MultiChannelPulseTemplate([(st1, {}, [0, 2]), (st2, {'bar': "bar"}, [1])], {'bar'}) + def test_mapping_template_pure_conversion(self): + subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] + template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) + + for st, pm, cm in zip(template.subtemplates, self.param_maps, self.chan_maps): + self.assertEqual(st.parameter_names, set(pm.values())) + self.assertEqual(st.defined_channels, set(cm.values())) + + def test_mapping_template_mixed_conversion(self): + subtemp_args = [ + (self.subtemplates[0], self.param_maps[0], self.chan_maps[0]), + MappingTemplate(self.subtemplates[1], self.param_maps[1], channel_mapping=self.chan_maps[1]), + (self.subtemplates[2], self.param_maps[2], self.chan_maps[2]) + ] + template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) + + for st, pm, cm in zip(template.subtemplates, self.param_maps, self.chan_maps): + self.assertEqual(st.parameter_names, set(pm.values())) + self.assertEqual(st.defined_channels, set(cm.values())) + + def test_channel_intersection(self): + chan_maps = self.chan_maps.copy() + chan_maps[-1]['c3'] = 'cc1' + with self.assertRaises(ChannelMappingException): + MultiChannelPulseTemplate(zip(self.subtemplates, self.param_maps, chan_maps), external_parameters={'pp1', 'pp2', 'pp3'}) + + def test_external_parameter_error(self): + subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] with self.assertRaises(MissingParameterDeclarationException): - MultiChannelPulseTemplate( - [ - (st1, {'foo': "2.3 ** bar"}, [0, 2]), - (st2, {'bar': "bar"}, [1]) - ], - {} - ) - - def test_init_broken_measurement_mappings(self) -> None: - st1 = DummyPulseTemplate(measurement_names={'foo'}, num_channels=2) - st2 = DummyPulseTemplate(num_channels=1) - - with self.assertRaises(UnnecessaryMappingException): - MultiChannelPulseTemplate([SubTemplate(st1, {},measurement_mapping={'asd': 'fdg'},channel_mapping=[0, 2]), - SubTemplate(st2, {},channel_mapping=[1])], {}) - - def test_measurement_names(self) -> None: - st1 = DummyPulseTemplate(num_channels=2,measurement_names={'foo'}) - st2 = DummyPulseTemplate(num_channels=1, measurement_names={'bar'}) - - self.assertEqual( - MultiChannelPulseTemplate([ - SubTemplate(st1, {}, measurement_mapping={'foo': 'mw'}, channel_mapping=[0, 2]), - SubTemplate(st2, {}, measurement_mapping={'bar': 'mw'},channel_mapping=[1]) - ], {}).measurement_names, {'mw'}) - self.assertEqual( - MultiChannelPulseTemplate([ - SubTemplate(st1, {}, measurement_mapping={'foo': 'mw1'}, channel_mapping=[0, 2]), - SubTemplate(st2, {}, measurement_mapping={'bar': 'mw2'}, channel_mapping=[1]) - ], {}).measurement_names, {'mw1','mw2'}) + MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2'}) + with self.assertRaises(MissingMappingException): + MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3', 'foo'}) + + def test_defined_channels(self): + subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] + template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) + self.assertEqual(template.defined_channels, {'cc1', 'cc2', 'cc3'}) + + def test_is_interruptable(self): + subtemp_args = [*zip(self.subtemplates, self.no_param_maps, self.chan_maps)] + + self.assertFalse( + MultiChannelPulseTemplate(subtemp_args, external_parameters=set()).is_interruptable) + + self.assertTrue( + MultiChannelPulseTemplate(subtemp_args[1:], external_parameters=set()).is_interruptable) class MultiChannelPulseTemplateSequencingTests(unittest.TestCase): def test_requires_stop_false_mapped_parameters(self) -> None: dummy = DummyPulseTemplate(parameter_names={'foo'}) - pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), [0]), - (dummy, dict(foo='rab-5'), [1])], + pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), {'default': 'A'}), + (dummy, dict(foo='rab-5'), {'default': 'B'})], {'bar', 'rab'}) self.assertEqual({'bar', 'rab'}, pulse.parameter_names) self.assertEqual({ParameterDeclaration('bar'), ParameterDeclaration('rab')}, pulse.parameter_declarations) + parameters = dict(bar=ConstantParameter(-3.6), rab=ConstantParameter(35.26)) self.assertFalse(pulse.requires_stop(parameters, dict())) def test_requires_stop_true_mapped_parameters(self) -> None: dummy = DummyPulseTemplate(parameter_names={'foo'}, requires_stop=True) - pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), [0]), - (dummy, dict(foo='rab-5'), [1])], + pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), {'default': 'A'}), + (dummy, dict(foo='rab-5'), {'default': 'B'})], {'bar', 'rab'}) self.assertEqual({'bar', 'rab'}, pulse.parameter_names) self.assertEqual({ParameterDeclaration('bar'), ParameterDeclaration('rab')}, @@ -293,35 +190,41 @@ def test_requires_stop_true_mapped_parameters(self) -> None: parameters = dict(bar=ConstantParameter(-3.6), rab=ConstantParameter(35.26)) self.assertTrue(pulse.requires_stop(parameters, dict())) - def test_build_sequence_no_params(self) -> None: - dummy1 = DummyPulseTemplate(parameter_names={'foo'}) - pulse = MultiChannelPulseTemplate([(dummy1, {'foo': '2*bar'}, [1]), - (dummy1, {'foo': '3'}, [0])], {'bar'}) - - self.assertEqual({'bar'}, pulse.parameter_names) - self.assertEqual({ParameterDeclaration('bar')}, pulse.parameter_declarations) - - with self.assertRaises(ParameterNotProvidedException): - pulse.build_waveform({}) - - with self.assertRaises(ParameterNotProvidedException): - pulse.build_sequence(DummySequencer(), dict(), dict(), dict(), DummyInstructionBlock()) - def test_build_sequence(self) -> None: - dummy_wf1 = DummyWaveform(duration=2.3, num_channels=2) - dummy_wf2 = DummyWaveform(duration=2.3, num_channels=1) - dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, waveform=dummy_wf1) - dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1, waveform=dummy_wf2) - - pulse = MultiChannelPulseTemplate([(dummy1, {'foo': '2*bar'}, [2, 1]), - (dummy2, {}, [0])], {'bar'}) - - result = pulse.build_waveform({'bar': ConstantParameter(3)}) - expected = MultiChannelWaveform([(dummy_wf1, [2, 1]), (dummy_wf2, [0])]) - self.assertEqual(expected, result) - self.assertEqual([{'foo': MappedParameter(Expression("2*bar"), {'bar': ConstantParameter(3)})}], dummy1.build_waveform_calls) - self.assertEqual([{}], dummy2.build_waveform_calls) - + dummy_wf1 = DummySingleChannelWaveform(duration=2.3) + dummy_wf2 = DummySingleChannelWaveform(duration=2.3) + dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1) + dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2) + + sequencer = DummySequencer() + pulse = MultiChannelPulseTemplate([dummy1, dummy2], {'bar'}) + + parameters = {'bar': ConstantParameter(3)} + measurement_mapping = {} + channel_mapping = {'A': 'A', 'B': 'B'} + instruction_block = DummyInstructionBlock() + conditions = {} + + pulse.build_sequence(sequencer, parameters=parameters, + conditions=conditions, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + + self.assertEqual(len(instruction_block), 2) + self.assertIsInstance(instruction_block[0], CHANInstruction) + + for chan, sub_block_ptr in instruction_block[0].channel_to_instruction_block.items(): + self.assertIn(chan,('A', 'B')) + if chan == 'A': + self.assertEqual( sequencer.sequencing_stacks[sub_block_ptr.block], + [(dummy1, parameters, conditions, measurement_mapping, channel_mapping)]) + if chan == 'B': + self.assertEqual(sequencer.sequencing_stacks[sub_block_ptr.block], + [(dummy2, parameters, conditions, measurement_mapping, channel_mapping)]) + + + @unittest.skip("Replace when/if there is an AtomicPulseTemplate detection.") def test_integration_table_and_function_template(self) -> None: from qctoolkit.pulses import TablePulseTemplate, FunctionPulseTemplate, Sequencer, EXECInstruction, STOPInstruction @@ -359,68 +262,36 @@ def test_integration_table_and_function_template(self) -> None: self.assertIsInstance(instructions[1], STOPInstruction) -class MutliChannelPulseTemplateSerializationTests(unittest.TestCase): +class MultiChannelPulseTemplateSerializationTests(unittest.TestCase): def __init__(self, methodName) -> None: super().__init__(methodName=methodName) self.maxDiff = None - self.dummy1 = DummyPulseTemplate(parameter_names={'foo'}, num_channels=2, measurement_names={'meas_1'}) - self.dummy2 = DummyPulseTemplate(parameter_names={}, num_channels=1) + self.dummy1 = DummyPulseTemplate(parameter_names={'foo'}, defined_channels={'A'}, measurement_names={'meas_1'}) + self.dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B', 'C'}) def test_get_serialization_data(self) -> None: serializer = DummySerializer( serialize_callback=lambda x: str(x) if isinstance(x, Expression) else str(id(x))) template = MultiChannelPulseTemplate( - [ - SubTemplate(self.dummy1, {'foo': "bar+3"}, measurement_mapping={'meas_1': 'meas_N'}, channel_mapping=[0, 2]), - SubTemplate(self.dummy2, {}, channel_mapping=[1]) - ], - {'bar'}, + [ self.dummy1, self.dummy2 ], + {'foo'}, identifier='herbert' ) expected_data = dict( - external_parameters=['bar'], - subtemplates = [ - dict(template=str(id(self.dummy1)), - parameter_mappings=dict(foo=str(Expression("bar+3"))), - measurement_mapping={'meas_1': 'meas_N'}, - channel_mappings=[0, 2]), - dict(template=str(id(self.dummy2)), - parameter_mappings=dict(), - measurement_mapping={}, - channel_mappings=[1]) - ] - ) + subtemplates = [str(id(self.dummy1)), str(id(self.dummy2))], + type=serializer.get_type_identifier(template)) data = template.get_serialization_data(serializer) self.assertEqual(expected_data, data) def test_deserialize(self) -> None: - exp = Expression("bar - 35") - - data = dict( - external_parameters=['bar'], - subtemplates=[ - dict(template=str(id(self.dummy1)), - parameter_mappings=dict(foo=str(exp)), - measurement_mapping={'meas_1': 'meas_N'}, - channel_mappings=[0, 2]), - dict(template=str(id(self.dummy2)), - parameter_mappings=dict(), - measurement_mapping={}, - channel_mappings=[1]) - ] - ) - serializer = DummySerializer(serialize_callback=lambda x: str(x) if isinstance(x, Expression) else str(id(x))) serializer.subelements[str(id(self.dummy1))] = self.dummy1 serializer.subelements[str(id(self.dummy2))] = self.dummy2 - serializer.subelements[str(exp)] = exp - - template = MultiChannelPulseTemplate.deserialize(serializer, **data) - self.assertEqual(set(data['external_parameters']), template.parameter_names) - self.assertEqual({ParameterDeclaration('bar')}, template.parameter_declarations) - self.assertEqual(template.measurement_names,{'meas_N'}) - recovered_data = template.get_serialization_data(serializer) - self.assertEqual(data, recovered_data) + data = dict( + subtemplates=[str(id(self.dummy1)), str(id(self.dummy2))]) + template = MultiChannelPulseTemplate.deserialize(serializer, **data) + for st_expected, st_found in zip( [self.dummy1, self.dummy2], template.subtemplates ): + self.assertEqual(st_expected,st_found) diff --git a/tests/pulses/plotting_tests.py b/tests/pulses/plotting_tests.py index c0ddcdd4f..22d83bb2a 100644 --- a/tests/pulses/plotting_tests.py +++ b/tests/pulses/plotting_tests.py @@ -7,7 +7,7 @@ from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate from qctoolkit.pulses.sequencing import Sequencer -from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstruction +from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummyInstruction class PlotterTests(unittest.TestCase): @@ -23,8 +23,8 @@ def test_render_no_waveforms(self) -> None: self.assertEqual(([], []), Plotter().render(InstructionBlock())) def test_render(self) -> None: - wf1 = DummyWaveform(duration=19) - wf2 = DummyWaveform(duration=21) + wf1 = DummySingleChannelWaveform(duration=19) + wf2 = DummySingleChannelWaveform(duration=21) block = InstructionBlock() block.add_instruction_exec(wf1) diff --git a/tests/pulses/pulse_template_parameter_mapping_tests.py b/tests/pulses/pulse_template_parameter_mapping_tests.py index 445d40e92..c2e6d2071 100644 --- a/tests/pulses/pulse_template_parameter_mapping_tests.py +++ b/tests/pulses/pulse_template_parameter_mapping_tests.py @@ -1,154 +1,110 @@ import unittest -from qctoolkit.pulses.pulse_template import SubTemplate -from qctoolkit.pulses.pulse_template_parameter_mapping import PulseTemplateParameterMapping, MissingMappingException,\ - UnnecessaryMappingException, MissingParameterDeclarationException, get_measurement_name_mappings +from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ + UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate from qctoolkit.expressions import Expression -from qctoolkit.pulses.parameters import MappedParameter, ParameterNotProvidedException, ConstantParameter +from qctoolkit.pulses.parameters import ParameterNotProvidedException +from qctoolkit.pulses.parameters import ConstantParameter -from tests.pulses.sequencing_dummies import DummyPulseTemplate +from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummySequencer, DummyInstructionBlock -class PulseTemplateParameterMappingTests(unittest.TestCase): +class MappingTemplateTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) - def __init__(self, methodName) -> None: - super().__init__(methodName=methodName) + def test_init(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + parameter_mapping = {'foo': 't*k', 'bar': 't*l'} - def test_empty_init(self) -> None: - map = PulseTemplateParameterMapping() - self.assertEqual(set(), map.external_parameters) - - def test_set_external_parameters(self) -> None: - map = PulseTemplateParameterMapping({'foo'}) - self.assertEqual({'foo'}, map.external_parameters) - map.set_external_parameters(None) - self.assertEqual({'foo'}, map.external_parameters) - map.set_external_parameters({'bar'}) - self.assertEqual({'bar'}, map.external_parameters) - map.set_external_parameters(set()) - self.assertEqual(set(), map.external_parameters) - - def test_add_unnecessary_mapping(self) -> None: - map = PulseTemplateParameterMapping() - dummy = DummyPulseTemplate(parameter_names={'foo'}) + with self.assertRaises(MissingMappingException): + MappingTemplate(template, parameter_mapping={}) + with self.assertRaises(MissingMappingException): + MappingTemplate(template, parameter_mapping={'bar': 'kneipe'}) with self.assertRaises(UnnecessaryMappingException): - map.add(dummy, 'bar', '2') - - def test_add_missing_external_parameter(self) -> None: - map = PulseTemplateParameterMapping() - dummy = DummyPulseTemplate(parameter_names={'foo'}) - with self.assertRaises(MissingParameterDeclarationException): - map.add(dummy, 'foo', 'bar') - - def test_add(self) -> None: - map = PulseTemplateParameterMapping({'bar'}) - dummy1 = DummyPulseTemplate(parameter_names={'foo', 'hugo'}) - dummy2 = DummyPulseTemplate(parameter_names={'grr'}) - map.add(dummy1, 'foo', '4*bar') - map.add(dummy2, 'grr', Expression('bar ** 2')) - map.add(dummy1, 'hugo', '3') - map.add(dummy2, 'grr', Expression('sin(bar)')) - self.assertEqual(dict(foo=Expression('4*bar'), hugo=Expression('3')), map.get_template_map(dummy1)) - self.assertEqual(dict(grr=Expression('sin(bar)')), map.get_template_map(dummy2)) - - def test_get_template_map_no_key(self) -> None: - map = PulseTemplateParameterMapping() - dummy = DummyPulseTemplate() - self.assertEqual(dict(), map.get_template_map(dummy)) - - def test_is_template_mapped(self) -> None: - map = PulseTemplateParameterMapping({'bar'}) - dummy1 = DummyPulseTemplate(parameter_names={'foo', 'hugo'}) - dummy2 = DummyPulseTemplate(parameter_names={'grr'}) - map.add(dummy1, 'foo', '4*bar') - self.assertFalse(map.is_template_mapped(dummy1)) - map.add(dummy1, 'hugo', 'bar + 1') - self.assertTrue(map.is_template_mapped(dummy1)) - self.assertFalse(map.is_template_mapped(dummy2)) - - def test_get_remaining_mappings(self) -> None: - map = PulseTemplateParameterMapping({'bar', 'barbar'}) - dummy = DummyPulseTemplate(parameter_names={'foo', 'hugo'}) - self.assertEqual({'foo', 'hugo'}, map.get_remaining_mappings(dummy)) - map.add(dummy, 'hugo', '4*bar') - self.assertEqual({'foo'}, map.get_remaining_mappings(dummy)) - map.add(dummy, 'foo', Expression('barbar')) - self.assertEqual(set(), map.get_remaining_mappings(dummy)) - - def test_map_parameters_not_provided(self) -> None: - map = PulseTemplateParameterMapping({'bar', 'barbar'}) - dummy = DummyPulseTemplate(parameter_names={'foo', 'hugo'}) - map.add(dummy, 'hugo', '4*bar') - map.add(dummy, 'foo', Expression('barbar')) - with self.assertRaises(ParameterNotProvidedException): - map.map_parameters(dummy, dict(bar=ConstantParameter(3))) - - def test_map_parameters(self) -> None: - map = PulseTemplateParameterMapping({'bar', 'barbar'}) - dummy = DummyPulseTemplate(parameter_names={'foo', 'hugo'}) - map.add(dummy, 'hugo', '4*bar') - map.add(dummy, 'foo', Expression('barbar')) - mapped = map.map_parameters(dummy, dict(bar=ConstantParameter(3), barbar=ConstantParameter(5))) - self.assertEqual(dict( - hugo=MappedParameter(Expression('4*bar'), dict(bar=ConstantParameter(3))), - foo=MappedParameter(Expression('barbar'), dict(barbar=ConstantParameter(5))) - ), mapped) - - -class GetMeasurementNameMappingsTest(unittest.TestCase): - def setUp(self): - self.template = DummyPulseTemplate() - - def test_no_mapping(self): - template = DummyPulseTemplate() - - mappings = get_measurement_name_mappings([SubTemplate(template, dict())]) - self.assertEqual(mappings,[dict()]) - - def test_mapping(self): - template = DummyPulseTemplate() - template.measurement_names_ = set(['foo', 'bar']) - - mapping_arg={'foo': 'hendrix', 'bar': 'shoe'} - mappings = get_measurement_name_mappings([SubTemplate(template,dict(),measurement_mapping=mapping_arg)]) + MappingTemplate(template, dict(**parameter_mapping, foobar='asd')) + MappingTemplate(template, parameter_mapping=parameter_mapping) - self.assertEqual(mappings,[mapping_arg]) - - def test_missing_mapping(self): - template = DummyPulseTemplate() - template.measurement_names_ = set(['foo', 'bar']) - - mapping_arg = {'foo': 'hendrix'} - mappings = get_measurement_name_mappings([SubTemplate(template, dict(), measurement_mapping=mapping_arg)]) - - self.assertEqual(mappings, [dict(**mapping_arg,bar='bar')]) - - def test_unnecessary_mapping(self): - template = DummyPulseTemplate() - template.measurement_names_ = set(['foo', 'bar']) - - mapping_arg = {'foo': 'hendrix', 'my': 'oh_my'} with self.assertRaises(UnnecessaryMappingException): - get_measurement_name_mappings([SubTemplate(template, dict(), measurement_mapping=mapping_arg)]) - - def test_multiple_arguments(self): - template1 = DummyPulseTemplate() - template1.measurement_names_ = set(['foo', 'bar']) - - template2 = DummyPulseTemplate() - template2.measurement_names_ = set(['hurr', 'durr']) - - mappings = get_measurement_name_mappings([SubTemplate(template1,dict(),measurement_mapping=dict()), - SubTemplate(template1,dict(),measurement_mapping={'foo': 'bar'}), - SubTemplate(template2, dict(), measurement_mapping={'hurr': 'bar','durr': 'bar'})]) - self.assertEqual(mappings,[dict(foo='foo',bar='bar'),dict(foo='bar',bar='bar'),{'hurr': 'bar','durr': 'bar'}]) - + MappingTemplate(template, parameter_mapping, measurement_mapping=dict(a='b')) with self.assertRaises(UnnecessaryMappingException): - get_measurement_name_mappings([SubTemplate(template1, dict(), measurement_mapping=dict()), - SubTemplate(template1, dict(), - measurement_mapping={'foo': 'bar'}), - SubTemplate(template2, dict(), - measurement_mapping={'hurr': 'bar', 'durr': 'bar', 'hopfen': 'malz'})]) + MappingTemplate(template, parameter_mapping, channel_mapping=dict(a='b')) + + def test_external_params(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) + external_params = {'t', 'l', 'k'} + self.assertEqual(st.parameter_names, external_params) + + def test_map_parameters(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) + + parameters = {'t': ConstantParameter(3), 'k': ConstantParameter(2), 'l': ConstantParameter(7)} + values = {'foo': 6, 'bar': 21} + for k, v in st.map_parameters(parameters).items(): + self.assertEqual(v.get_value(), values[k]) + parameters.popitem() + with self.assertRaises(ParameterNotProvidedException): + st.map_parameters(parameters) + + def test_get_updated_channel_mapping(self): + template = DummyPulseTemplate(defined_channels={'foo', 'bar'}) + st = MappingTemplate(template, {}, channel_mapping={'bar': 'kneipe'}) + with self.assertRaises(KeyError): + st.get_updated_channel_mapping(dict()) + self.assertEqual(st.get_updated_channel_mapping({'kneipe': 'meas1', 'foo': 'meas2', 'troet': 'meas3'}), + {'foo': 'meas2', 'bar': 'meas1'}) + + def test_measurement_names(self): + template = DummyPulseTemplate(measurement_names={'foo', 'bar'}) + st = MappingTemplate(template, {}, measurement_mapping={'foo': 'froop', 'bar': 'kneipe'}) + self.assertEqual( st.measurement_names, {'froop','kneipe'} ) + + def test_defined_channels(self): + mapping = {'asd': 'A', 'fgh': 'B'} + template = DummyPulseTemplate(defined_channels=set(mapping.keys())) + st = MappingTemplate(template, {}, channel_mapping=mapping) + self.assertEqual(st.defined_channels, set(mapping.values())) + + def test_get_updated_measurement_mapping(self): + template = DummyPulseTemplate(measurement_names={'foo', 'bar'}) + st = MappingTemplate(template, {}, measurement_mapping={'bar': 'kneipe'}) + with self.assertRaises(KeyError): + st.get_updated_measurement_mapping(dict()) + self.assertEqual(st.get_updated_measurement_mapping({'kneipe': 'meas1', 'foo': 'meas2', 'troet': 'meas3'}), + {'foo': 'meas2', 'bar': 'meas1'}) + + def test_build_sequence(self): + measurement_mapping = {'meas1': 'meas2'} + parameter_mapping = {'t': 'k'} + + template = DummyPulseTemplate(measurement_names=set(measurement_mapping.keys()), + parameter_names=set(parameter_mapping.keys())) + st = MappingTemplate(template, parameter_mapping, measurement_mapping=measurement_mapping) + sequencer = DummySequencer() + block = DummyInstructionBlock() + pre_parameters = {'k': ConstantParameter(5)} + pre_measurement_mapping = {'meas2': 'meas3'} + pre_channel_mapping = {'default': 'A'} + conditions = dict(a=True) + st.build_sequence(sequencer, pre_parameters, conditions, pre_measurement_mapping, pre_channel_mapping, block) + + self.assertEqual(template.build_sequence_calls, 1) + forwarded_args = template.build_sequence_arguments[0] + self.assertEqual(forwarded_args[0], sequencer) + self.assertEqual(forwarded_args[1], st.map_parameters(pre_parameters)) + self.assertEqual(forwarded_args[2], conditions) + self.assertEqual(forwarded_args[3], + st.get_updated_measurement_mapping(pre_measurement_mapping)) + self.assertEqual(forwarded_args[4], + st.get_updated_channel_mapping(pre_channel_mapping)) + self.assertEqual(forwarded_args[5], block) + + @unittest.skip("Extend of dummy template for argument checking needed.") + def test_requires_stop(self): + pass + class PulseTemplateParameterMappingExceptionsTests(unittest.TestCase): diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index 4de63b13e..94eef6e3e 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -3,10 +3,11 @@ from typing import Optional, Dict, Set, Any, List from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow -from qctoolkit.pulses.instructions import Waveform, EXECInstruction +from qctoolkit.pulses.instructions import SingleChannelWaveform, EXECInstruction from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -from tests.pulses.sequencing_dummies import DummyWaveform, DummySequencer, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummySequencer, DummyInstructionBlock class AtomicPulseTemplateStub(AtomicPulseTemplate): @@ -14,7 +15,7 @@ class AtomicPulseTemplateStub(AtomicPulseTemplate): def is_interruptable(self) -> bool: return super().is_interruptable() - def __init__(self, waveform: Waveform, measurement_windows: List[MeasurementWindow] = [], + def __init__(self, waveform: SingleChannelWaveform, measurement_windows: List[MeasurementWindow] = [], identifier: Optional[str]=None) -> None: super().__init__(identifier=identifier) self.waveform = waveform @@ -32,7 +33,7 @@ def requires_stop(self, return False @property - def num_channels(self) -> int: + def defined_channels(self) -> Set['ChannelID']: raise NotImplementedError() @property @@ -58,7 +59,7 @@ def deserialize(serializer: 'Serializer', **kwargs) -> 'AtomicPulseTemplateStub' class AtomicPulseTemplateTests(unittest.TestCase): def test_is_interruptable(self) -> None: - wf = DummyWaveform() + wf = DummySingleChannelWaveform() template = AtomicPulseTemplateStub(wf) self.assertFalse(template.is_interruptable()) template = AtomicPulseTemplateStub(wf, identifier="arbg4") @@ -69,14 +70,20 @@ def test_build_sequence_no_waveform(self) -> None: block = DummyInstructionBlock() template = AtomicPulseTemplateStub(None) - template.build_sequence(sequencer, {}, {}, {}, block) + template.build_sequence(sequencer, {}, {}, {}, {}, block) self.assertFalse(block.instructions) def test_build_sequence(self) -> None: - wf = DummyWaveform() + single_wf = DummySingleChannelWaveform(duration=6) + wf = MultiChannelWaveform(dict(A=single_wf)) + measurement_windows = [('M', 0, 5)] sequencer = DummySequencer() block = DummyInstructionBlock() - template = AtomicPulseTemplateStub(wf) - template.build_sequence(sequencer, {}, {}, {}, block) - self.assertEqual([EXECInstruction(wf)], block.instructions) \ No newline at end of file + template = AtomicPulseTemplateStub(wf,measurement_windows) + template.build_sequence(sequencer, {}, {}, measurement_mapping={'M': 'M2'}, channel_mapping={'A': 'B'}, instruction_block=block) + self.assertEqual(len(block.instructions), 1) + self.assertIsInstance(block.instructions[0], EXECInstruction) + self.assertEqual(block.instructions[0].waveform.defined_channels, {'B'}) + self.assertEqual(block.instructions[0].waveform['B'], single_wf) + self.assertEqual(block.instructions[0].measurement_windows, [('M2', 0, 5)]) \ No newline at end of file diff --git a/tests/pulses/repetition_pulse_template_tests.py b/tests/pulses/repetition_pulse_template_tests.py index 83fe1415a..26ecd6877 100644 --- a/tests/pulses/repetition_pulse_template_tests.py +++ b/tests/pulses/repetition_pulse_template_tests.py @@ -87,24 +87,26 @@ def test_build_sequence_constant(self) -> None: parameters = {} measurement_mapping = {'my' : 'thy'} conditions = dict(foo=DummyCondition(requires_stop=True)) - t.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, self.block) + channel_mapping = {} + t.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, channel_mapping, self.block) self.assertTrue(self.block.embedded_blocks) body_block = self.block.embedded_blocks[0] self.assertEqual({body_block}, set(self.sequencer.sequencing_stacks.keys())) - self.assertEqual([(self.body, parameters, conditions, measurement_mapping)], self.sequencer.sequencing_stacks[body_block]) + self.assertEqual([(self.body, parameters, conditions, measurement_mapping, channel_mapping)], self.sequencer.sequencing_stacks[body_block]) self.assertEqual([REPJInstruction(repetitions, InstructionPointer(body_block, 0))], self.block.instructions) def test_build_sequence_declaration_success(self) -> None: parameters = dict(foo=3) conditions = dict(foo=DummyCondition(requires_stop=True)) measurement_mapping = dict(moth='fire') - self.template.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, self.block) + channel_mapping = dict(asd='f') + self.template.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, channel_mapping, self.block) self.assertTrue(self.block.embedded_blocks) body_block = self.block.embedded_blocks[0] self.assertEqual({body_block}, set(self.sequencer.sequencing_stacks.keys())) - self.assertEqual([(self.body, parameters, conditions, measurement_mapping)], + self.assertEqual([(self.body, parameters, conditions, measurement_mapping, channel_mapping)], self.sequencer.sequencing_stacks[body_block]) self.assertEqual([REPJInstruction(parameters['foo'], InstructionPointer(body_block, 0))], self.block.instructions) @@ -113,21 +115,21 @@ def test_build_sequence_declaration_exceeds_bounds(self) -> None: parameters = dict(foo=9) conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterValueIllegalException): - self.template.build_sequence(self.sequencer, parameters, conditions, {}, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, {}, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) def test_build_sequence_declaration_parameter_missing(self) -> None: parameters = {} conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterNotProvidedException): - self.template.build_sequence(self.sequencer, parameters, conditions, {}, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, {}, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) def test_build_sequence_declaration_parameter_value_not_whole(self) -> None: parameters = dict(foo=3.3) conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterNotIntegerException): - self.template.build_sequence(self.sequencer, parameters, conditions, {}, self.block) + self.template.build_sequence(self.sequencer, parameters, conditions, {}, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index d577eafcc..0e5e589bd 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -1,11 +1,11 @@ import unittest import copy -from qctoolkit.pulses.pulse_template import DoubleParameterNameException, SubTemplate +from qctoolkit.pulses.pulse_template import DoubleParameterNameException from qctoolkit.expressions import Expression from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate -from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException +from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ConstantParameter from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyNoValueParameter @@ -32,7 +32,7 @@ def __init__(self, *args, **kwargs) -> None: self.window_name_mapping = {'mw1' : 'test_window'} - self.outer_parameters = ['uptime', 'length', 'pulse_length', 'voltage'] + self.outer_parameters = {'uptime', 'length', 'pulse_length', 'voltage'} self.parameters = {} self.parameters['uptime'] = ConstantParameter(5) @@ -40,7 +40,7 @@ def __init__(self, *args, **kwargs) -> None: self.parameters['pulse_length'] = ConstantParameter(100) self.parameters['voltage'] = ConstantParameter(10) - self.sequence = SequencePulseTemplate([SubTemplate(self.square, self.mapping1, measurement_mapping=self.window_name_mapping)], self.outer_parameters) + self.sequence = SequencePulseTemplate([MappingTemplate(self.square, self.mapping1, measurement_mapping=self.window_name_mapping)], self.outer_parameters) def test_missing_mapping(self) -> None: mapping = self.mapping1 @@ -60,21 +60,25 @@ def test_unnecessary_mapping(self) -> None: def test_identifier(self) -> None: identifier = 'some name' - pulse = SequencePulseTemplate([], [], identifier=identifier) + pulse = SequencePulseTemplate([DummyPulseTemplate()], {}, identifier=identifier) self.assertEqual(identifier, pulse.identifier) def test_multiple_channels(self) -> None: - dummy = DummyPulseTemplate(parameter_names={'hugo'}, num_channels=2) + dummy = DummyPulseTemplate(parameter_names={'hugo'}, defined_channels={'A', 'B'}) subtemplates = [(dummy, {'hugo': 'foo'}, {}), (dummy, {'hugo': '3'}, {})] sequence = SequencePulseTemplate(subtemplates, {'foo'}) - self.assertEqual(2, sequence.num_channels) + self.assertEqual({'A', 'B'}, sequence.defined_channels) def test_multiple_channels_mismatch(self) -> None: - dummy1 = DummyPulseTemplate(num_channels=6) - dummy2 = DummyPulseTemplate(num_channels=5) - subtemplates = [(dummy1, dict(), dict()), (dummy2, dict(), dict())] with self.assertRaises(ValueError): - SequencePulseTemplate(subtemplates, set()) + SequencePulseTemplate( + [DummyPulseTemplate(defined_channels={'A'}), DummyPulseTemplate(defined_channels={'B'})] + , set()) + + with self.assertRaises(ValueError): + SequencePulseTemplate( + [DummyPulseTemplate(defined_channels={'A'}), DummyPulseTemplate(defined_channels={'A', 'B'})] + , set()) class SequencePulseTemplateSerializationTests(unittest.TestCase): @@ -93,69 +97,43 @@ def setUp(self) -> None: self.table = TablePulseTemplate() def test_get_serialization_data(self) -> None: + dummy1 = DummyPulseTemplate() + dummy2 = DummyPulseTemplate() + + sequence = SequencePulseTemplate([dummy1, dummy2], []) serializer = DummySerializer(serialize_callback=lambda x: str(x)) - foo_mappings = {k: Expression(v) for k, v in self.foo_param_mappings.items()} - sequence = SequencePulseTemplate([SubTemplate(self.table_foo, self.foo_param_mappings, self.foo_meas_mappings), (self.table, {})], - ['ilse', 'albert', 'voltage'], - identifier='foo') + expected_data = dict( type=serializer.get_type_identifier(sequence), - external_parameters=['albert', 'ilse', 'voltage'], - is_interruptable=True, - subtemplates = [ - dict(template=str(self.table_foo), - parameter_mappings={k: str(v) for k, v in foo_mappings.items()}, - measurement_mappings=self.foo_meas_mappings), - dict(template=str(self.table), parameter_mappings=dict(), measurement_mappings=dict()) - ] + subtemplates = [str(dummy1), str(dummy2)] ) data = sequence.get_serialization_data(serializer) self.assertEqual(expected_data, data) def test_deserialize(self) -> None: - foo_param_mappings = {k: Expression(v) for k, v in self.foo_param_mappings.items()} + dummy1 = DummyPulseTemplate() + dummy2 = DummyPulseTemplate() + + serializer = DummySerializer(serialize_callback=lambda x: str(id(x))) + data = dict( - external_parameters={'ilse', 'albert', 'voltage'}, - is_interruptable=True, - subtemplates = [ - dict(template=str(id(self.table_foo)), - parameter_mappings={k: str(id(v)) for k, v in foo_param_mappings.items()}, - measurement_mappings=self.foo_meas_mappings), - dict(template=str(id(self.table)), parameter_mappings=dict(),measurement_mappings=dict()) - ], + subtemplates = [serializer.dictify(dummy1), serializer.dictify(dummy2)], identifier='foo' ) - # prepare dependencies for deserialization - self.serializer.subelements[str(id(self.table_foo))] = self.table_foo - self.serializer.subelements[str(id(self.table))] = self.table - for v in foo_param_mappings.values(): - self.serializer.subelements[str(id(v))] = v - - # deserialize - sequence = SequencePulseTemplate.deserialize(self.serializer, **data) - - # compare! - self.assertEqual(data['external_parameters'], sequence.parameter_names) - self.assertEqual({ParameterDeclaration('ilse'), ParameterDeclaration('albert'), ParameterDeclaration('voltage')}, - sequence.parameter_declarations) - self.assertIs(self.table_foo, sequence.subtemplates[0][0]) - self.assertIs(self.table, sequence.subtemplates[1][0]) - #self.assertEqual(self.foo_mappings, {k: m.string for k,m in sequence.subtemplates[0][1].items()}) - self.assertEqual(foo_param_mappings, sequence.subtemplates[0][1]) - self.assertEqual(dict(), sequence.subtemplates[1][1]) - self.assertEqual(data['identifier'], sequence.identifier) + template = SequencePulseTemplate.deserialize(serializer,**data) + self.assertEqual(template.subtemplates, [dummy1, dummy2]) class SequencePulseTemplateSequencingTests(SequencePulseTemplateTest): - + @unittest.skip("The test for missing parameters is performed on the lowest level.") def test_missing_parameter(self): sequencer = DummySequencer() block = DummyInstructionBlock() parameters = copy.deepcopy(self.parameters) parameters.pop('uptime') with self.assertRaises(ParameterNotProvidedException): - self.sequence.build_sequence(sequencer, parameters, {}, {}, block) + self.sequence.build_sequence(sequencer, parameters, {}, {}, {}, block) def test_build_sequence(self) -> None: sub1 = DummyPulseTemplate(requires_stop=False) @@ -165,23 +143,21 @@ def test_build_sequence(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() seq = SequencePulseTemplate([(sub1, {}, {}), (sub2, {'foo': 'foo'}, {})], {'foo'}) - seq.build_sequence(sequencer, parameters, {}, {}, block) + seq.build_sequence(sequencer, parameters, {}, {}, {}, block) self.assertEqual(2, len(sequencer.sequencing_stacks[block])) sequencer = DummySequencer() block = DummyInstructionBlock() seq = SequencePulseTemplate([(sub2, {'foo': 'foo'}, {}), (sub1, {}, {})], {'foo'}) - seq.build_sequence(sequencer, parameters, {}, {}, block) + seq.build_sequence(sequencer, parameters, {}, {}, {}, block) self.assertEqual(2, len(sequencer.sequencing_stacks[block])) + @unittest.skip("Was this test faulty before? Why should the three last cases return false?") def test_requires_stop(self) -> None: sub1 = (DummyPulseTemplate(requires_stop=False), {}, {}) sub2 = (DummyPulseTemplate(requires_stop=True, parameter_names={'foo'}), {'foo': 'foo'}, {}) parameters = {'foo': DummyNoValueParameter()} - seq = SequencePulseTemplate([],[]) - self.assertFalse(seq.requires_stop(parameters, {})) - seq = SequencePulseTemplate([sub1], {}) self.assertFalse(seq.requires_stop(parameters, {})) @@ -237,10 +213,10 @@ def test_crash(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() self.assertFalse(sequence.requires_stop(parameters, {})) - sequence.build_sequence(sequencer, parameters, {}, {}, block) + sequence.build_sequence(sequencer, parameters, {}, {}, {'default', 'default'}, block) from qctoolkit.pulses.sequencing import Sequencer s = Sequencer() - s.push(sequence, parameters) + s.push(sequence, parameters, channel_mapping={'default': 'EXAMPLE_A'}) s.build() def test_missing_parameter_declaration_exception(self) -> None: @@ -254,45 +230,79 @@ def test_missing_parameter_declaration_exception(self) -> None: class SequencePulseTemplateTestProperties(SequencePulseTemplateTest): def test_is_interruptable(self): - self.assertTrue(self.sequence.is_interruptable) - self.sequence.is_interruptable = False - self.assertFalse(self.sequence.is_interruptable) + + self.assertTrue( + SequencePulseTemplate([DummyPulseTemplate(is_interruptable=True), + DummyPulseTemplate(is_interruptable=True)], []).is_interruptable) + self.assertTrue( + SequencePulseTemplate([DummyPulseTemplate(is_interruptable=True), + DummyPulseTemplate(is_interruptable=False)], []).is_interruptable) + self.assertFalse( + SequencePulseTemplate([DummyPulseTemplate(is_interruptable=False), + DummyPulseTemplate(is_interruptable=False)], []).is_interruptable) def test_parameter_declarations(self): decl = self.sequence.parameter_declarations self.assertEqual(decl, set([ParameterDeclaration(i) for i in self.outer_parameters])) -class SequencePulseTemplateConcatenationTest(SequencePulseTemplateTest): +class PulseTemplateConcatenationTest(unittest.TestCase): + def __init__(self,*args,**kwargs): + super().__init__(*args,**kwargs) + + def test_concatenation_pulse_template(self): + a = DummyPulseTemplate(parameter_names={'foo'}, defined_channels={'A'}) + b = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}) + c = DummyPulseTemplate(parameter_names={'snu'}, defined_channels={'A'}) + d = DummyPulseTemplate(parameter_names={'snu'}, defined_channels={'A'}) + + seq = a @ a + self.assertTrue(len(seq.subtemplates) == 2) + for st in seq.subtemplates: + self.assertEqual(st, a) + + seq = a @ b + self.assertTrue(len(seq.subtemplates)==2) + for st, expected in zip(seq.subtemplates,[a, b]): + self.assertTrue(st, expected) + + with self.assertRaises(DoubleParameterNameException): + a @ b @ a + with self.assertRaises(DoubleParameterNameException): + a @ b @ c @ d + + seq = a @ b @ c + self.assertTrue(len(seq.subtemplates) == 3) + for st, expected in zip(seq.subtemplates, [a, b, c]): + self.assertTrue(st, expected) + def test_concatenation_sequence_table_pulse(self): - a = TablePulseTemplate() - a.add_entry('t', 'a') - b = TablePulseTemplate() - b.add_entry('t', 'a') - - subtemplates = [(b, {'t': 't_ext', 'a': 'a_ext'})] - seq = SequencePulseTemplate(subtemplates, ['t_ext', 'a_ext']) - - concat = seq @ a - subtemplates2 = [(seq, {'t_ext':'t_ext', 'a_ext': 'a_ext'}), - (a, {'t': 't', 'a':'a'})] - seq2 = SequencePulseTemplate(subtemplates2, ['a', 'a_ext', 't', 't_ext']) - self.assertEqual(concat.parameter_names, seq2.parameter_names) - self.assertEqual(concat.subtemplates, seq2.subtemplates) - #self.assertEqual(concat, seq2) - - def test_concatenation_sequence_table_pulse_double_parameter(self): - a = TablePulseTemplate() - a.add_entry('t', 'a') - b = TablePulseTemplate() - b.add_entry('t', 'a') - - subtemplates = [(b, {'t': 't', 'a': 'a'})] - seq = SequencePulseTemplate(subtemplates, ['t', 'a']) - - with self.assertRaises(DoubleParameterNameException) as e: - concat = seq @ a + a = DummyPulseTemplate(parameter_names={'foo'}, defined_channels={'A'}) + b = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}) + c = DummyPulseTemplate(parameter_names={'snu'}, defined_channels={'A'}) + d = DummyPulseTemplate(parameter_names={'snu'}, defined_channels={'A'}) + + seq1 = SequencePulseTemplate([a, b], ['foo', 'bar']) + seq2 = SequencePulseTemplate([c, d], ['snu']) + + seq = seq1 @ c + self.assertTrue(len(seq.subtemplates) == 3) + for st, expected in zip(seq.subtemplates,[a, b, c]): + self.assertTrue(st, expected) + + seq = c @ seq1 + self.assertTrue(len(seq.subtemplates) == 3) + for st, expected in zip(seq.subtemplates, [c, a, b]): + self.assertTrue(st, expected) + + seq = seq1 @ seq2 + self.assertTrue(len(seq.subtemplates) == 4) + for st, expected in zip(seq.subtemplates, [a, b, c, d]): + self.assertTrue(st, expected) + + with self.assertRaises(DoubleParameterNameException): + seq2 @ c if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index 2c23c5c6f..d1d41ed8b 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -5,13 +5,13 @@ """LOCAL IMPORTS""" from qctoolkit.serialization import Serializer -from qctoolkit.pulses.instructions import Waveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction +from qctoolkit.pulses.instructions import SingleChannelWaveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock, SequencingElement from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow from qctoolkit.pulses.interpolation import InterpolationStrategy from qctoolkit.pulses.conditions import Condition - +from qctoolkit.pulses.multi_channel_pulse_template import ChannelID class DummyParameter(Parameter): @@ -63,6 +63,7 @@ def __init__(self, requires_stop: bool = False, push_elements: Tuple[Instruction self.parameters = None self.conditions = None self.window_mapping = None + self.channel_mapping = None self.requires_stop_ = requires_stop self.push_elements = push_elements self.parameter_names = set() @@ -73,6 +74,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Optional[Dict[str, str]], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: self.build_call_counter += 1 self.target_block = instruction_block @@ -80,9 +82,10 @@ def build_sequence(self, self.parameters = parameters self.conditions = conditions self.window_mapping = measurement_mapping + self.channel_mapping = channel_mapping if self.push_elements is not None: for element in self.push_elements[1]: - sequencer.push(element, parameters, conditions, measurement_mapping, self.push_elements[0]) + sequencer.push(element, parameters, conditions, measurement_mapping, channel_mapping, self.push_elements[0]) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Conditions']) -> bool: self.requires_stop_call_counter += 1 @@ -114,7 +117,7 @@ def add_instruction(self, instruction: Instruction) -> None: self.embedded_blocks.append(instruction.target.block) -class DummyWaveform(Waveform): +class DummySingleChannelWaveform(SingleChannelWaveform): def __init__(self, duration: float=0, sample_output: numpy.ndarray=None, num_channels: int=1) -> None: super().__init__() @@ -160,6 +163,7 @@ def push(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], window_mapping: Optional[Dict[str, str]] = None, + channel_mapping: Dict['ChannelID', 'ChannelID'] = dict(), target_block: InstructionBlock = None) -> None: if target_block is None: target_block = self.__main_block @@ -167,7 +171,8 @@ def push(self, if target_block not in self.sequencing_stacks: self.sequencing_stacks[target_block] = [] - self.sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping)) + self.sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping, + channel_mapping)) def build(self) -> InstructionBlock: raise NotImplementedError() @@ -207,6 +212,7 @@ def build_sequence_loop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: self.loop_call_data = dict( delegator=delegator, @@ -215,6 +221,7 @@ def build_sequence_loop(self, parameters=parameters, conditions=conditions, measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, instruction_block=instruction_block ) @@ -226,6 +233,7 @@ def build_sequence_branch(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: self.branch_call_data = dict( delegator=delegator, @@ -235,6 +243,7 @@ def build_sequence_branch(self, parameters=parameters, conditions=conditions, measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, instruction_block=instruction_block ) @@ -245,16 +254,18 @@ def __init__(self, requires_stop: bool=False, is_interruptable: bool=False, parameter_names: Set[str]={}, - num_channels: int=1, + defined_channels: Set[ChannelID]={'default'}, duration: float=0, - waveform: Waveform=None, + waveform: SingleChannelWaveform=None, measurement_names: Set[str] = set()) -> None: super().__init__() self.requires_stop_ = requires_stop + self.requires_stop_arguments = [] + self.is_interruptable_ = is_interruptable self.parameter_names_ = parameter_names - self.build_sequence_calls = 0 - self.num_channels_ = num_channels + self.build_sequence_arguments = [] + self.defined_channels_ = defined_channels self.duration = duration self.waveform = waveform self.build_waveform_calls = [] @@ -272,13 +283,17 @@ def get_measurement_windows(self, parameters: Dict[str, Parameter] = None) -> Li """Return all measurement windows defined in this PulseTemplate.""" raise NotImplementedError() + @property + def build_sequence_calls(self): + return len(self.build_sequence_arguments) + @property def is_interruptable(self) -> bool: return self.is_interruptable_ @property - def num_channels(self) -> int: - return self.num_channels_ + def defined_channels(self) -> Set[ChannelID]: + return self.defined_channels_ @property def measurement_names(self) -> Set[str]: @@ -289,16 +304,18 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock): - self.build_sequence_calls += 1 + self.build_sequence_arguments.append((sequencer,parameters,conditions, measurement_mapping, channel_mapping, instruction_block)) - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[Waveform]: + def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[SingleChannelWaveform]: self.build_waveform_calls.append(parameters) if self.waveform is not None: return self.waveform - return DummyWaveform(duration=self.duration, num_channels=self.num_channels) + return DummySingleChannelWaveform(duration=self.duration, num_channels=self.num_channels) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition]) -> bool: + self.requires_stop_arguments.append((parameters,conditions)) return self.requires_stop_ def get_serialization_data(self, serializer: Serializer): diff --git a/tests/pulses/sequencing_tests.py b/tests/pulses/sequencing_tests.py index 76b09d010..18e8b0f43 100644 --- a/tests/pulses/sequencing_tests.py +++ b/tests/pulses/sequencing_tests.py @@ -95,12 +95,13 @@ def test_build_path_o1_m2_i1_f_i1_f_one_element_custom_and_main_block_requires_s ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} wm = {} + cm = {} elem_main = DummySequencingElement(True) - sequencer.push(elem_main, ps, cs) + sequencer.push(elem_main, ps, cs, cm) elem_cstm = DummySequencingElement(True) target_block = InstructionBlock() - sequencer.push(elem_cstm, ps, cs, wm, target_block) + sequencer.push(elem_cstm, ps, cs, wm, cm, target_block) sequencer.build() @@ -250,11 +251,12 @@ def test_build_path_o2_m2_i0_i1_t_m2_i0_i0_one_element_custom_block(self) -> Non ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - wm = {'foo' : 'bar'} + wm = {'foo': 'bar'} + cm = {'A': 'B'} target_block = InstructionBlock() elem = DummySequencingElement(False) - sequencer.push(elem, ps, cs, wm, target_block) + sequencer.push(elem, ps, cs, wm, cm, target_block=target_block) sequencer.build() @@ -262,6 +264,8 @@ def test_build_path_o2_m2_i0_i1_t_m2_i0_i0_one_element_custom_block(self) -> Non self.assertIs(target_block, elem.target_block) self.assertEqual(ps, elem.parameters) self.assertEqual(cs, elem.conditions) + self.assertEqual(wm, elem.window_mapping) + self.assertEqual(cm, elem.channel_mapping) self.assertEqual(1, elem.requires_stop_call_counter) self.assertEqual(1, elem.build_call_counter) @@ -271,14 +275,13 @@ def test_build_path_o2_m2_i1_f_i1_t_m2_i1_f_i0_one_element_custom_block_one_elem ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - wm = {'foo': 'bar'} elem_main = DummySequencingElement(True) sequencer.push(elem_main, ps, cs) target_block = InstructionBlock() elem_cstm = DummySequencingElement(False) - sequencer.push(elem_cstm, ps, cs, wm, target_block) + sequencer.push(elem_cstm, ps, cs, target_block=target_block) sequencer.build() @@ -298,14 +301,13 @@ def test_build_path_o2_m2_i1_t_i1_t_m2_i0_i0_one_element_custom_block_one_elemen ps = {'foo': ConstantParameter(1), 'bar': ConstantParameter(7.3)} cs = {'foo': DummyCondition()} - wm = {'foo': 'bar'} elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) target_block = InstructionBlock() elem_cstm = DummySequencingElement(False) - sequencer.push(elem_cstm, ps, cs, wm, target_block) + sequencer.push(elem_cstm, ps, cs, target_block=target_block) sequence = sequencer.build() @@ -332,10 +334,10 @@ def test_build_path_o2_m2_i0_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_req target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) sequencer.build() @@ -359,10 +361,10 @@ def test_build_path_o2_m2_i0_i2_tt_m2_i0_i0_two_elements_custom_block(self) -> N target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) sequencer.build() @@ -387,10 +389,10 @@ def test_build_path_o2_m2_i1_f_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_last target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) elem_main = DummySequencingElement(True) sequencer.push(elem_main, ps, cs) @@ -421,10 +423,10 @@ def test_build_path_o2_m2_i1_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_r target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) @@ -458,10 +460,10 @@ def test_build_path_o2_m2_i1_t_i2_tt_m2_i0_i0_two_elements_custom_block_one_elem target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) @@ -496,10 +498,10 @@ def test_build_path_o2_m2_i2_tf_t_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_l target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) elem_main2 = DummySequencingElement(True) sequencer.push(elem_main2, ps, cs) @@ -541,10 +543,10 @@ def test_build_path_o2_m2_i2_tt_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_las target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) @@ -586,10 +588,10 @@ def test_build_path_o2_m2_i2_tt_t_i2_tt_m2_i0_i0_two_elements_custom_block_two_e target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, wm, target_block) + sequencer.push(elem2, ps, cs, wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block) + sequencer.push(elem1, ps, cs, wm, target_block=target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 465788daa..721faac15 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -2,9 +2,10 @@ import numpy from qctoolkit.pulses.instructions import EXECInstruction -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableSingleChannelWaveform, TableEntry, WaveformTableEntry from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyInterpolationStrategy, DummyParameter, DummyCondition from tests.serialization_dummies import DummySerializer @@ -18,7 +19,7 @@ def test_add_entry_known_interpolation_strategies(self) -> None: for i,strategy in enumerate(strategies): table.add_entry(i, i, strategy) - manual = [[(0,0,LinearInterpolationStrategy()), (1,1,HoldInterpolationStrategy()), (2,2,JumpInterpolationStrategy())]] + manual = [(0,0,LinearInterpolationStrategy()), (1,1,HoldInterpolationStrategy()), (2,2,JumpInterpolationStrategy())] self.assertEqual(manual, table.entries) def test_add_entry_unknown_interpolation_strategy(self) -> None: @@ -67,7 +68,7 @@ def test_add_entry_empty_time_is_negative(self) -> None: table = TablePulseTemplate() with self.assertRaises(ValueError): table.add_entry(-2, 0) - self.assertEqual([[TableEntry(0, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([TableEntry(0, 0, HoldInterpolationStrategy())], table.entries) #self.assertFalse(table.entries[0]) self.assertFalse(table.parameter_declarations) self.assertFalse(table.parameter_names) @@ -75,7 +76,7 @@ def test_add_entry_empty_time_is_negative(self) -> None: def test_add_entry_empty_time_is_0(self) -> None: table = TablePulseTemplate() table.add_entry(0, 3.1) - self.assertEqual([[(0, 3.1, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 3.1, HoldInterpolationStrategy())], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations) @@ -83,14 +84,14 @@ def test_add_entry_empty_time_is_0_voltage_is_parameter(self) -> None: table = TablePulseTemplate() table.add_entry(0, 'foo') decl = ParameterDeclaration('foo') - self.assertEqual([[(0, decl, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, decl, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) def test_add_entry_empty_time_is_positive(self) -> None: table = TablePulseTemplate() table.add_entry(2, -254.67) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (2, -254.67, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (2, -254.67, HoldInterpolationStrategy())], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations) @@ -98,7 +99,7 @@ def test_add_entry_empty_time_is_str(self) -> None: table = TablePulseTemplate() table.add_entry('t', 0) decl = ParameterDeclaration('t', min=0) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (decl, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 0, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -107,7 +108,7 @@ def test_add_entry_empty_time_is_declaration(self) -> None: decl = ParameterDeclaration('foo') table.add_entry(decl, 0) decl.min_value = 0 - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (decl, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 0, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -120,7 +121,7 @@ def test_add_entry_time_float_after_float(self) -> None: # adding a higher value or equal value as last entry should work table.add_entry(1.2, 2.5252) table.add_entry(3.7, 1.34875) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (1.2, 2.5252, HoldInterpolationStrategy()), (3.7, 1.34875, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (1.2, 2.5252, HoldInterpolationStrategy()), (3.7, 1.34875, HoldInterpolationStrategy())], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations) @@ -129,7 +130,7 @@ def test_add_entry_time_float_after_declaration_no_bound(self) -> None: table.add_entry('t', 7.1) table.add_entry(2.1, 5.5) decl = ParameterDeclaration('t', min=0, max=2.1) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -139,7 +140,7 @@ def test_add_entry_time_float_after_declaration_greater_bound(self) -> None: table.add_entry(decl, 7.1) table.add_entry(2.1, 5.5) expected_decl = ParameterDeclaration('t', min=0, max=2.1) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (expected_decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (expected_decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({expected_decl}, table.parameter_declarations) @@ -148,7 +149,7 @@ def test_add_entry_time_float_after_declaration_smaller_bound(self) -> None: decl = ParameterDeclaration('t', min=1.0, max=1.3) table.add_entry(decl, 7.1) table.add_entry(2.1, 5.5) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -158,7 +159,7 @@ def test_add_entry_time_float_after_declaration_smaller_than_min_bound(self) -> table.add_entry(decl, 2.2) with self.assertRaises(ValueError): table.add_entry(1.1, -6.3) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (decl, 2.2, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 2.2, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -172,7 +173,7 @@ def test_add_entry_time_parameter_name_in_use_as_voltage(self) -> None: table.add_entry('foo', 4.3) self.assertEqual({'foo'}, table.parameter_names) self.assertEqual({foo_decl}, table.parameter_declarations) - self.assertEqual([[(0, foo_decl, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, foo_decl, HoldInterpolationStrategy())], table.entries) def test_add_entry_time_parmeter_name_in_use_as_time(self) -> None: table = TablePulseTemplate() @@ -181,7 +182,7 @@ def test_add_entry_time_parmeter_name_in_use_as_time(self) -> None: bar_decl = ParameterDeclaration('bar') with self.assertRaises(ValueError): table.add_entry(ParameterDeclaration('foo'), 3.4) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (foo_decl, bar_decl, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (foo_decl, bar_decl, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo', 'bar'}, table.parameter_names) self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) @@ -194,7 +195,7 @@ def test_add_entry_time_declaration_invalid_bounds(self) -> None: table.add_entry(foo_decl, 23857.23) with self.assertRaises(ValueError): table.add_entry(bar_decl, -4967.1) - self.assertEquals([[TableEntry(0, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEquals([TableEntry(0, 0, HoldInterpolationStrategy())], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations) @@ -203,7 +204,7 @@ def test_add_entry_time_declaration_no_bounds_after_float(self) -> None: table.add_entry(3.2, 92.1) table.add_entry('t', 1.2) decl = ParameterDeclaration('t', min=3.2) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy()), (decl, 1.2, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy()), (decl, 1.2, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -212,7 +213,7 @@ def test_add_entry_time_declaration_higher_min_after_float(self) -> None: table.add_entry(3.2, 92.1) decl = ParameterDeclaration('t', min=4.5) table.add_entry(decl, 1.2) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy()), (decl, 1.2, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy()), (decl, 1.2, HoldInterpolationStrategy())], table.entries) self.assertEqual({'t'}, table.parameter_names) self.assertEqual({decl}, table.parameter_declarations) @@ -222,7 +223,7 @@ def test_add_entry_time_declaration_lower_min_after_float(self) -> None: decl = ParameterDeclaration('t', min=0.1) with self.assertRaises(ValueError): table.add_entry(decl, 1.2) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy())], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations) @@ -233,7 +234,7 @@ def test_add_entry_time_declaration_after_declaration_no_upper_bound(self) -> No bar_decl = ParameterDeclaration('bar', min=0) foo_decl = ParameterDeclaration('foo') foo_decl.min_value = bar_decl - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, 72.14, HoldInterpolationStrategy()), (foo_decl, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, 72.14, HoldInterpolationStrategy()), (foo_decl, 0, HoldInterpolationStrategy())], table.entries) self.assertEqual({'bar', 'foo'}, table.parameter_names) self.assertEqual({bar_decl, foo_decl}, table.parameter_declarations) @@ -244,7 +245,7 @@ def test_add_entry_time_declaration_after_declaration_upper_bound(self) -> None: table.add_entry(bar_decl, -3) table.add_entry(foo_decl, 0.1) foo_decl.min_value = bar_decl - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo', 'bar'}, table.parameter_names) self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) @@ -254,7 +255,7 @@ def test_add_entry_time_declaration_lower_bound_after_declaration_upper_bound(se foo_decl = ParameterDeclaration('foo', min=1) table.add_entry(bar_decl, -3) table.add_entry(foo_decl, 0.1) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo', 'bar'}, table.parameter_names) self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) @@ -266,7 +267,7 @@ def test_add_entry_time_declaration_lower_bound_after_declaration_no_upper_bound table.add_entry(bar_decl, -3) table.add_entry(foo_decl, 0.1) bar_decl.max_value = foo_decl - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo', 'bar'}, table.parameter_names) self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) @@ -277,7 +278,7 @@ def test_add_entry_time_declaration_lower_bound_too_small_after_declaration_no_u table.add_entry(bar_decl, -3) with self.assertRaises(ValueError): table.add_entry(foo_decl, 0.1) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())], table.entries) self.assertEqual({'bar'}, table.parameter_names) self.assertEqual({bar_decl}, table.parameter_declarations) @@ -288,7 +289,7 @@ def test_add_entry_time_declaration_no_lower_bound_upper_bound_too_small_after_d table.add_entry(bar_decl, -3) with self.assertRaises(ValueError): table.add_entry(foo_decl, 0.1) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())], table.entries) self.assertEqual({'bar'}, table.parameter_names) self.assertEqual({bar_decl}, table.parameter_declarations) @@ -299,7 +300,7 @@ def test_add_entry_time_declaration_lower_bound_upper_bound_too_small_after_decl table.add_entry(bar_decl, -3) with self.assertRaises(ValueError): table.add_entry(foo_decl, 0.1) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())], table.entries) self.assertEqual({'bar'}, table.parameter_names) self.assertEqual({bar_decl}, table.parameter_declarations) @@ -312,8 +313,8 @@ def test_add_entry_voltage_declaration_reuse(self) -> None: table.add_entry(3, 'foo') table.add_entry('t', foo_decl) t_decl = ParameterDeclaration('t', min=3) - self.assertEqual([[(0, foo_decl, HoldInterpolationStrategy()), (1.51, bar_decl, HoldInterpolationStrategy()), - (3, foo_decl, HoldInterpolationStrategy()), (t_decl, foo_decl, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, foo_decl, HoldInterpolationStrategy()), (1.51, bar_decl, HoldInterpolationStrategy()), + (3, foo_decl, HoldInterpolationStrategy()), (t_decl, foo_decl, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo', 'bar', 't'}, table.parameter_names) self.assertEqual({foo_decl, bar_decl, t_decl}, table.parameter_declarations) @@ -323,7 +324,7 @@ def test_add_entry_voltage_declaration_in_use_as_time(self) -> None: table.add_entry(foo_decl, 0) with self.assertRaises(ValueError): table.add_entry(4, foo_decl) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (foo_decl, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (foo_decl, 0, HoldInterpolationStrategy())], table.entries) self.assertEqual({'foo'}, table.parameter_names) self.assertEqual({foo_decl}, table.parameter_declarations) @@ -331,7 +332,7 @@ def test_add_entry_time_and_voltage_same_declaration(self) -> None: table = TablePulseTemplate() with self.assertRaises(ValueError): table.add_entry('foo', 'foo') - self.assertEquals([[TableEntry(0, 0, HoldInterpolationStrategy())]], table.entries) + self.assertEquals([TableEntry(0, 0, HoldInterpolationStrategy())], table.entries) self.assertFalse(table.parameter_names) self.assertFalse(table.parameter_declarations) @@ -341,27 +342,27 @@ def test_is_interruptable(self) -> None: def test_get_entries_instantiated_one_entry_float_float(self) -> None: table = TablePulseTemplate() table.add_entry(0, 2) - instantiated_entries = table.get_entries_instantiated({}) - self.assertEqual([[(0, 2, HoldInterpolationStrategy())]], instantiated_entries) + instantiated_entries = table.get_entries_instantiated({})['default'] + self.assertEqual([(0, 2, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_one_entry_float_declaration(self) -> None: table = TablePulseTemplate() table.add_entry(0, 'foo') - instantiated_entries = table.get_entries_instantiated({'foo': 2}) - self.assertEqual([[(0, 2, HoldInterpolationStrategy())]], instantiated_entries) + instantiated_entries = table.get_entries_instantiated({'foo': 2})['default'] + self.assertEqual([(0, 2, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_two_entries_float_float_declaration_float(self) -> None: table = TablePulseTemplate() table.add_entry('foo', -3.1415) - instantiated_entries = table.get_entries_instantiated({'foo': 2}) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (2, -3.1415, HoldInterpolationStrategy())]], instantiated_entries) + instantiated_entries = table.get_entries_instantiated({'foo': 2})['default'] + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (2, -3.1415, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_two_entries_float_declaraton_declaration_declaration(self) -> None: table = TablePulseTemplate() table.add_entry(0, 'v1') table.add_entry('t', 'v2') - instantiated_entries = table.get_entries_instantiated({'v1': -5, 'v2': 5, 't': 3}) - self.assertEqual([[(0, -5, HoldInterpolationStrategy()), (3, 5, HoldInterpolationStrategy())]], instantiated_entries) + instantiated_entries = table.get_entries_instantiated({'v1': -5, 'v2': 5, 't': 3})['default'] + self.assertEqual([(0, -5, HoldInterpolationStrategy()), (3, 5, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_two_entries_invalid_parameters(self) -> None: table = TablePulseTemplate() @@ -388,8 +389,8 @@ def test_get_entries_instantiated_linked_time_declarations(self) -> None: bar_decl = ParameterDeclaration('bar') table.add_entry(foo_decl, 'v', 'linear') table.add_entry(bar_decl, 0, 'jump') - instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4}) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy())]], instantiated_entries) + instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4})['default'] + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy())], instantiated_entries) with self.assertRaises(Exception): table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 1}) with self.assertRaises(Exception): @@ -401,14 +402,14 @@ def test_get_entries_instantiated_unlinked_time_declarations(self) -> None: bar_decl = ParameterDeclaration('bar', min=1.5, max=4) table.add_entry(foo_decl, 'v', 'linear') table.add_entry(bar_decl, 0, 'jump') - instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4}) - self.assertEqual([[(0, 0, HoldInterpolationStrategy()), (1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy())]], instantiated_entries) + instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4})['default'] + self.assertEqual([(0, 0, HoldInterpolationStrategy()), (1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy())], instantiated_entries) with self.assertRaises(Exception): table.get_entries_instantiated({'v': 2.3, 'foo': 2, 'bar': 1.5}) def test_get_entries_instantiated_empty(self) -> None: table = TablePulseTemplate() - self.assertEquals([[(0, 0, HoldInterpolationStrategy())]], table.get_entries_instantiated({})) + self.assertEquals([(0, 0, HoldInterpolationStrategy())], table.get_entries_instantiated({})['default']) def test_get_entries_instantiated_two_equal_entries(self) -> None: table = TablePulseTemplate() @@ -416,13 +417,13 @@ def test_get_entries_instantiated_two_equal_entries(self) -> None: table.add_entry(1, 5) table.add_entry(3, 5) table.add_entry(5, 1) - entries = table.get_entries_instantiated({}) - expected = [[ + entries = table.get_entries_instantiated({})['default'] + expected = [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 5, HoldInterpolationStrategy()), TableEntry(3, 5, HoldInterpolationStrategy()), TableEntry(5, 1, HoldInterpolationStrategy()) - ]] + ] self.assertEqual(expected, entries) def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries(self) -> None: @@ -431,13 +432,13 @@ def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries(sel table.add_entry(1.5, 5) table.add_entry(2, 5) table.add_entry(3, 0) - entries = table.get_entries_instantiated({}) - expected = [[ + entries = table.get_entries_instantiated({})['default'] + expected = [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 5, HoldInterpolationStrategy()), TableEntry(2, 5, HoldInterpolationStrategy()), TableEntry(3, 0, HoldInterpolationStrategy()) - ]] + ] self.assertEqual(expected, entries) def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries_does_not_destroy_linear_interpolation(self) -> None: @@ -447,38 +448,38 @@ def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries_doe table.add_entry(5, 5) table.add_entry(10, 0, 'linear') - entries = table.get_entries_instantiated({}) + entries = table.get_entries_instantiated({})['default'] - expected = [[ + expected = [ TableEntry(0, 5, HoldInterpolationStrategy()), TableEntry(5, 5, HoldInterpolationStrategy()), TableEntry(10, 0, LinearInterpolationStrategy()) - ]] + ] self.assertEqual(expected, entries) - result_sampled = TableWaveform(entries).sample(numpy.linspace(0, 10, 11), 0) + result_sampled = TableSingleChannelWaveform(entries).sample(numpy.linspace(0, 10, 11), 0) numbers = [5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0] - expected = [[float(x) for x in numbers]] + expected = [float(x) for x in numbers] self.assertEqual(expected, result_sampled.tolist()) def test_get_entries_instantiated_two_channels_one_empty(self) -> None: - table = TablePulseTemplate(channels=2) - table.add_entry('foo', 4) + table = TablePulseTemplate(channels=['A','B']) + table.add_entry('foo', 4, channel='A') parameters = {'foo': 10} entries = table.get_entries_instantiated(parameters) - expected = [ - [ + expected = { + 'A': [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(10, 4, HoldInterpolationStrategy()), ], - [ + 'B': [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(10, 0, HoldInterpolationStrategy()) ] - ] + } self.assertEqual(expected, entries) @@ -486,20 +487,21 @@ def test_from_array_1D(self) -> None: times = numpy.array([0, 1, 3]) voltages = numpy.array([5, 0, 5]) pulse = TablePulseTemplate.from_array(times, voltages) - entries = [[]] + entries = [] for (time, voltage) in zip(times, voltages): - entries[0].append(TableEntry(time, voltage, HoldInterpolationStrategy())) + entries.append(TableEntry(time, voltage, HoldInterpolationStrategy())) self.assertEqual(entries, pulse.entries) def test_from_array_multi(self) -> None: times = numpy.array([0, 1, 3]) voltages = numpy.array([[1,2,3], [2,3,4]]).T # todo: why transposed?? - pulse = TablePulseTemplate.from_array(times, voltages) - entries = [[],[]] - for i, channel in enumerate(voltages.T): - for (time, voltage) in zip(times, channel): - entries[i].append(TableEntry(time, voltage, HoldInterpolationStrategy())) + pulse = TablePulseTemplate.from_array(times, voltages, [0, 1]) + entries = { + i: [TableEntry(time, voltage, HoldInterpolationStrategy()) + for (time, voltage) in zip(times, channel_voltage)] + for i, channel_voltage in enumerate(voltages.T)} + self.assertEqual(entries, pulse.entries) def test_add_entry_multi_invalid_channel(self) -> None: @@ -508,20 +510,20 @@ def test_add_entry_multi_invalid_channel(self) -> None: pulse.add_entry(2,2, channel=1) def test_add_entry_multi(self) -> None: - pulse = TablePulseTemplate(channels=2) + pulse = TablePulseTemplate(channels=[0, 1]) pulse.add_entry(1,1, channel=0) pulse.add_entry(1,1, channel=1) - entries = [[(0,0,HoldInterpolationStrategy()), + entries = {0: [(0,0,HoldInterpolationStrategy()), (1,1,HoldInterpolationStrategy())], - [(0,0,HoldInterpolationStrategy()), - (1,1,HoldInterpolationStrategy())]] + 1: [(0,0,HoldInterpolationStrategy()), + (1,1,HoldInterpolationStrategy())]} self.assertEqual(entries, pulse.entries) def test_add_entry_multi_same_time_param(self) -> None: - pulse = TablePulseTemplate(channels=2) - pulse.add_entry(1, 3) - pulse.add_entry('foo', 'bar') - pulse.add_entry(7, 3) + pulse = TablePulseTemplate(channels=[0, 1]) + pulse.add_entry(1, 3, channel=0) + pulse.add_entry('foo', 'bar', channel=0) + pulse.add_entry(7, 3, channel=0) pulse.add_entry(0, -5, channel=1) pulse.add_entry(0.5, -2, channel=1) @@ -530,23 +532,23 @@ def test_add_entry_multi_same_time_param(self) -> None: expected_foo = ParameterDeclaration('foo', min=1, max=5) expected_bar = ParameterDeclaration('bar') - entries = [[TableEntry(0, 0, HoldInterpolationStrategy()), + entries = {0: [TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(expected_foo, expected_bar, HoldInterpolationStrategy()), TableEntry(7, 3, HoldInterpolationStrategy())], - [TableEntry(0, -5, HoldInterpolationStrategy()), + 1: [TableEntry(0, -5, HoldInterpolationStrategy()), TableEntry(0.5, -2, HoldInterpolationStrategy()), TableEntry(expected_foo, 0, HoldInterpolationStrategy()), - TableEntry(5, expected_bar, HoldInterpolationStrategy())]] + TableEntry(5, expected_bar, HoldInterpolationStrategy())]} self.assertEqual(entries, pulse.entries) self.assertEqual({'foo', 'bar'}, pulse.parameter_names) self.assertEqual({expected_bar, expected_foo}, pulse.parameter_declarations) def test_get_instantiated_entries_multi_same_time_param(self) -> None: - table = TablePulseTemplate(channels=2) - table.add_entry(1, 3) - table.add_entry('foo', 'bar') - table.add_entry(7, 3) + table = TablePulseTemplate(channels=[0, 1]) + table.add_entry(1, 3, channel=0) + table.add_entry('foo', 'bar', channel=0) + table.add_entry(7, 3, channel=0) table.add_entry(0, -5, channel=1) table.add_entry(0.5, -2, channel=1) @@ -557,26 +559,26 @@ def test_get_instantiated_entries_multi_same_time_param(self) -> None: entries = table.get_entries_instantiated(parameters) - expected = [ - [ + expected = { + 0: [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(2.7, -3.3, HoldInterpolationStrategy()), TableEntry(7, 3, HoldInterpolationStrategy()), ], - [ + 1: [ TableEntry(0, -5, HoldInterpolationStrategy()), TableEntry(0.5, -2, HoldInterpolationStrategy()), TableEntry(2.7, 0, HoldInterpolationStrategy()), TableEntry(5, -3.3, HoldInterpolationStrategy()), TableEntry(7, -3.3, HoldInterpolationStrategy()) ] - ] + } self.assertEqual(expected, entries) def test_get_instaniated_entries_multi_one_empty_channel(self) -> None: - table = TablePulseTemplate(channels=2) + table = TablePulseTemplate(channels=[0, 1]) table.add_entry(1, 3, channel=1) table.add_entry('foo', 'bar', 'linear', channel=1) @@ -584,25 +586,25 @@ def test_get_instaniated_entries_multi_one_empty_channel(self) -> None: entries = table.get_entries_instantiated(parameters) - expected = [ - [ + expected = { + 0: [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(5.2, 0, HoldInterpolationStrategy()) ], - [ + 1: [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(5.2, -83.8, LinearInterpolationStrategy()) ] - ] + } self.assertEqual(expected, entries) def test_measurement_windows_multi(self) -> None: - pulse = TablePulseTemplate(channels=2) - pulse.add_entry(1, 1) - pulse.add_entry(3, 0) - pulse.add_entry(5, 0) + pulse = TablePulseTemplate(channels=[0, 1]) + pulse.add_entry(1, 1, channel=0) + pulse.add_entry(3, 0, channel=0) + pulse.add_entry(5, 0, channel=0) pulse.add_entry(1, 1, channel=1) pulse.add_entry(3, 0, channel=1) @@ -613,10 +615,10 @@ def test_measurement_windows_multi(self) -> None: self.assertEqual([('mw',1,7)], windows) def test_measurement_windows_multi_out_of_pulse(self) -> None: - pulse = TablePulseTemplate(channels=2) - pulse.add_entry(1, 1) - pulse.add_entry(3, 0) - pulse.add_entry(5, 0) + pulse = TablePulseTemplate(channels=[0, 1]) + pulse.add_entry(1, 1, channel=0) + pulse.add_entry(3, 0, channel=0) + pulse.add_entry(5, 0, channel=0) pulse.add_entry(1, 1, channel=1) pulse.add_entry(3, 0, channel=1) @@ -631,18 +633,22 @@ class TablePulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: self.serializer = DummySerializer(lambda x: dict(name=x.name), lambda x: x.name, lambda x: x['name']) - self.template = TablePulseTemplate(identifier='foo') + self.template = TablePulseTemplate(identifier='foo', channels=['A', 'B']) self.expected_data = dict(type=self.serializer.get_type_identifier(self.template)) + self.maxDiff = None def test_get_serialization_data(self) -> None: - self.template.add_entry('foo', 2) - self.template.add_entry('hugo', 'ilse', interpolation='linear') + self.template.add_entry('foo', 2, channel='A') + self.template.add_entry('hugo', 'ilse', interpolation='linear',channel='A') + + self.template.add_entry(2, 2, channel='B', interpolation='jump') + self.template.add_measurement_declaration('mw',2,'hugo+franz') self.expected_data['measurement_declarations'] = {'mw': [(2,'hugo+franz')]} self.expected_data['time_parameter_declarations'] = [dict(name=name) for name in sorted(['foo','hugo','franz'])] self.expected_data['voltage_parameter_declarations'] = [dict(name='ilse')] - self.expected_data['entries'] = [[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')]] + self.expected_data['entries'] = dict(A=[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')], B=[(0, 0, 'hold'), (2, 2, 'jump')]) data = self.template.get_serialization_data(self.serializer) self.assertEqual(self.expected_data, data) @@ -651,7 +657,7 @@ def test_deserialize(self) -> None: data = dict(measurement_declarations={'mw': [(2,'hugo+franz')]}, time_parameter_declarations=[dict(name='hugo'), dict(name='foo'), dict(name='franz')], voltage_parameter_declarations=[dict(name='ilse')], - entries=[[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')]], + entries=dict(default=[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')]), identifier='foo') # prepare dependencies for deserialization @@ -669,9 +675,9 @@ def test_deserialize(self) -> None: all_declarations = set(self.serializer.subelements.values()) # prepare expected entries - entries = [[(0, 0, HoldInterpolationStrategy()), + entries = [(0, 0, HoldInterpolationStrategy()), (self.serializer.subelements['foo'], 2, HoldInterpolationStrategy()), - (self.serializer.subelements['hugo'], self.serializer.subelements['ilse'], LinearInterpolationStrategy())]] + (self.serializer.subelements['hugo'], self.serializer.subelements['ilse'], LinearInterpolationStrategy())] # compare! self.assertEqual(all_declarations, template.parameter_declarations) @@ -689,18 +695,21 @@ def test_build_sequence(self) -> None: table.add_entry(foo_decl, 'v', 'linear') table.add_entry(bar_decl, 0, 'jump') parameters = {'v': 2.3, 'foo': 1, 'bar': 4} - instantiated_entries = tuple(table.get_entries_instantiated(parameters)) + instantiated_entries = table.get_entries_instantiated(parameters) waveform = table.build_waveform(parameters) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, {}, instruction_block) - expected_waveform = TableWaveform(instantiated_entries) + channel_mapping = {'default': 'default'} + table.build_sequence(sequencer, parameters, {}, {}, channel_mapping, instruction_block) + expected_waveform = MultiChannelWaveform({channel: TableSingleChannelWaveform(inst) + for channel,inst in instantiated_entries.items()}) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) self.assertEqual(expected_waveform, waveform) + @unittest.skip("What exactly is the point of allowing empty/non-existent waveforms?") def test_build_sequence_empty(self) -> None: table = TablePulseTemplate() sequencer = DummySequencer() @@ -739,72 +748,91 @@ def test_identifier(self) -> None: self.assertEqual(pulse.identifier, identifier) def test_build_sequence_multi(self) -> None: - table = TablePulseTemplate(channels=2) - table.add_entry(1, 3) - table.add_entry('foo', 'bar') - table.add_entry(7, 3) + table = TablePulseTemplate(channels=['A', 'B']) + table.add_entry(1, 3, channel='A') + table.add_entry('foo', 'bar', channel='A') + table.add_entry(7, 3, channel='A') - table.add_entry(0, -5, channel=1) - table.add_entry('foo', 0, channel=1) - table.add_entry(5, 'bar', channel=1) + table.add_entry(0, -5, channel='B') + table.add_entry('foo', 0, channel='B') + table.add_entry(5, 'bar', channel='B') parameters = {'foo': 3, 'bar': 17} + channel_mapping = {'A': 'CHA', 'B': 'CHB'} instantiated_entries = table.get_entries_instantiated(parameters) + expected_waveform = MultiChannelWaveform( + {('CH' + channel): TableSingleChannelWaveform(instantiated) for channel, instantiated in + instantiated_entries.items()}) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, {}, instruction_block) - expected_waveform = TableWaveform(instantiated_entries) + table.build_sequence(sequencer, parameters, {}, {}, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) waveform = table.build_waveform(parameters) - self.assertEqual(expected_waveform, waveform) + for ch in waveform.defined_channels: + self.assertEqual(expected_waveform['CH' + ch], waveform[ch]) def test_build_sequence_multi_one_channel_empty(self) -> None: - table = TablePulseTemplate(channels=2) - table.add_entry('foo', 4) + table = TablePulseTemplate(channels={'A', 'B'}) + table.add_entry('foo', 4, channel='A') parameters = {'foo': 3} + channel_mapping = {'A': 'CHA', 'B': 'CHB'} instantiated_entries = table.get_entries_instantiated(parameters) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, {}, instruction_block) - expected_waveform = TableWaveform(instantiated_entries) + table.build_sequence(sequencer, parameters, + conditions={}, + measurement_mapping={}, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + expected_waveform = MultiChannelWaveform({('CH'+channel):TableSingleChannelWaveform(instantiated) for channel, instantiated in instantiated_entries.items()}) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) waveform = table.build_waveform(parameters) - self.assertEqual(expected_waveform, waveform) + for ch in waveform.defined_channels: + self.assertEqual(expected_waveform['CH' + ch], waveform[ch]) class TableWaveformDataTests(unittest.TestCase): def test_duration(self) -> None: - entries = [[WaveformTableEntry(0, 0, HoldInterpolationStrategy()), WaveformTableEntry(5, 1, HoldInterpolationStrategy())]] - waveform = TableWaveform(entries) + entries = [WaveformTableEntry(0, 0, HoldInterpolationStrategy()), WaveformTableEntry(5, 1, HoldInterpolationStrategy())] + waveform = TableSingleChannelWaveform(entries) self.assertEqual(5, waveform.duration) + @unittest.skip("What is the point of empty waveforms?") def test_duration_no_entries(self) -> None: - waveform = TableWaveform([]) + waveform = TableSingleChannelWaveform([]) self.assertEqual(0, waveform.duration) + def test_duration_no_entries_exception(self) -> None: + with self.assertRaises(ValueError): + waveform = TableSingleChannelWaveform([]) + self.assertEqual(0, waveform.duration) + def test_few_entries(self) -> None: with self.assertRaises(ValueError): - TableWaveform([[]]) + TableSingleChannelWaveform([[]]) with self.assertRaises(ValueError): - TableWaveform([[WaveformTableEntry(0, 0, HoldInterpolationStrategy())]]) + TableSingleChannelWaveform([WaveformTableEntry(0, 0, HoldInterpolationStrategy())]) def test_sample(self) -> None: interp = DummyInterpolationStrategy() - entries = [[WaveformTableEntry(0, 0, interp), + entries = [WaveformTableEntry(0, 0, interp), WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)]] - waveform = TableWaveform(entries) + WaveformTableEntry(5.7, 123.4, interp)] + waveform = TableSingleChannelWaveform(entries) sample_times = numpy.linspace(98.5, 103.5, num=11) offset = 0.5 @@ -815,51 +843,6 @@ def test_sample(self) -> None: expected_result = [sample_times - 98] self.assertTrue(numpy.all(expected_result == result)) - def test_few_entries_multi(self) -> None: - with self.assertRaises(ValueError): - TableWaveform([[],[WaveformTableEntry(0, 0, HoldInterpolationStrategy())]]) - - with self.assertRaises(ValueError): - TableWaveform([[WaveformTableEntry(0, 0, HoldInterpolationStrategy())],[]]) - - def test_sample_multi(self) -> None: - interp = DummyInterpolationStrategy() - entries = [[WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)], - [WaveformTableEntry(0,0,interp), - WaveformTableEntry(2.1,-33.2,interp), - WaveformTableEntry(5.7, 123.4, interp)]] - waveform = TableWaveform(entries) - sample_times = numpy.linspace(98.5, 103.5, num=11) - expected_result = numpy.vstack([sample_times, sample_times]) - 98 - offset = 0.5 - result = waveform.sample(sample_times, offset) - expected_data = [((0, 0), (2.1, -33.2), [0.5, 1.0, 1.5, 2.0]), - ((2.1, -33.2), (5.7, 123.4), [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5]),((0, 0), (2.1, -33.2), [0.5, 1.0, 1.5, 2.0]), - ((2.1, -33.2), (5.7, 123.4), [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5])] - self.assertEqual(expected_data, interp.call_arguments) - self.assertTrue(numpy.all(expected_result == result)) - - def test_num_channels(self) -> None: - waveform = TableWaveform([]) - self.assertEqual(0, waveform.num_channels) - - interp = DummyInterpolationStrategy() - entries = [[WaveformTableEntry(0, 0, interp), - WaveformTableEntry(5, 1, interp)]] - waveform = TableWaveform(entries) - self.assertEqual(1, waveform.num_channels) - - entries = [[WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)], - [WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)]] - waveform = TableWaveform(entries) - self.assertEqual(2, waveform.num_channels) - class ParameterValueIllegalExceptionTest(unittest.TestCase): diff --git a/tests/pulses/table_sequence_sequencer_intergration_tests.py b/tests/pulses/table_sequence_sequencer_intergration_tests.py index 82a097082..01b18d870 100644 --- a/tests/pulses/table_sequence_sequencer_intergration_tests.py +++ b/tests/pulses/table_sequence_sequencer_intergration_tests.py @@ -1,11 +1,11 @@ import unittest -from qctoolkit.pulses.pulse_template import SubTemplate +from qctoolkit.pulses.multi_channel_pulse_template import MappingTemplate from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate from qctoolkit.pulses.parameters import ParameterNotProvidedException from qctoolkit.pulses.sequencing import Sequencer -from qctoolkit.pulses.instructions import EXECInstruction +from qctoolkit.pulses.instructions import EXECInstruction, AbstractInstructionBlock from tests.pulses.sequencing_dummies import DummyParameter, DummyNoValueParameter @@ -24,19 +24,21 @@ def test_table_sequence_sequencer_integration(self) -> None: t2.add_entry(5, 0) t2.add_measurement_declaration('foo', 4, 5) - seqt = SequencePulseTemplate([SubTemplate(t1, {'foo': 'foo'}, measurement_mapping={'foo': 'bar'}), - SubTemplate(t2, {'bar': '2 * hugo'})], {'foo', 'hugo'}) + seqt = SequencePulseTemplate([MappingTemplate(t1, {'foo': 'foo'}, measurement_mapping={'foo': 'bar'}), + MappingTemplate(t2, {'bar': '2 * hugo'})], {'foo', 'hugo'}) with self.assertRaises(ParameterNotProvidedException): t1.requires_stop(dict(), dict()) with self.assertRaises(ParameterNotProvidedException): t2.requires_stop(dict(), dict()) - self.assertFalse(seqt.requires_stop({}, {})) + self.assertFalse(seqt.requires_stop({'foo': DummyParameter(), 'hugo': DummyParameter()}, {})) foo = DummyNoValueParameter() bar = DummyNoValueParameter() sequencer = Sequencer() - sequencer.push(seqt, {'foo': foo, 'hugo': bar}, window_mapping=dict(bar='my', foo='thy')) + sequencer.push(seqt, {'foo': foo, 'hugo': bar}, + window_mapping=dict(bar='my', foo='thy'), + channel_mapping={'default': 'A'}) instructions = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertEqual(1, len(instructions)) @@ -44,15 +46,20 @@ def test_table_sequence_sequencer_integration(self) -> None: foo = DummyParameter(value=1.1) bar = DummyNoValueParameter() sequencer = Sequencer() - sequencer.push(seqt, {'foo': foo, 'hugo': bar}, window_mapping=dict(bar='my', foo='thy')) + sequencer.push(seqt, {'foo': foo, 'hugo': bar}, + window_mapping=dict(bar='my', foo='thy'), + channel_mapping={'default': 'A'}) instructions = sequencer.build() self.assertFalse(sequencer.has_finished()) + self.assertIsInstance(instructions, AbstractInstructionBlock) self.assertEqual(2, len(instructions)) foo = DummyParameter(value=1.1) bar = DummyNoValueParameter() sequencer = Sequencer() - sequencer.push(seqt, {'foo': bar, 'hugo': foo}, window_mapping=dict(bar='my', foo='thy')) + sequencer.push(seqt, {'foo': bar, 'hugo': foo}, + window_mapping=dict(bar='my', foo='thy'), + channel_mapping={'default': 'A'}) instructions = sequencer.build() self.assertFalse(sequencer.has_finished()) self.assertEqual(1, len(instructions)) @@ -60,7 +67,9 @@ def test_table_sequence_sequencer_integration(self) -> None: foo = DummyParameter(value=1.1) bar = DummyParameter(value=-0.2) sequencer = Sequencer() - sequencer.push(seqt, {'foo': foo, 'hugo': bar}, window_mapping=dict(bar='my', foo='thy')) + sequencer.push(seqt, {'foo': foo, 'hugo': bar}, + window_mapping=dict(bar='my', foo='thy'), + channel_mapping={'default': 'A'}) instructions = sequencer.build() self.assertTrue(sequencer.has_finished()) self.assertEqual(3, len(instructions)) From d2274c6c78aacbba4727cc4336e634d0fb5feded Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 17 Jan 2017 19:27:10 +0100 Subject: [PATCH 012/116] Working Tabor control flow upload. TODO check if the uploaded pulse data is valid. First draft for setup class and DAC --- qctoolkit/hardware/__init__.py | 3 +- qctoolkit/hardware/awgs/__init__.py | 6 +- qctoolkit/hardware/awgs/{awg.py => base.py} | 2 +- qctoolkit/hardware/awgs/tabor.py | 263 ++++++++++++++ qctoolkit/hardware/awgs/tektronix.py | 8 +- qctoolkit/hardware/dacs/__init__.py | 22 ++ qctoolkit/hardware/program.py | 331 ++++++++++++++++++ qctoolkit/hardware/setup.py | 89 +++++ qctoolkit/hardware/util.py | 24 ++ qctoolkit/pulses/instructions.py | 27 +- .../pulses/multi_channel_pulse_template.py | 4 +- qctoolkit/pulses/pulse_template.py | 2 +- qctoolkit/qcmatlab/pulse_control.py | 8 +- setup.py | 12 +- tests/hardware/__init__.py | 0 tests/hardware/program_tests.py | 297 ++++++++++++++++ tests/hardware/tabor_tests.py | 69 ++++ tests/qcmatlab/pulse_control_tests.py | 10 +- 18 files changed, 1147 insertions(+), 30 deletions(-) rename qctoolkit/hardware/awgs/{awg.py => base.py} (99%) create mode 100644 qctoolkit/hardware/awgs/tabor.py create mode 100644 qctoolkit/hardware/program.py create mode 100644 qctoolkit/hardware/setup.py create mode 100644 qctoolkit/hardware/util.py create mode 100644 tests/hardware/__init__.py create mode 100644 tests/hardware/program_tests.py create mode 100644 tests/hardware/tabor_tests.py diff --git a/qctoolkit/hardware/__init__.py b/qctoolkit/hardware/__init__.py index 5d61ff09e..633d1eafc 100644 --- a/qctoolkit/hardware/__init__.py +++ b/qctoolkit/hardware/__init__.py @@ -1,4 +1,5 @@ __all__ = [ 'awgs', - 'dacs' + 'dacs', + 'program' ] diff --git a/qctoolkit/hardware/awgs/__init__.py b/qctoolkit/hardware/awgs/__init__.py index df3606d49..7c66c21ba 100644 --- a/qctoolkit/hardware/awgs/__init__.py +++ b/qctoolkit/hardware/awgs/__init__.py @@ -1,4 +1,6 @@ +from qctoolkit.hardware.awgs.base import AWG, DummyAWG + __all__ = [ - 'awg', - 'tektronix' + 'AWG', + 'DummyAWG' ] diff --git a/qctoolkit/hardware/awgs/awg.py b/qctoolkit/hardware/awgs/base.py similarity index 99% rename from qctoolkit/hardware/awgs/awg.py rename to qctoolkit/hardware/awgs/base.py index 2b1dac998..2d5f3aced 100644 --- a/qctoolkit/hardware/awgs/awg.py +++ b/qctoolkit/hardware/awgs/base.py @@ -13,7 +13,7 @@ from qctoolkit.pulses.instructions import InstructionSequence, EXECInstruction __all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", - "OutOfWaveformMemoryExecption"] + "OutOfWaveformMemoryException"] Program = InstructionSequence diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py new file mode 100644 index 000000000..9244e4a88 --- /dev/null +++ b/qctoolkit/hardware/awgs/tabor.py @@ -0,0 +1,263 @@ +from qctoolkit.pulses.pulse_template import ChannelID +from qctoolkit.hardware.program import Loop, MultiChannelProgram +from qctoolkit.hardware.util import voltage_to_uint16 + +import sys +import numpy as np +from typing import List, Tuple, Iterable + +# Provided by Tabor electronics for python 2.7 +# a python 3 version is in a private repository on https://git.rwth-aachen.de/qutech +# Beware of the string encoding change! +import pytabor +import teawg + +assert(sys.byteorder == 'little') + + +class TaborProgram: + WAVEFORM_MODES = ('single', 'advanced', 'sequence') + + def __init__(self, program: MultiChannelProgram, device_properties, channels: Tuple[ChannelID, ChannelID]): + if len(channels) > 2: + raise Exception('TaborProgram only supports 2 channels') + channel_set = frozenset(channel for channel in channels if channel is not None) + self.__root_loop = None + for known_channels in program.programs.keys(): + if known_channels.issuperset(channel_set): + self.__root_loop = program.programs[known_channels] + if self.__root_loop is None: + raise TaborException("{} not found in program.".format(channel_set)) + + self.__waveform_mode = 'advanced' + self.__channels = channels + self.__device_properties = device_properties + + self.__waveforms = [] + self.__sequencer_tables = [] + self.__advanced_sequencer_table = [] + + if self.program.depth() == 0: + self.setup_single_waveform_mode() + elif self.program.depth() == 1: + self.setup_single_sequence_table_mode() + else: + self.setup_advanced_sequence_mode() + + def setup_single_waveform_mode(self): + raise NotImplementedError() + + def setup_single_sequence_mode(self): + self.__waveform_mode = 'sequence' + if len(self.program) < self.__device_properties['min_seq_len']: + raise TaborException('SEQuence:LENGth has to be >={min_seq_len}'.format(**self.__device_properties)) + raise NotImplementedError() + + def setup_advanced_sequence_mode(self): + while self.program.depth() > 2 or not self.program.is_balanced(): + for i, sequence_table in enumerate(self.program): + if sequence_table.depth() == 0: + sequence_table.encapsulate() + elif sequence_table.depth() == 1: + assert (sequence_table.is_balanced()) + elif len(sequence_table) == 1 and len(sequence_table[0]) == 1: + sequence_table.join_loops() + elif sequence_table.is_balanced(): + if len(self.program) < self.__device_properties['min_aseq_len'] or (sequence_table.repetition_count / self.__device_properties['max_aseq_len'] < + max(entry.repetition_count for entry in sequence_table) / + self.__device_properties['max_seq_len']): + sequence_table.unroll() + else: + for entry in sequence_table.children: + entry.unroll() + + else: + depth_to_unroll = sequence_table.depth() - 1 + for entry in sequence_table: + if entry.depth() == depth_to_unroll: + entry.unroll() + + i = 0 + while i < len(self.program): + sequence_table = self.program[i] + if len(sequence_table) > self.__device_properties['max_seq_len']: + raise TaborException() + elif len(sequence_table) < self.__device_properties['min_seq_len']: + # try to merge with neighbours + if sequence_table.repetition_count == 1: + if i > 0 and self.program[i-1].repetition_count == 1: + self.program[i-1][len(self.program[i-1]):] = sequence_table[:] + self.program[i:i+1] = [] + elif i+1 < len(self.program) and self.program[i+1].repetition_count == 1: + self.program[i+1][:0] = sequence_table[:] + self.program[i:i+1] = [] + else: + self.increase_sequence_table_length(sequence_table, self.__device_properties) + i += 1 + else: + self.increase_sequence_table_length(sequence_table, self.__device_properties) + i += 1 + else: + i += 1 + + assert (self.program.repetition_count == 1) + if len(self.program) < self.__device_properties['min_aseq_len']: + raise TaborException() + if len(self.program) > self.__device_properties['max_aseq_len']: + raise TaborException() + for sequence_table in self.program: + if len(sequence_table) < self.__device_properties['min_seq_len']: + raise TaborException() + if len(sequence_table) > self.__device_properties['max_seq_len']: + raise TaborException() + + advanced_sequencer_table = [] + sequencer_tables = [] + waveforms = [] + for sequencer_table_loop in self.program: + current_sequencer_table = [] + for waveform_loop in sequencer_table_loop: + if waveform_loop.instruction.waveform in waveforms: + segment_no = waveforms.index(waveform_loop.instruction.waveform) + 1 + else: + segment_no = len(waveforms) + 1 + waveforms.append(waveform_loop.instruction.waveform) + current_sequencer_table.append((waveform_loop.repetition_count, segment_no, 0)) + + if current_sequencer_table in sequencer_tables: + sequence_no = sequencer_tables.index(current_sequencer_table) + 1 + else: + sequence_no = len(sequencer_tables) + 1 + sequencer_tables.append(current_sequencer_table) + + advanced_sequencer_table.append((sequencer_table_loop.repetition_count, sequence_no, 0)) + + self.__advanced_sequencer_table = advanced_sequencer_table + self.__sequencer_tables = sequencer_tables + self.__waveforms = waveforms + + @property + def program(self): + return self.__root_loop + + def get_sequencer_tables(self): + return self.__sequencer_tables + + @staticmethod + def increase_sequence_table_length(sequence_table: Loop, device_properties): + assert(sequence_table.depth() == 1) + if len(sequence_table) < device_properties['min_seq_len']: + + if sum(entry.repetition_count for entry in sequence_table)*sequence_table.repetition_count >= device_properties['min_seq_len']: + if sum(entry.repetition_count for entry in sequence_table) < device_properties['min_seq_len']: + sequence_table.unroll_children() + while len(sequence_table) < device_properties['min_seq_len']: + sequence_table.split_one_child() + else: + TaborException('Sequence table too short: ', sequence_table) + + def get_advanced_sequencer_table(self): + """Advanced sequencer table that can be used via the download_adv_seq_table pytabor command""" + return self.__advanced_sequencer_table + + def get_waveform_data(self, device_properties, samplerate: float, voltage_amplitude: Tuple[float, float], voltage_offset: Tuple[float, float]): + if any(not(waveform.duration*samplerate).is_integer() for waveform in self.__waveforms): + raise TaborException('At least one waveform has a length that is no multiple of the time per sample') + maximal_length = int(max(waveform.duration for waveform in self.__waveforms) * samplerate) + time_array = np.arange(0, maximal_length, 1) + maximal_size = int(2 * (samplerate*sum(waveform.duration for waveform in self.__waveforms) + 16*len(self.__waveforms))) + data = np.empty(maximal_size, dtype=np.uint16) + offset = 0 + segment_lengths = np.zeros(len(self.__waveforms), dtype=np.uint32) + + def voltage_to_data(waveform, time, channel): + return voltage_to_uint16(waveform[self.__channels[channel]].sample(time), + voltage_amplitude[channel], + voltage_offset[channel], + resolution=14) if self.__channels[channel] else None + + for i, waveform in enumerate(self.__waveforms): + t = time_array[:int(waveform.duration*samplerate)] + segment1 = voltage_to_data(waveform, t, 0) + segment2 = voltage_to_data(waveform, t, 1) + segment_lengths[i] = len(segment1) if segment1 is not None else len(segment2) + assert(segment2 is None or segment_lengths[i] == len(segment2)) + offset = pytabor.make_combined_wave( + segment1, + segment2, + data, offset, add_idle_pts=True) + + if np.any(segment_lengths < device_properties['min_seq_len']): + raise Exception() + if np.any(segment_lengths % device_properties['seg_quantum']>0): + raise Exception() + + return data[:offset], segment_lengths + + def upload_to_device(self, device: 'TaborAWG', channel_pair): + if channel_pair not in ((1, 2), (3, 4)): + raise Exception('Invalid channel pair', channel_pair) + + if self.__waveform_mode == 'advanced': + samplerate = device.samplerate(channel_pair[0]) + amplitude = (device.amplitude(channel_pair[0]), device.amplitude(channel_pair[1])) + offset = (device.offset(channel_pair[0]), device.offset(channel_pair[1])) + + wf_data, segment_lengths = self.get_waveform_data(device_properties=device.dev_properties, + samplerate=samplerate, + voltage_amplitude=amplitude, + voltage_offset=offset) + # download the waveform data as one big waveform + device.select_channel(channel_pair[0]) + device.send_cmd(':FUNC:MODE ASEQ') + device.send_cmd(':TRAC:DEF 1,{}'.format(len(wf_data))) + device.send_cmd(':TRAC:SEL 1') + device.send_binary_data(pref=':TRAC:DATA', bin_dat=wf_data) + # partition the memory + device.download_segment_lengths(segment_lengths) + + #download all sequence tables + for i, sequencer_table in enumerate(self.get_sequencer_tables()): + device.send_cmd('SEQ:SEL {}'.format(i+1)) + device.download_sequencer_table(sequencer_table) + + device.download_adv_seq_table(self.get_advanced_sequencer_table()) + device.send_cmd('SEQ:SEL 1') + + + +class TaborAWG(teawg.TEWXAwg): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.__selected_channel = None + + @property + def is_open(self): + return self.visa_inst is not None + + def select_channel(self, channel): + if channel not in (1, 2, 3, 4): + raise TaborException('Invalid channel: {}'.format(channel)) + if self.__selected_channel != channel: + self.send_cmd(':INST:SEL {channel}'.format(channel=channel)) + self.__selected_channel = channel + + def samplerate(self, channel): + self.select_channel(channel) + return int(float(self.send_query(':FREQ:RAST?'.format(channel=channel)))) + + def amplitude(self, channel): + self.select_channel(channel) + couplig = self.send_query(':OUTP:COUP?'.format(channel=channel)) + if couplig == 'DC': + return float(self.send_query(':VOLT?')) + elif couplig == 'HV': + return float(self.send_query(':VOLD:HV?')) + + def offset(self, channel): + self.select_channel(channel) + return float(self.send_query(':VOLT:OFFS?'.format(channel=channel))) + + +class TaborException(Exception): + pass diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index cef6ec974..71ebc5754 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -14,8 +14,8 @@ warnings.warn("pyVISA not available, Tektronix AWGs only available in simulation mode!") hasVisa = False -from qctoolkit.hardware.awgs.awg import AWG, ProgramOverwriteException, Program -from qctoolkit.pulses.instructions import EXECInstruction, Waveform +from qctoolkit.hardware.awgs.base import AWG, ProgramOverwriteException, Program +from qctoolkit.pulses.instructions import EXECInstruction, SingleChannelWaveform __all__ = ['TektronixAWG', 'AWGSocket', 'EchoTestServer'] @@ -173,10 +173,10 @@ def rescale(self, voltages: np.ndarray) -> np.ndarray: # data = data + marker.astype(np.uint16) * 2**14 return data - def waveform2name(self, waveform: Waveform) -> str: + def waveform2name(self, waveform: SingleChannelWaveform) -> str: return str(hash(waveform)) - def add_waveform(self, waveform: Waveform, offset: float) -> None: + def add_waveform(self, waveform: SingleChannelWaveform, offset: float) -> None: """Samples a Waveform object to actual data and sends it to the AWG.""" # check if waveform is on the AWG already if waveform in self.__waveform_memory: diff --git a/qctoolkit/hardware/dacs/__init__.py b/qctoolkit/hardware/dacs/__init__.py index e69de29bb..b3348eaa7 100644 --- a/qctoolkit/hardware/dacs/__init__.py +++ b/qctoolkit/hardware/dacs/__init__.py @@ -0,0 +1,22 @@ +from abc import ABCMeta, abstractmethod +from typing import Dict + + +__all__ = ['DAC'] + + +class DAC(ABCMeta): + """Representation of a data acquisition card""" + + @abstractmethod + def register_measurement_windows(self, program_name: str, windows: Dict[str, 'numpy.ndarray']): + """""" + + def register_operations(self, program_name: str, operations): + """""" + + def arm_program(self, program_name: str): + """""" + + def delete_program(self, program_name): + """""" diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py new file mode 100644 index 000000000..65bfef267 --- /dev/null +++ b/qctoolkit/hardware/program.py @@ -0,0 +1,331 @@ +from qctoolkit.pulses.instructions import AbstractInstructionBlock, InstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction +from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable +from qctoolkit.hardware.awgs import AWG +from qctoolkit.comparable import Comparable + +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform +import itertools +from collections import namedtuple +from copy import deepcopy, copy as shallowcopy + + +ChannelID = int + +__all__ = ['ChannelID', 'Loop'] + + +class Loop(Comparable): + """Build a loop tree. The leaves of the tree are loops with one element.""" + def __init__(self, parent=None, instruction=None, children=list(), repetition_count=1): + super().__init__() + + self.__parent = parent + self.__children = [self.__parse_child(child) for child in children] + self.__instruction = instruction + self.__repetition_count = repetition_count + + if not (instruction is None or isinstance(instruction, (EXECInstruction, MultiChannelWaveform))): + raise Exception() + + def unroll(self): + for i, e in enumerate(self.__parent): + if e is self: + self.__parent[i:i+1] = (child.copy_tree_structure(new_parent=self.__parent) + for _ in range(self.repetition_count) + for child in self.children) + self.__parent.assert_tree_integrity() + self.__parent = None + return + raise Exception('self not found in parent') + + def unroll_children(self): + self.__children = [Loop(parent=self, instruction=child.instruction, children=child.children) + for _ in range(self.repetition_count) + for child in self.children] + self.__repetition_count = 1 + self.assert_tree_integrity() + + def encapsulate(self): + self.__children = [Loop(children=self.__children, + parent=self, + repetition_count=self.__repetition_count, + instruction=self.__instruction)] + self.__repetition_count = 1 + self.__instruction = None + self.assert_tree_integrity() + + def split_one_child(self): + self.assert_tree_integrity() + for i in reversed(range(len(self))): + if self[i].repetition_count > 1: + self[i].repetition_count -= 1 + self[i+1:i+1] = (self[i].copy_tree_structure(),) + self[i+1].repetition_count = 1 + self.assert_tree_integrity() + return + raise Exception('Could not split of one child', self) + + def is_leaf(self) -> bool: + return len(self.__children) == 0 + + def depth(self) -> int: + return 0 if self.is_leaf() else (1 + max((e.depth() for e in self))) + + def is_balanced(self) -> bool: + if self.is_leaf(): + return True + return all((e.depth() == self.__children[0].depth() and e.is_balanced()) for e in self) + + def merge(self): + """Merge successive loops that are repeated once and are no leafs into one""" + raise Exception("Not tested.") + if self.depth() < 2: + return + # TODO: make this pythonic + i = 0 + while i < len(self.__children): + if self.__children[i].repetition_count == 1 and not self.__children[i].is_leaf(): + j = i + 1 + while j < len(self.__children) and self.__children[j].repetition_count == 1 and not self.__children[j].is_leaf(): + j += 1 + if j > i + 1: + self.__children[i:j] = Loop(parent=self, + children=[cc for child in self.__children[i:j] for cc in child], + repetition_count=1) + i += 1 + self.assert_tree_integrity() + + def compare_key(self): + return self.__instruction, self.__repetition_count, tuple(c.compare_key() for c in self.__children) + + @property + def children(self) -> List['Loop']: + """ + :return: shallow copy of children + """ + return shallowcopy(self.__children) + + def append_child(self, **kwargs): + self.__children.append(Loop(parent=self, **kwargs)) + self.assert_tree_integrity() + + def __check_circular(self, visited: List['Loop']): + for v in visited: + if self is v: + raise Exception(self, visited) + visited.append(self) + for c in self.__children: + c.__check_circular(shallowcopy(visited)) + + def check_circular(self): + self.__check_circular([]) + + @property + def instruction(self): + return self.__instruction + + @instruction.setter + def instruction(self, val): + self.__instruction = val + + @property + def repetition_count(self): + return self.__repetition_count + + @repetition_count.setter + def repetition_count(self, val): + self.__repetition_count = val + + def get_root(self): + if self.__parent: + return self.__parent.get_root() + else: + return self + + def get_depth_first_iterator(self): + if not self.is_leaf(): + for e in self.__children: + yield from e.get_depth_first_iterator() + yield self + + def get_breadth_first_iterator(self, queue: List['Loop']=[]): + yield self + if not self.is_leaf(): + queue += self.__children + if queue: + yield from queue.pop(0).get_breadth_first_iterator(queue) + + def __iter__(self) -> Iterable['Loop']: + return iter(self.children) + + def __setitem__(self, idx, value): + if isinstance(idx, slice): + if isinstance(value, Loop): + raise TypeError('can only assign an iterable (Loop does not count)') + value = (self.__parse_child(child) for child in value) + else: + value = self.__parse_child(value) + self.__children.__setitem__(idx, value) + + def __getitem__(self, *args, **kwargs) ->Union['Loop', List['Loop']]: + return self.__children.__getitem__(*args, **kwargs) + + def __len__(self): + return len(self.__children) + + def __repr__(self): + try: + self.check_circular() + except Exception as e: + if len(e.args) == 2: + return '{}: Circ {}'.format(id(self), len(e.args[1])) + + if self.is_leaf(): + return 'EXEC {} {} times'.format(self.__instruction, self.__repetition_count) + else: + repr = ['LOOP {} times:'.format(self.__repetition_count)] + for elem in self.__children: + sub_repr = elem.__repr__().splitlines() + sub_repr = [' ->' + sub_repr[0]] + [' ' + line for line in sub_repr[1:]] + repr += sub_repr + return '\n'.join(repr) + + def __parse_child(self, child): + if isinstance(child, dict): + return Loop(parent=self, **child) + elif isinstance(child, Loop): + child.__parent = self + return child + else: + raise TypeError('Invalid child type', type(child)) + + def assert_tree_integrity(self): + if self.__parent: + children_ids = [id(c) for c in self.__parent.children] + if id(self) not in children_ids: + raise Exception() + for child in self.__children: + child.assert_tree_integrity() + + def copy_tree_structure(self, new_parent: Union['Loop', bool]=False): + return type(self)(parent=self.__parent if new_parent is not False else new_parent, + instruction=self.__instruction, + repetition_count=self.repetition_count, + children=(child.copy_tree_structure() for child in self.__children)) + + +class ChannelSplit(Exception): + def __init__(self, channels_and_blocks): + self.channels_and_stacks = channels_and_blocks + + +class MultiChannelProgram: + def __init__(self, instruction_block: AbstractInstructionBlock, channels: Iterable[ChannelID] = None): + if channels is None: + def find_defined_channels(instruction_list): + for instruction in instruction_list: + if isinstance(instruction, EXECInstruction): + return instruction.waveform.defined_channels + elif isinstance(instruction, REPJInstruction): + for _ in range(instruction.count): + return find_defined_channels( + instruction.target.block.instructions[instruction.target.offset:]) + elif isinstance(instruction, GOTOInstruction): + return find_defined_channels(instruction.target.block.instructions[instruction.target.offset:]) + elif isinstance(instruction, CHANInstruction): + return itertools.chain(*instruction.channel_to_instruction_block.keys()) + elif isinstance(instruction, STOPInstruction): + break + else: + raise TypeError('Unhandled instruction type', type(instruction)) + raise ValueError('Instruction block has no defined channels') + + channels = find_defined_channels(instruction_block.instructions) + + channels = frozenset(channels) + + stacks = {channels: [(Loop(), [*instruction_block[:-1]])]} + self.__programs = dict() + + while len(stacks) > 0: + chans, stack = stacks.popitem() + try: + self.__programs[chans] = MultiChannelProgram.__split_channels(stack, chans) + except ChannelSplit as c: + for new_chans, new_stack in c.channels_and_stacks.items(): + assert (new_chans not in stacks) + assert (chans.issuperset(new_chans)) + stacks[new_chans] = new_stack + + for channels, program in self.__programs.items(): + iterable = program.get_breadth_first_iterator() + while True: + try: + loop = next(iterable) + if len(loop) == 1: + loop.instruction = loop[0].instruction + loop.repetition_count = loop.repetition_count * loop[0].repetition_count + loop[:] = loop[0][:] + + iterable = itertools.chain((loop,), iterable) + except StopIteration: + break + + for loop in program.get_breadth_first_iterator(): + loop.assert_tree_integrity() + + @property + def programs(self): + return self.__programs + + @property + def channels(self): + return set(itertools.chain(*self.__programs.keys())) + + @staticmethod + def __split_channels(block_stack, channels): + while block_stack: + current_loop, current_instruction_block = block_stack.pop() + while current_instruction_block: + instruction = current_instruction_block.pop(0) + if isinstance(instruction, EXECInstruction): + if not instruction.waveform.defined_channels.issuperset(channels): + raise Exception(instruction.waveform.defined_channels, channels) + current_loop.append_child(instruction=instruction) + + elif isinstance(instruction, REPJInstruction): + current_loop.append_child(repetition_count=instruction.count) + block_stack.append( + (current_loop.children[-1], + [*instruction.target.block[instruction.target.offset:-1]]) + ) + + elif isinstance(instruction, CHANInstruction): + if channels in instruction.channel_to_instruction_block.keys(): + # push to front + new_instruction_ptr = instruction.channel_to_instruction_block[channels] + new_instruction_list = [*new_instruction_ptr.block[new_instruction_ptr.offset:-1]] + current_instruction_block[0:0] = new_instruction_list + + else: + block_stack.append((current_loop, current_instruction_block)) + + channel_to_stack = dict() + for (chs, instruction_ptr) in instruction.channel_to_instruction_block.items(): + channel_to_stack[chs] = deepcopy(block_stack) + channel_to_stack[chs][-1][1][0:0] = [*instruction_ptr.block[instruction_ptr.offset:-1]] + raise ChannelSplit(channel_to_stack) + else: + raise Exception('Encountered unhandled instruction {} on channel(s) {}'.format(instruction, channels)) + return current_loop.get_root() + + def __getitem__(self, item: Union[ChannelID, Set[ChannelID], FrozenSet[ChannelID]]): + if not isinstance(item, (set, frozenset)): + item = frozenset((item,)) + elif isinstance(item, set): + item = frozenset(item) + + for channels, program in self.__programs.items(): + if item.issubset(channels): + return program + raise KeyError(item) diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py new file mode 100644 index 000000000..f5b4d86bb --- /dev/null +++ b/qctoolkit/hardware/setup.py @@ -0,0 +1,89 @@ +from typing import NamedTuple, Any, Callable + +from ctypes import c_int64 as MutableInt + +from qctoolkit.hardware.awgs import AWG +from qctoolkit.hardware.dacs import DAC +from qctoolkit.hardware.program import MultiChannelProgram, Loop + +import numpy as np + + +__all__ = ['PlaybackChannel', 'HardwareSetup'] + + +PlaybackChannel = NamedTuple('PlaybackChannel', [('awg', AWG), + ('channel_on_awg', Any), + ('voltage_transformation', Callable[[np.ndarray], np.ndarray])]) +PlaybackChannel.__new__.__defaults__ = (lambda v: v,) +PlaybackChannel.__doc__ += ': Properties of an actual hardware channel' +PlaybackChannel.awg.__doc__ = 'The AWG the channel is defined on' +PlaybackChannel.channel_on_awg.__doc__ = 'The channel\'s ID on the AWG.' +PlaybackChannel.voltage_transformation.__doc__ = \ + 'A transformation that is applied to the pulses on the channel.\ + One use case is to scale up the voltage if an amplifier is inserted.' + + + +class HardwareSetup: + """Representation of the hardware setup. + + The class takes an instruction block, forms it into possibly channel dependent programs + and registers the programs at the AWGs which modify their program to fit to their capabilities. The class also + extracts the measurement windows(with absolute times) and hands them over to the DACs which will do further + processing.""" + def __init__(self): + self.__dacs = [] + + self.__channel_map = dict() # type: Dict[ChannelID, PlaybackChannel] + self.__awgs = [] # type: List[AWG, List[ChannelID, Any]] + + self.__registered_programs = dict() + + def register_program(self, name: str, instruction_block, run_callback=None): + mcp = MultiChannelProgram(instruction_block) + + measurement_windows = dict() + + def extract_measurement_windows(loop: Loop, offset: MutableInt): + if loop.is_leaf(): + for (mw_name, begin, length) in loop.instruction.measurement_windows: + measurement_windows.get(mw_name, default=[]).append(begin + offset.value, length) + offset.value += loop.instruction.waveform.duration + else: + for sub_loop in loop: + extract_measurement_windows(sub_loop, offset) + for program in mcp.programs.values(): + extract_measurement_windows(program, MutableInt(0)) + + for channels, program in mcp.programs: + pass + + try: + for dac in self.__dacs: + dac.register_measurement_windows(name, measurement_windows) + except: + raise + + self.__registered_programs[name] = (mcp, measurement_windows, + lambda: None if run_callback is None else run_callback) + + raise NotImplementedError() + + def arm_program(self, name): + """Assert program is in memory. Hardware will wait for trigger event""" + raise NotImplementedError() + + def run_program(self, name): + """Calls arm program and starts it using the run callback""" + raise NotImplementedError() + + def set_channel(self, identifier: ChannelID, channel: PlaybackChannel): + self.__channel_map[identifier] = channel + + + + + + + diff --git a/qctoolkit/hardware/util.py b/qctoolkit/hardware/util.py new file mode 100644 index 000000000..94be70b92 --- /dev/null +++ b/qctoolkit/hardware/util.py @@ -0,0 +1,24 @@ +import numpy as np + +__all__ = ['voltage_to_uint16'] + + +def voltage_to_uint16(voltage: np.ndarray, output_amplitude: float, output_offset: float, resolution: int): + """ + + :param voltage: + :param output_amplitude: + :param output_offset: + :param resolution: + :return: + """ + non_dc_voltage = voltage - output_offset + + if np.any(np.abs(non_dc_voltage) > output_amplitude): + raise ValueError('Voltage of range', dict(voltage=voltage, + output_offset=output_offset, + output_amplitude=output_amplitude)) + non_dc_voltage += output_amplitude + non_dc_voltage *= (2**resolution - 1) / (2*output_amplitude) + np.rint(non_dc_voltage, out=non_dc_voltage) + return non_dc_voltage.astype(np.uint16) diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index ee6042ca0..08922f10b 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -19,6 +19,7 @@ import itertools from abc import ABCMeta, abstractmethod, abstractproperty from typing import List, Any, Dict, Iterable, Optional, Tuple, Union +from weakref import WeakValueDictionary import numpy from qctoolkit.comparable import Comparable @@ -36,6 +37,8 @@ class SingleChannelWaveform(Comparable, metaclass=ABCMeta): """Represents an instantiated PulseTemplate which can be sampled to retrieve arrays of voltage values for the hardware.""" + __sampled_cache = WeakValueDictionary() + @abstractproperty def duration(self) -> float: """The duration of the waveform in time units.""" @@ -57,10 +60,24 @@ def sample(self, sample_times: numpy.ndarray, first_offset: float=0) -> numpy.nd float first_offset: Offset of the discrete first sample from the actual beginning of the waveform in a continuous time domain. Result: - numpy.ndarray of the sampled values of this Waveform at the provided sample times. If - this Waveform defines multiple channels, the array will be structured as - [ [channel 0 values] [channel 1 values] .... [channel n values] ]. + numpy.ndarray of the sampled values of this Waveform at the provided sample times. + """ + + def get_sampled(self, *args, **kwargs) -> numpy.ndarray: + """A wrapper to the sample method which caches the result. + + Args: + args, kwargs: Same signature as the sample method. See documentation there. + + Result: + A read only numpy.ndarray of the sampled values of this Waveform at the provided sample times. """ + result = self.sample(*args, **kwargs) + result.flags.writable = False + key = hash(result.data) + if key not in self.__sampled_cache: + self.__sampled_cache[key] = result + return self.__sampled_cache[key] class Trigger(Comparable): @@ -213,11 +230,11 @@ def __str__(self) -> str: class EXECInstruction(Instruction): """An instruction to execute/play back a waveform.""" - def __init__(self, waveform: SingleChannelWaveform, measurement_windows: List[Tuple[str, List['MeasurementWindow']]] = []) -> None: + def __init__(self, waveform: 'MultiChannelWaveform', measurement_windows: List['MeasurementWindow'] = []) -> None: """Create a new EXECInstruction object. Args: - waveform (SingleChannelWaveform): The waveform that will be executed by this instruction. + waveform (MultiChannelWaveform): The waveform that will be executed by this instruction. """ super().__init__() self.waveform = waveform diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index b8bbc5a6a..c55e4b12f 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -81,10 +81,10 @@ def duration(self) -> float: def __getitem__(self, key: Union[Set[ChannelID], ChannelID]) -> Union[SingleChannelWaveform, 'MultiChannelWaveform']: try: - if not isinstance(key, set): + if not isinstance(key, (set, frozenset)): return self.__channel_waveforms[key] else: - return MultiChannelWaveform( dict( (chID, self.__channel_waveforms[chID]) for chID in key ) ) + return MultiChannelWaveform(dict((chID, self.__channel_waveforms[chID]) for chID in key)) except KeyError as err: raise KeyError('Unknown channel ID: {}'.format(err.args[0]),*err.args) diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index f58d3c68a..b0ef1c1ee 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -11,7 +11,7 @@ import itertools MeasurementWindow = Tuple[str, float, float] -ChannelID = Union[str,int] +ChannelID = Union[str, int] from qctoolkit.serialization import Serializable diff --git a/qctoolkit/qcmatlab/pulse_control.py b/qctoolkit/qcmatlab/pulse_control.py index 2703d114f..c68ad78f8 100644 --- a/qctoolkit/qcmatlab/pulse_control.py +++ b/qctoolkit/qcmatlab/pulse_control.py @@ -7,7 +7,7 @@ import numpy -from qctoolkit.pulses.instructions import Waveform, EXECInstruction, \ +from qctoolkit.pulses.instructions import SingleChannelWaveform, EXECInstruction, \ STOPInstruction, InstructionSequence __all__ = ["PulseControlInterface"] @@ -34,17 +34,17 @@ def __init__(self, sample_rate: int, time_scaling: float=0.001) -> None: self.__time_scaling = time_scaling @staticmethod - def __get_waveform_name(waveform: Waveform) -> str: + def __get_waveform_name(waveform: SingleChannelWaveform) -> str: # returns a unique identifier for a waveform object return 'wf_{}'.format(hash(waveform)) def create_waveform_struct(self, - waveform: Waveform, + waveform: SingleChannelWaveform, name: str) -> 'PulseControlInterface.Pulse': """Construct a dictionary adhering to the waveform struct definition in pulse control. Arguments: - waveform (Waveform): The Waveform object to convert. + waveform (SingleChannelWaveform): The Waveform object to convert. name (str): Value for the name field in the resulting waveform dictionary. Returns: a dictionary representing waveform as a waveform struct for pulse control diff --git a/setup.py b/setup.py index 9a7f6c5a0..0b911475c 100644 --- a/setup.py +++ b/setup.py @@ -18,14 +18,16 @@ version='0.1', description='Quantum Computing Toolkit', author='qutech', - package_dir = {'qctoolkit': 'qctoolkit'}, + package_dir ={'qctoolkit': 'qctoolkit'}, packages=packages, tests_require=['pytest'], - install_requires= ['py_expression_eval', 'numpy', 'pyvisa'] + requires_typing, + install_requires=['py_expression_eval', 'numpy'] + requires_typing, extras_require={ - 'testing' : ['pytest'], - 'plotting' : ['matplotlib'], - 'faster expressions' : ['numexpr'] + 'testing': ['pytest'], + 'plotting': ['matplotlib'], + 'faster expressions': ['numexpr'], + 'VISA': ['pyvisa'], + 'tabor instruments': ['pytabor', 'teawg'] }, test_suite="tests", ) diff --git a/tests/hardware/__init__.py b/tests/hardware/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py new file mode 100644 index 000000000..3228e47a6 --- /dev/null +++ b/tests/hardware/program_tests.py @@ -0,0 +1,297 @@ +import unittest +import itertools +from copy import deepcopy + +from string import Formatter + +from qctoolkit.hardware.program import Loop, MultiChannelProgram +from qctoolkit.pulses.instructions import REPJInstruction, InstructionBlock, ImmutableInstructionBlock +from tests.pulses.sequencing_dummies import DummySingleChannelWaveform +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform + + +class LoopTests(unittest.TestCase): + def __init__(self, *args, waveform_data_generator=itertools.repeat(None), waveform_duration=None, **kwargs): + super().__init__(*args, **kwargs) + + def generate_waveform(): + return DummySingleChannelWaveform(sample_output=next(waveform_data_generator), duration=waveform_duration) + + self.old_description = \ +"""\ +LOOP 1 times: + ->EXEC 1 times + ->LOOP 10 times: + ->LOOP 5 times: + ->EXEC 1 times + ->LOOP 17 times: + ->LOOP 2 times: + ->EXEC 1 times + ->EXEC 1 times + ->EXEC 1 times + ->LOOP 3 times: + ->EXEC 1 times + ->EXEC 1 times + ->LOOP 4 times: + ->LOOP 6 times: + ->LOOP 7 times: + ->EXEC 1 times + ->LOOP 8 times: + ->EXEC 1 times + ->LOOP 9 times: + ->LOOP 10 times: + ->EXEC 1 times + ->LOOP 11 times: + ->EXEC 1 times""" + + self.new_description = \ +"""\ +LOOP 1 times: + ->EXEC {} 1 times + ->EXEC {} 50 times + ->LOOP 17 times: + ->LOOP 2 times: + ->EXEC {} 1 times + ->EXEC {} 1 times + ->EXEC {} 1 times + ->LOOP 3 times: + ->EXEC {} 1 times + ->EXEC {} 1 times + ->LOOP 4 times: + ->LOOP 6 times: + ->EXEC {} 7 times + ->EXEC {} 8 times + ->LOOP 9 times: + ->EXEC {} 10 times + ->EXEC {} 11 times""" + + self.loop_block11 = InstructionBlock() + self.loop_block11.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block1 = InstructionBlock() + self.loop_block1.add_instruction_repj(5,ImmutableInstructionBlock(self.loop_block11)) + + self.loop_block21 = InstructionBlock() + self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block2 = InstructionBlock() + self.loop_block2.add_instruction_repj(2,ImmutableInstructionBlock(self.loop_block21)) + self.loop_block2.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block3 = InstructionBlock() + self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block411 = InstructionBlock() + self.loop_block411.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block412 = InstructionBlock() + self.loop_block412.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block41 = InstructionBlock() + self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411)) + self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412)) + + self.loop_block421 = InstructionBlock() + self.loop_block421.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block422 = InstructionBlock() + self.loop_block422.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block42 = InstructionBlock() + self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421)) + self.loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(self.loop_block422)) + + self.loop_block4 = InstructionBlock() + self.loop_block4.add_instruction_repj(6, ImmutableInstructionBlock(self.loop_block41)) + self.loop_block4.add_instruction_repj(9, ImmutableInstructionBlock(self.loop_block42)) + + self.root_block = InstructionBlock() + self.root_block.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1)) + self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2)) + self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3)) + self.root_block.add_instruction_repj(4, ImmutableInstructionBlock(self.loop_block4)) + + self.maxDiff = None + + def get_root_loop(self): + program = MultiChannelProgram(self.root_block, {'A', 'B'}) + return program[{'A', 'B'}] + + def test_repr(self): + root_loop = self.get_root_loop() + repres = root_loop.__repr__() + expected = self.new_description.format(*(loop.instruction + for loop in root_loop.get_depth_first_iterator() if loop.is_leaf())) + self.assertEqual(repres, expected) + + def test_is_leaf(self): + root_loop = self.get_root_loop() + + for loop in root_loop.get_depth_first_iterator(): + self.assertTrue(bool(loop.is_leaf()) != bool(loop.instruction is None)) + + for loop in root_loop.get_breadth_first_iterator(): + self.assertTrue(bool(loop.is_leaf()) != bool(loop.instruction is None)) + + def test_depth(self): + root_loop = self.get_root_loop() + self.assertEqual(root_loop.depth(), 3) + self.assertEqual(root_loop[-1].depth(), 2) + self.assertEqual(root_loop[-1][-1].depth(), 1) + self.assertEqual(root_loop[-1][-1][-1].depth(), 0) + with self.assertRaises(IndexError): + root_loop[-1][-1][-1][-1].depth() + + def test_is_balanced(self): + root_loop = self.get_root_loop() + self.assertFalse(root_loop.is_balanced()) + + self.assertFalse(root_loop[2].is_balanced()) + self.assertTrue(root_loop[0].is_balanced()) + self.assertTrue(root_loop[1].is_balanced()) + self.assertTrue(root_loop[3].is_balanced()) + self.assertTrue(root_loop[4].is_balanced()) + + @unittest.skip("Further thinking needed.") + def test_merge(self): + # TODO: this test sucks + root_loop1 = self.get_root_loop() + root_loop2 = self.get_root_loop() + + for l1, l2 in zip(root_loop1,root_loop2): + l1.merge() + self.assertEqual(l1.__repr__(),l2.__repr__()) + + if not l1.is_leaf(): + for m1, m2 in zip(l1, l2): + m1.merge() + self.assertEqual(m1.__repr__(), m2.__repr__()) + + merge_loop = self.get_root_loop() + merge_loop[1].__repetition_count = 1 + merge_loop[2].__repetition_count = 1 + merge_loop.merge() + self.assertEqual(len(merge_loop) + 1, len(root_loop1)) + self.assertEqual(merge_loop[1].repetition_count, 1) + self.assertEqual(merge_loop[2].repetition_count, 3) + self.assertEqual(merge_loop[1].depth(), 1) + + merge_loop = Loop(self.root_block) + merge_loop[1].__repetition_count = 1 + merge_loop[2].__repetition_count = 1 + merge_loop[3].__repetition_count = 1 + merge_loop.merge() + self.assertEqual(len(merge_loop) + 2,len(root_loop1)) + self.assertEqual(merge_loop[1].repetition_count,1) + self.assertEqual(merge_loop[2].repetition_count,4) + + +class MultiChannelTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + wf = DummySingleChannelWaveform() + self.descriptionA = \ +"""\ +LOOP 1 times: + ->EXEC {} 1 times + ->EXEC {} 50 times + ->LOOP 17 times: + ->LOOP 2 times: + ->EXEC {} 1 times + ->EXEC {} 1 times + ->EXEC {} 1 times + ->LOOP 3 times: + ->EXEC {} 1 times + ->EXEC {} 1 times + ->LOOP 24 times: + ->EXEC {} 7 times + ->EXEC {} 8 times""" + self.descriptionB = \ +"""\ +LOOP 1 times: + ->EXEC {} 1 times + ->EXEC {} 50 times + ->LOOP 17 times: + ->LOOP 2 times: + ->EXEC {} 1 times + ->EXEC {} 1 times + ->EXEC {} 1 times + ->LOOP 3 times: + ->EXEC {} 1 times + ->EXEC {} 1 times + ->LOOP 36 times: + ->EXEC {} 10 times + ->EXEC {} 11 times""" + + def generate_waveform(): + return DummySingleChannelWaveform(sample_output=None, duration=None) + + self.loop_block11 = InstructionBlock() + self.loop_block11.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block1 = InstructionBlock() + self.loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(self.loop_block11)) + + self.loop_block21 = InstructionBlock() + self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block2 = InstructionBlock() + self.loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(self.loop_block21)) + self.loop_block2.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block3 = InstructionBlock() + self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + + self.loop_block411 = InstructionBlock() + self.loop_block411.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform()))) + self.loop_block412 = InstructionBlock() + self.loop_block412.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform()))) + + self.loop_block41 = InstructionBlock() + self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411)) + self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412)) + + self.loop_block421 = InstructionBlock() + self.loop_block421.add_instruction_exec(MultiChannelWaveform(dict(B=generate_waveform()))) + self.loop_block422 = InstructionBlock() + self.loop_block422.add_instruction_exec(MultiChannelWaveform(dict(B=generate_waveform()))) + + self.loop_block42 = InstructionBlock() + self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421)) + self.loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(self.loop_block422)) + + self.chan_block4A = InstructionBlock() + self.chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(self.loop_block41)) + + self.chan_block4B = InstructionBlock() + self.chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(self.loop_block42)) + + self.loop_block4 = InstructionBlock() + self.loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(self.chan_block4A), + frozenset('B'): ImmutableInstructionBlock(self.chan_block4B)}) + + self.root_block = InstructionBlock() + self.root_block.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1)) + self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2)) + self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3)) + self.root_block.add_instruction_repj(4, ImmutableInstructionBlock(self.loop_block4)) + + self.maxDiff = None + + def get_root_loop(self, channels): + program = MultiChannelProgram(self.root_block, ['A', 'B']) + return program[channels] + + def test_via_repr(self): + root_loopA = self.get_root_loop('A') + root_loopB = self.get_root_loop('B') + reprA = self.descriptionA.format(*(loop.instruction + for loop in root_loopA.get_depth_first_iterator() if loop.is_leaf())) + reprB = self.descriptionB.format(*(loop.instruction + for loop in root_loopB.get_depth_first_iterator() if loop.is_leaf())) + self.assertEqual(root_loopA.__repr__(), reprA) + self.assertEqual(root_loopB.__repr__(), reprB) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py new file mode 100644 index 000000000..3f9193ce7 --- /dev/null +++ b/tests/hardware/tabor_tests.py @@ -0,0 +1,69 @@ +import unittest +from qctoolkit.hardware.awgs.tabor import TaborAWG, TaborException, TaborProgram +from qctoolkit.hardware.program import MultiChannelProgram +import numbers +import itertools +import numpy as np + +from .program_tests import LoopTests + +instrument_address = '127.0.0.1' +#instrument_address = '192.168.1.223' +instrument = TaborAWG(instrument_address) +instrument._visa_inst.timeout = 25000 + +class TaborAWGTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + + + def test_samplerate(self): + if not instrument.is_open: + self.skipTest("No instrument found.") + for ch in (1, 2, 3, 4): + self.assertIsInstance(instrument.samplerate(ch), numbers.Number) + with self.assertRaises(TaborException): + instrument.samplerate(0) + + def test_amplitude(self): + for ch in range(1, 5): + self.assertIsInstance(instrument.amplitude(ch), float) + + +class TaborProgramTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.waveform_data_generator = itertools.cycle([np.linspace(-0.5, 0.5, num=4048), + np.concatenate((np.linspace(-0.5, 0.5, num=2024), + np.linspace(0.5, -0.5, num=2024))), + -0.5*np.cos(np.linspace(0, 2*np.pi, num=4048))]) + + @property + def root_block(self): + return LoopTests(waveform_data_generator=self.waveform_data_generator, waveform_duration=4048e-9).root_block + + @property + def working_root_block(self): + block = self.root_block + + def setUp(self): + if not instrument.is_open: + self.skipTest("Instrument not found.") + + def test_init(self): + prog = MultiChannelProgram(self.root_block) + TaborProgram(prog, instrument.dev_properties, {'A', 'B'}) + + def test_upload(self): + prog = MultiChannelProgram(self.root_block) + program = TaborProgram(prog, instrument.dev_properties, ('A', 'B')) + + program.upload_to_device(instrument, (1, 2)) + + + + + + diff --git a/tests/qcmatlab/pulse_control_tests.py b/tests/qcmatlab/pulse_control_tests.py index 8268a7417..2df5945ee 100644 --- a/tests/qcmatlab/pulse_control_tests.py +++ b/tests/qcmatlab/pulse_control_tests.py @@ -2,7 +2,7 @@ import numpy import numpy.random from qctoolkit.qcmatlab.pulse_control import PulseControlInterface -from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummyInstructionBlock class PulseControlInterfaceTests(unittest.TestCase): @@ -12,7 +12,7 @@ def test_create_waveform_struct(self) -> None: sample_rate = 10 expected_samples = numpy.random.rand(11) - waveform = DummyWaveform(duration=1, sample_output=expected_samples) + waveform = DummySingleChannelWaveform(duration=1, sample_output=expected_samples) pci = PulseControlInterface(sample_rate, time_scaling=1) result = pci.create_waveform_struct(waveform, name=name) @@ -48,9 +48,9 @@ def test_create_pulse_group(self) -> None: expected_samples_wf1 = numpy.random.rand(11) expected_samples_wf2 = numpy.random.rand(11) block = DummyInstructionBlock() - wf1a = DummyWaveform(duration=1, sample_output=expected_samples_wf1) - wf1b = DummyWaveform(duration=1, sample_output=expected_samples_wf1) - wf2 = DummyWaveform(duration=1, sample_output=expected_samples_wf2) + wf1a = DummySingleChannelWaveform(duration=1, sample_output=expected_samples_wf1) + wf1b = DummySingleChannelWaveform(duration=1, sample_output=expected_samples_wf1) + wf2 = DummySingleChannelWaveform(duration=1, sample_output=expected_samples_wf2) block.add_instruction_exec(wf1a) block.add_instruction_exec(wf1b) block.add_instruction_exec(wf2) From e7665966336c2f3b1d24cb6163d666492fad7c2b Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 15 Feb 2017 13:50:23 +0100 Subject: [PATCH 013/116] Introduce atomicity property and redesign waveform: -atomicity may be switched on fi. for SequencePulseTemplates -> it will be translated to one waveform -measurement windows now included in waveform -waveforms for RepeatedPulseTemplate and SequecencePulseTemplate added --- qctoolkit/pulses/branch_pulse_template.py | 4 + qctoolkit/pulses/function_pulse_template.py | 66 +++++--- qctoolkit/pulses/instructions.py | 110 ++++++++---- qctoolkit/pulses/loop_pulse_template.py | 4 + .../pulses/multi_channel_pulse_template.py | 138 +++++++++------ qctoolkit/pulses/plotting.py | 48 ++++-- qctoolkit/pulses/pulse_template.py | 67 ++++---- .../pulse_template_parameter_mapping.py | 26 ++- qctoolkit/pulses/repetition_pulse_template.py | 112 ++++++++++-- qctoolkit/pulses/sequence_pulse_template.py | 109 +++++++++++- qctoolkit/pulses/sequencing.py | 6 +- qctoolkit/pulses/table_pulse_template.py | 76 ++++++--- tests/pulses/function_pulse_tests.py | 32 ++-- tests/pulses/instructions_tests.py | 92 ++++++++-- .../multi_channel_pulse_template_tests.py | 159 +++++++++++------- tests/pulses/plotting_tests.py | 42 +++-- tests/pulses/pulse_template_tests.py | 26 +-- .../pulses/repetition_pulse_template_tests.py | 51 +++++- tests/pulses/sequence_pulse_template_tests.py | 65 +++++-- tests/pulses/sequencing_dummies.py | 61 +++++-- tests/pulses/table_pulse_template_tests.py | 94 +++++++---- ...e_sequence_sequencer_intergration_tests.py | 2 +- tests/qcmatlab/pulse_control_tests.py | 12 +- 23 files changed, 1012 insertions(+), 390 deletions(-) diff --git a/qctoolkit/pulses/branch_pulse_template.py b/qctoolkit/pulses/branch_pulse_template.py index 56ffbcda4..d82a9c7f8 100644 --- a/qctoolkit/pulses/branch_pulse_template.py +++ b/qctoolkit/pulses/branch_pulse_template.py @@ -72,6 +72,10 @@ def defined_channels(self) -> Set['ChannelID']: def measurement_names(self) -> Set[str]: return self.__if_branch.measurement_names | self.__else_branch.measurement_names + @property + def atomicity(self) -> bool: + return False + def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: try: return conditions[self.__condition] diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index f105e7f3b..108b565f6 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -8,7 +8,6 @@ from typing import Any, Dict, List, Set, Optional, Union -import numbers import numpy as np @@ -16,12 +15,12 @@ from qctoolkit.serialization import Serializer from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow -from qctoolkit.pulses.instructions import SingleChannelWaveform +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow, ChannelID +from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.pulse_template_parameter_mapping import ParameterNotProvidedException from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -__all__ = ["FunctionPulseTemplate", "FunctionSingleChannelWaveform"] +__all__ = ["FunctionPulseTemplate", "FunctionWaveform"] class FunctionPulseTemplate(AtomicPulseTemplate): @@ -89,9 +88,6 @@ def get_pulse_length(self, parameters: Dict[str, Parameter]) -> float: for (parameter_name, parameter) in parameters.items()} ) - def get_measurement_windows(self, parameters: Dict[str, Parameter]) -> List[MeasurementWindow]: - return [] - @property def measurement_names(self) -> Set[str]: return set() @@ -104,16 +100,20 @@ def is_interruptable(self) -> bool: def defined_channels(self) -> Set['ChannelID']: return set(self.__channel) - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[MultiChannelWaveform]: - return MultiChannelWaveform( - {self.__channel: FunctionSingleChannelWaveform( - {parameter_name: parameter.get_value() - for (parameter_name, parameter) in parameters.items()}, - self.__expression, - self.__duration_expression - )} + def build_waveform(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]): + return FunctionWaveform( + channel=channel_mapping[self.__channel], + parameters={parameter_name: parameter.get_value() + for (parameter_name, parameter) in parameters.items()}, + expression=self.__expression, + duration_expression=self.__duration_expression, + measurement_windows=[] ) + def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: @@ -143,13 +143,16 @@ def deserialize(serializer: 'Serializer', ) -class FunctionSingleChannelWaveform(SingleChannelWaveform): +class FunctionWaveform(Waveform): """Waveform obtained from instantiating a FunctionPulseTemplate.""" def __init__(self, parameters: Dict[str, float], expression: Expression, - duration_expression: Expression) -> None: + duration_expression: Expression, + measurement_windows: List[MeasurementWindow], + channel: ChannelID + ) -> None: """Creates a new FunctionWaveform instance. Args: @@ -164,10 +167,15 @@ def __init__(self, self.__expression = expression self.__parameters = parameters self.__duration = duration_expression.evaluate(**self.__parameters) + self.__channel_id = channel + self.__measurement_windows = measurement_windows @property - def num_channels(self): - return 1 + def defined_channels(self): + return {self.__channel_id} + + def get_measurement_windows(self): + return self.__measurement_windows def __evaluate_partially(self, t): params = self.__parameters.copy() @@ -176,15 +184,21 @@ def __evaluate_partially(self, t): @property def compare_key(self) -> Any: - return self.__expression, self.__duration, self.__parameters + return self.__channel_id, self.__expression, self.__duration, self.__parameters @property def duration(self) -> float: return self.__duration - def sample(self, sample_times: np.ndarray, first_offset: float=0) -> np.ndarray: - sample_times -= (sample_times[0] - first_offset) - func = np.vectorize(self.__evaluate_partially) - voltages = np.empty((1, len(sample_times))) - voltages[0] = func(sample_times) - return voltages + def unsafe_sample(self, + channel: ChannelID, + sample_times: np.ndarray, + output_array: Union[np.ndarray, None] = None) -> np.ndarray: + if output_array is None: + output_array = np.empty(len(sample_times)) + for i, t in enumerate(sample_times): + output_array[i] = self.__evaluate_partially(t) + return output_array + + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + return self diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index 08922f10b..ab1cd3f8e 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -18,13 +18,14 @@ import itertools from abc import ABCMeta, abstractmethod, abstractproperty -from typing import List, Any, Dict, Iterable, Optional, Tuple, Union +from typing import List, Any, Dict, Iterable, Optional, Tuple, Union, Set from weakref import WeakValueDictionary import numpy from qctoolkit.comparable import Comparable +from qctoolkit.pulses.pulse_template import MeasurementWindow -__all__ = ["SingleChannelWaveform", "Trigger", +__all__ = ["Waveform", "Trigger", "InstructionPointer", "Instruction", "CJMPInstruction", "EXECInstruction", "GOTOInstruction", "STOPInstruction", "REPJInstruction", "AbstractInstructionBlock", "InstructionBlock", "ImmutableInstructionBlock", "InstructionSequence", "ChannelID" @@ -33,7 +34,7 @@ ChannelID = Union[str,int] -class SingleChannelWaveform(Comparable, metaclass=ABCMeta): +class Waveform(Comparable, metaclass=ABCMeta): """Represents an instantiated PulseTemplate which can be sampled to retrieve arrays of voltage values for the hardware.""" @@ -44,40 +45,86 @@ def duration(self) -> float: """The duration of the waveform in time units.""" @abstractmethod - def sample(self, sample_times: numpy.ndarray, first_offset: float=0) -> numpy.ndarray: + def unsafe_sample(self, + channel: ChannelID, + sample_times: numpy.ndarray, + output_array: Union[numpy.ndarray, None]=None) -> numpy.ndarray: """Sample the waveform at given sample times. - The only requirement on the provided sample times is that they must be monotonously - increasing. The must not lie in the range of [0, waveform.duration] (but will be normalized - internally into that range for the sampling). For example, if this Waveform had a duration - of 5 and the given sample times would be [11, 15, 20], the result would be the samples of - this Waveform at [0, 2.5, 5] in the Waveforms domain. This allows easier sampling of - multiple subsequent Waveforms. + The unsafe means that there are no sanity checks performed. The provided sample times are assumed to be + monotonously increasing and lie in the range of [0, waveform.duration] Args: - numpy.ndarray sample_times: Times at which this Waveform will be sampled. Will be - normalized such that they lie in the range [0, waveform.duration] for interpolation. - float first_offset: Offset of the discrete first sample from the actual beginning of - the waveform in a continuous time domain. + numpy.ndarray sample_times: Times at which this Waveform will be sampled. + numpy.ndarray output_array: Has to be either None or an array of the same size and type as sample_times. If + not None, the sampled values will be written here and this array will be returned Result: - numpy.ndarray of the sampled values of this Waveform at the provided sample times. + numpy.ndarray of the sampled values of this Waveform at the provided sample times. Has the same number of + elements as sample_times. """ - def get_sampled(self, *args, **kwargs) -> numpy.ndarray: - """A wrapper to the sample method which caches the result. + def get_sampled(self, + channel: ChannelID, + sample_times: numpy.ndarray, + output_array: Union[numpy.ndarray, None]=None) -> numpy.ndarray: + """A wrapper to the unsafe_sample method which caches the result. This method enforces the constrains + unsafe_sample expects and caches the result to save memory. + + Args/Result: + numpy.ndarray sample_times: Times at which this Waveform will be sampled. + numpy.ndarray output_array: Has to be either None or an array of the same size and type as sample_times. + If None, a new array will be created and cached to save memory. + If not None, the sampled values will be written here and this array will be returned. + Result: + A numpy.ndarray of the sampled values of this Waveform at the provided sample times. + """ + if numpy.any(sample_times[:-1] >= sample_times[1:]): + raise ValueError('The sample times are not in the range [0, duration]') + if sample_times[0] < 0 or sample_times[-1] > self.duration: + raise ValueError('The sample times are not monotonously increasing') + if channel not in self.defined_channels: + raise KeyError('Channel not defined in this waveform: {}'.format(channel)) + + if output_array is None: + # cache the result to save memory + result = self.unsafe_sample(channel, sample_times) + result.flags.writeable = False + key = hash(bytes(result)) + if key not in self.__sampled_cache: + self.__sampled_cache[key] = result + return self.__sampled_cache[key] + else: + if len(output_array) != len(sample_times): + raise ValueError('Output array length and sample time length are different') + # use the user provided memory + return self.unsafe_sample(channel=channel, + sample_times=sample_times, + output_array=output_array) - Args: - args, kwargs: Same signature as the sample method. See documentation there. + @abstractproperty + def defined_channels(self) -> Set[ChannelID]: + """""" - Result: - A read only numpy.ndarray of the sampled values of this Waveform at the provided sample times. + @abstractmethod + def get_measurement_windows(self) -> Iterable[MeasurementWindow]: + """This function will in must cases return a generator to fill the measurement windows in a more efficient + data structure like a dict.""" + + @abstractmethod + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + """""" + + def get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + """ + + :param channels: + :return: """ - result = self.sample(*args, **kwargs) - result.flags.writable = False - key = hash(result.data) - if key not in self.__sampled_cache: - self.__sampled_cache[key] = result - return self.__sampled_cache[key] + if not channels <= self.defined_channels: + raise KeyError('Channels not defined on waveform: {}'.format(channels)) + if channels == self.defined_channels: + return self + return self.unsafe_get_subset_for_channels(channels=channels) class Trigger(Comparable): @@ -230,7 +277,7 @@ def __str__(self) -> str: class EXECInstruction(Instruction): """An instruction to execute/play back a waveform.""" - def __init__(self, waveform: 'MultiChannelWaveform', measurement_windows: List['MeasurementWindow'] = []) -> None: + def __init__(self, waveform: 'MultiChannelWaveform') -> None: """Create a new EXECInstruction object. Args: @@ -238,7 +285,6 @@ def __init__(self, waveform: 'MultiChannelWaveform', measurement_windows: List[' """ super().__init__() self.waveform = waveform - self.measurement_windows = measurement_windows @property def compare_key(self) -> Any: @@ -396,14 +442,14 @@ def add_instruction(self, instruction: Instruction) -> None: """ self.__instruction_list.append(instruction) - def add_instruction_exec(self, waveform: 'MultiChannelWaveform', measurement_windows: List[Tuple[str, List['MeasurementWindows']]] = None) -> None: + def add_instruction_exec(self, waveform: 'MultiChannelWaveform') -> None: """Create and append a new EXECInstruction object for the given waveform at the end of this instruction block. Args: - waveform (SingleChannelWaveform): The Waveform object referenced by the new EXECInstruction. + waveform (Waveform): The Waveform object referenced by the new EXECInstruction. """ - self.add_instruction(EXECInstruction(waveform, measurement_windows)) + self.add_instruction(EXECInstruction(waveform)) def add_instruction_goto(self, target_block: 'InstructionBlock') -> None: """Create and append a new GOTOInstruction object with a given target block at the end of diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index 721dd94e2..cc6897aba 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -69,6 +69,10 @@ def defined_channels(self) -> Set['ChannelID']: def measurement_names(self) -> Set[str]: return self.__body.measurement_names + @property + def atomicity(self) -> bool: + False + def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: try: return conditions[self.__condition] diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index c55e4b12f..b38562a70 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -7,14 +7,16 @@ - MultiChannelWaveform: A waveform defined for several channels by combining waveforms """ -from typing import Dict, List, Tuple, Set, Optional, Any, Iterable, Union +from typing import Dict, List, Tuple, FrozenSet, Optional, Any, Iterable, Union, Set +import itertools import numpy + from qctoolkit.serialization import Serializer -from qctoolkit.pulses.instructions import InstructionBlock, SingleChannelWaveform, InstructionPointer -from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.instructions import InstructionBlock, Waveform, InstructionPointer +from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingTemplate,\ MissingParameterDeclarationException, ChannelID from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ @@ -25,7 +27,7 @@ __all__ = ["MultiChannelWaveform", "MultiChannelPulseTemplate"] -class MultiChannelWaveform(Comparable): +class MultiChannelWaveform(Waveform): """A MultiChannelWaveform is a Waveform object that allows combining arbitrary Waveform objects to into a single waveform defined for several channels. @@ -44,7 +46,7 @@ class MultiChannelWaveform(Comparable): assigned more than one channel of any Waveform object it consists of """ - def __init__(self, subwaveforms: Dict[ChannelID, SingleChannelWaveform]) -> None: + def __init__(self, subwaveforms: Iterable[Waveform]) -> None: """Create a new MultiChannelWaveform instance. Requires a list of subwaveforms in the form (Waveform, List(int)) where the list defines @@ -52,8 +54,8 @@ def __init__(self, subwaveforms: Dict[ChannelID, SingleChannelWaveform]) -> None subwaveform will be mapped to channel y of this MultiChannelWaveform object. Args: - subwaveforms (List( (Waveform, List(int)) )): The list of subwaveforms of this - MultiChannelWaveform as tuples of the form (Waveform, List(int)) + subwaveforms (Iterable( Waveform )): The list of subwaveforms of this + MultiChannelWaveform Raises: ValueError, if a channel mapping is out of bounds of the channels defined by this MultiChannelWaveform @@ -67,54 +69,67 @@ def __init__(self, subwaveforms: Dict[ChannelID, SingleChannelWaveform]) -> None "MultiChannelWaveform cannot be constructed without channel waveforms." ) - self.__channel_waveforms = subwaveforms - duration = next(iter(self.__channel_waveforms.values())).duration - if not all(waveform.duration == duration for waveform in self.__channel_waveforms.values()): + def flattened_sub_waveforms(): + for sub_waveform in subwaveforms: + if isinstance(sub_waveform, MultiChannelWaveform): + yield from sub_waveform.__sub_waveforms + else: + yield sub_waveform + self.__sub_waveforms = tuple(flattened_sub_waveforms()) + + if not all(waveform.duration == self.__sub_waveforms[0].duration for waveform in self.__sub_waveforms[1:]): raise ValueError( "MultiChannelWaveform cannot be constructed from channel waveforms of different" "lengths." ) + self.__defined_channels = set() + for waveform in self.__sub_waveforms: + if waveform.defined_channels & self.__defined_channels: + raise ValueError('Channel may not be defined in multiple waveforms') + self.__defined_channels |= waveform.defined_channels @property def duration(self) -> float: - return next(iter(self.__channel_waveforms.values())).duration - - def __getitem__(self, key: Union[Set[ChannelID], ChannelID]) -> Union[SingleChannelWaveform, 'MultiChannelWaveform']: - try: - if not isinstance(key, (set, frozenset)): - return self.__channel_waveforms[key] - else: - return MultiChannelWaveform(dict((chID, self.__channel_waveforms[chID]) for chID in key)) - except KeyError as err: - raise KeyError('Unknown channel ID: {}'.format(err.args[0]),*err.args) - - def __add__(self, other: 'MultiChannelWaveform') -> 'MultiChannelWaveform': - if set(self.__channel_waveforms) | set(other.__channel_waveforms): - raise ChannelMappingException(set(self.__channel_waveforms) | set(other.__channel_waveforms)) - return MultiChannelWaveform(dict(**self.__channel_waveforms,**other.__channel_waveforms)) - - def __iadd__(self, other: 'MultiChannelWaveform'): - if set(self.__channel_waveforms) | set(other.__channel_waveforms): - raise ChannelMappingException(set(self.__channel_waveforms) | set(other.__channel_waveforms)) - if self.duration != other.duration: - raise ValueError('Waveforms not combinable as they have different durations.') - self.__channel_waveforms = dict(**self.__channel_waveforms,**other.__channel_waveforms) + return self.__sub_waveforms[0].duration + + def __getitem__(self, key: ChannelID) -> Waveform: + for waveform in self.__sub_waveforms: + if key in waveform.defined_channels: + return waveform + raise KeyError('Unknown channel ID: {}'.format(key), key) @property def defined_channels(self) -> Set[ChannelID]: - return set(self.__channel_waveforms.keys()) + return self.__defined_channels @property def compare_key(self) -> Any: - return self.__channel_waveforms - - def get_remapped(self, channel_mapping): - return MultiChannelWaveform( - {channel_mapping[old_channel]: waveform for old_channel, waveform in self.__channel_waveforms.items()} - ) - - -class MultiChannelPulseTemplate(PulseTemplate): + # make independent of order + return set(self.__sub_waveforms) + + def unsafe_sample(self, + channel: ChannelID, + sample_times: numpy.ndarray, + output_array: Union[numpy.ndarray, None]=None) -> numpy.ndarray: + return self[channel].unsafe_sample(channel, sample_times, output_array) + + def get_measurement_windows(self): + return itertools.chain.from_iterable(sub_waveform.get_measurement_windows() + for sub_waveform in self.__sub_waveforms) + + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + relevant_sub_waveforms = tuple(swf for swf in self.__sub_waveforms if swf.defined_channels & channels) + if len(relevant_sub_waveforms) == 1: + return relevant_sub_waveforms[0].get_subset_for_channels(channels) + elif len(relevant_sub_waveforms) > 1: + return MultiChannelWaveform( + sub_waveform.get_subset_for_channels(channels & sub_waveform.defined_channels) + for sub_waveform in relevant_sub_waveforms) + else: + raise KeyError('Unknown channels: {}'.format(channels)) + + +class MultiChannelPulseTemplate(PossiblyAtomicPulseTemplate): """A multi-channel group of several AtomicPulseTemplate objects. While SequencePulseTemplate combines several subtemplates (with an identical number of channels) @@ -204,6 +219,8 @@ def to_mapping_template(template, parameter_mapping, channel_mapping): if remaining: raise MissingMappingException(subtemplate.template, remaining.pop()) + self.__atomicity = False + @property def parameter_names(self) -> Set[str]: return set.union(*(st.parameter_names for st in self.__subtemplates)) @@ -229,6 +246,21 @@ def defined_channels(self) -> Set[ChannelID]: def measurement_names(self) -> Set[str]: return set.union(*(st.measurement_names for st in self.__subtemplates)) + @property + def atomicity(self): + if any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): + self.__atomicity = False + return self.__atomicity + + @atomicity.setter + def atomicity(self, val: bool): + if val and any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): + raise ValueError('Cannot make atomic as not all sub templates are atomic') + self.__atomicity = val + + def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['MultiChannelWaveform']: + return MultiChannelWaveform([subtemplate.build_waveform(parameters) for subtemplate in self.__subtemplates]) + def build_sequence(self, sequencer: 'Sequencer', parameters: Dict[str, Parameter], @@ -236,31 +268,39 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - channel_to_instruction_block = dict() - for subtemplate in self.subtemplates: - block = InstructionBlock() - sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, block) - channel_to_instruction_block.update(dict.fromkeys(subtemplate.defined_channels, block)) - instruction_block.add_instruction_chan(channel_to_instruction=channel_to_instruction_block) + if self.atomicity: + self.atomic_build_sequence(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + else: + channel_to_instruction_block = dict() + for subtemplate in self.subtemplates: + block = InstructionBlock() + sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, block) + channel_to_instruction_block.update(dict.fromkeys(subtemplate.defined_channels, block)) + instruction_block.add_instruction_chan(channel_to_instruction=channel_to_instruction_block) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: return any(st.requires_stop(parameters, conditions) for st in self.__subtemplates) - def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], + atomicity=self.atomicity, type=serializer.get_type_identifier(self)) return data @staticmethod def deserialize(serializer: Serializer, subtemplates: Iterable[Dict[str, Any]], + atomicity: bool, identifier: Optional[str]=None) -> 'MultiChannelPulseTemplate': subtemplates = [serializer.deserialize(st) for st in subtemplates] external_parameters = set.union(*(st.parameter_names for st in subtemplates)) mul_template = MultiChannelPulseTemplate(subtemplates, external_parameters, identifier=identifier) + mul_template.atomicity = atomicity return mul_template diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index 3aa0f9e19..1458bf7dd 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -12,7 +12,7 @@ import numpy as np from matplotlib import pyplot as plt -from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.pulse_template import PulseTemplate, ChannelID from qctoolkit.pulses.parameters import Parameter from qctoolkit.pulses.sequencing import Sequencer from qctoolkit.pulses.instructions import EXECInstruction, STOPInstruction, InstructionSequence, \ @@ -38,37 +38,51 @@ def __init__(self, sample_rate: int=10) -> None: super().__init__() self.__sample_rate = sample_rate - def render(self, sequence: InstructionSequence) -> Tuple[np.ndarray, np.ndarray]: + def render(self, sequence: InstructionSequence) -> Tuple[np.ndarray, Dict[ChannelID, np.ndarray]]: """'Render' an instruction sequence (sample all contained waveforms into an array). Returns: a tuple (times, values) of numpy.ndarrays of similar size. times contains the time value of all sample times and values the corresponding sampled value. """ - if [x for x in sequence if not isinstance(x, (EXECInstruction, - STOPInstruction, - REPJInstruction))]: + if not all(isinstance(x, (EXECInstruction, STOPInstruction, REPJInstruction)) for x in sequence): raise NotImplementedError('Can only plot waveforms without branching so far.') - waveforms = [instruction.waveform - for instruction in sequence if isinstance(instruction, EXECInstruction)] + def get_waveform_generator(instruction_block): + for instruction in instruction_block: + if isinstance(instruction, EXECInstruction): + yield instruction.waveform + elif isinstance(instruction, REPJInstruction): + for _ in range(instruction.count): + yield from get_waveform_generator(instruction.target.block[instruction.target.offset:]) + else: + return + + waveforms = [wf for wf in get_waveform_generator(sequence)] if not waveforms: return [], [] - total_time = sum([waveform.duration for waveform in waveforms]) + total_time = sum(waveform.duration for waveform in waveforms) + + channels = waveforms[0].defined_channels + + # add one sample to see the end of the waveform sample_count = total_time * self.__sample_rate + 1 times = np.linspace(0, total_time, num=sample_count) + # move the last sample inside the waveform + times[-1] = np.nextafter(times[-1], times[-2]) - channels = max([waveform.num_channels for waveform in waveforms]) - voltages = np.empty((channels, len(times))) - time = 0 + voltages = dict((ch, np.empty(len(times))) for ch in channels) + offset = 0 for waveform in waveforms: - indices = np.logical_and(times >= time, times <= time + waveform.duration) - sample_times = times[indices] - offset = times[indices][0] - time - w_voltages = waveform.sample(sample_times, offset) - voltages[:,indices] = w_voltages - time += waveform.duration + for channel in channels: + indices = slice(*np.searchsorted(times, (offset, offset+waveform.duration))) + sample_times = times[indices] - offset + output_array = voltages[channel][indices] + waveform.get_sampled(channel=channel, + sample_times=sample_times, + output_array=output_array) + offset += waveform.duration return times, voltages diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index b0ef1c1ee..09059b402 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -21,7 +21,6 @@ __all__ = ["MeasurementWindow", "PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException", "ChannelID"] - class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): """A PulseTemplate represents the parametrized general structure of a pulse. @@ -77,23 +76,17 @@ def __matmul__(self, other: 'PulseTemplate') -> 'SequencePulseTemplate': return SequencePulseTemplate(subtemplates, external_parameters) - -class AtomicPulseTemplate(PulseTemplate): - """A PulseTemplate that does not imply any control flow disruptions and can be directly - translated into a waveform. - - Implies that no AtomicPulseTemplate object is interruptable. - """ - +class PossiblyAtomicPulseTemplate(PulseTemplate): + """This PulseTemplate may be atomic.""" def __init__(self, identifier: Optional[str]=None): super().__init__(identifier=identifier) - def is_interruptable(self) -> bool: - return False - @abstractmethod - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['MultiChannelWaveform']: - """Translate this AtomicPulseTemplate into a waveform according to the given parameteres. + def build_waveform(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + """Translate this PulseTemplate into a waveform according to the given parameteres. Args: parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects. @@ -102,12 +95,33 @@ def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['MultiCha does not represent a valid waveform. """ - @abstractmethod - def get_measurement_windows(self, parameters: Dict[str, Parameter]=None) -> List[MeasurementWindow]: - """ - :param parameters: - :return: - """ + def atomic_build_sequence(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], + instruction_block: InstructionBlock): + waveform = self.build_waveform(parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) + if waveform: + instruction_block.add_instruction_exec(waveform) + + +class AtomicPulseTemplate(PossiblyAtomicPulseTemplate, metaclass=ABCMeta): + """A PulseTemplate that does not imply any control flow disruptions and can be directly + translated into a waveform. + + Implies that no AtomicPulseTemplate object is interruptable. + """ + def __init__(self, identifier: Optional[str]=None): + super().__init__(identifier=identifier) + + def is_interruptable(self) -> bool: + return False + + @property + def atomicity(self) -> bool: + return True def build_sequence(self, sequencer: 'Sequencer', @@ -116,15 +130,10 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - waveform = self.build_waveform(parameters) - if waveform: - meas_windows = self.get_measurement_windows(parameters) - - meas_windows = [(measurement_mapping[name],begin,end) for name, begin, end in meas_windows] - - instruction_block.add_instruction_exec(waveform.get_remapped(channel_mapping), meas_windows) - - + self.atomic_build_sequence(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) class DoubleParameterNameException(Exception): diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index a1681a1fd..61714d14d 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -5,7 +5,7 @@ import itertools from qctoolkit.expressions import Expression -from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration, MappedParameter, ParameterNotProvidedException from qctoolkit.pulses.instructions import ChannelID @@ -17,7 +17,7 @@ ] -class MappingTemplate(PulseTemplate): +class MappingTemplate(PossiblyAtomicPulseTemplate): def __init__(self, template: PulseTemplate, parameter_mapping: Dict[str, str], measurement_mapping: Dict[str, str] = dict(), @@ -29,7 +29,7 @@ def __init__(self, template: PulseTemplate, if mapped_internal_parameters - internal_parameters: raise UnnecessaryMappingException(template, mapped_internal_parameters - internal_parameters) elif internal_parameters - mapped_internal_parameters: - raise MissingMappingException(template,internal_parameters - mapped_internal_parameters) + raise MissingMappingException(template, internal_parameters - mapped_internal_parameters) parameter_mapping = dict((k, Expression(v)) for k, v in parameter_mapping.items()) internal_names = template.measurement_names @@ -79,6 +79,14 @@ def is_interruptable(self) -> bool: def defined_channels(self) -> Set[ChannelID]: return {self.__channel_mapping[k] for k in self.template.defined_channels} + @property + def atomicity(self) -> bool: + return self.__template.atomicity + + @atomicity.setter + def atomicity(self, val) -> None: + self.__template.atomicity = val + def get_serialization_data(self, serializer: 'Serializer') -> Dict[str,Any]: return dict(template=serializer.dictify(self.template), parameter_mapping=self.__parameter_mapping, @@ -132,9 +140,19 @@ def build_sequence(self, parameters=self.map_parameters(parameters), conditions=conditions, measurement_mapping=self.get_updated_measurement_mapping(measurement_mapping), - channel_mapping=channel_mapping, + channel_mapping=self.get_updated_channel_mapping(channel_mapping), instruction_block=instruction_block) + def build_waveform(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]): + """This gets called if the parent is atomic""" + return self.template.build_waveform( + parameters=self.map_parameters(parameters), + measurement_mapping=self.get_updated_measurement_mapping(measurement_mapping), + channel_mapping=self.get_updated_channel_mapping(channel_mapping)) + def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index 8f32661a4..001ba8dc2 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -3,11 +3,13 @@ from typing import Dict, List, Set, Optional, Union, Any +import numpy as np + from qctoolkit.serialization import Serializer -from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow +from qctoolkit.pulses.pulse_template import PulseTemplate, ChannelID, PossiblyAtomicPulseTemplate from qctoolkit.pulses.sequencing import Sequencer -from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer +from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer, Waveform from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.conditions import Condition @@ -15,7 +17,58 @@ __all__ = ["RepetitionPulseTemplate", "ParameterNotIntegerException"] -class RepetitionPulseTemplate(PulseTemplate): +class RepetitionWaveform(Waveform): + """This class allows putting multiple PulseTemplate together in one waveform on the hardware.""" + def __init__(self, body: Waveform, repetition_count: int): + """ + + :param subwaveforms: All waveforms must have the same defined channels + """ + self.__body = body + self.__repetition_count = repetition_count + if repetition_count < 1 or not isinstance(repetition_count, int): + raise ValueError('Repetition count must be an integer >0') + + @property + def defined_channels(self) -> Set[ChannelID]: + return self.__body.defined_channels + + def unsafe_sample(self, + channel: ChannelID, + sample_times: np.ndarray, + output_array: Union[np.ndarray, None]=None): + if output_array is None: + output_array = np.empty(len(sample_times)) + body_duration = self.__body.duration + for i in range(self.__repetition_count): + indices = slice(*np.searchsorted(sample_times, (i*body_duration, (i+1)*body_duration))) + self.__body.unsafe_sample(channel=channel, + sample_times=sample_times[indices], + output_array=output_array[indices]) + return output_array + + @property + def compare_key(self): + return self.__body.compare_key, self.__repetition_count + + @property + def duration(self): + return self.__body.duration*self.__repetition_count + + def get_measurement_windows(self): + def get_measurement_window_generator(body: Waveform, repetition_count: int): + body_windows = list(body.get_measurement_windows()) + for i in range(repetition_count): + for (name, begin, length) in body_windows: + yield (name, begin+i*body.duration, length) + return get_measurement_window_generator(self.__body, self.__repetition_count) + + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]): + return RepetitionWaveform(body=self.__body.unsafe_get_subset_for_channels(channels), + repetition_count=self.__repetition_count) + + +class RepetitionPulseTemplate(PossiblyAtomicPulseTemplate): """Repeat a PulseTemplate a constant number of times. The equivalent to a simple for-loop in common programming languages in qctoolkit's pulse @@ -37,6 +90,7 @@ def __init__(self, super().__init__(identifier=identifier) self.__body = body self.__repetition_count = repetition_count + self.__atomicity = False @property def body(self) -> PulseTemplate: @@ -72,6 +126,22 @@ def defined_channels(self) -> Set['ChannelID']: def measurement_names(self) -> Set[str]: return self.__body.measurement_names + @property + def atomicity(self) -> bool: + if self.__body.atomicity is False: + self.__atomicity = False + return self.__atomicity + + @atomicity.setter + def atomicity(self, val) -> None: + if val and self.__body.atomicity is False: + raise ValueError('Cannot make RepetitionPulseTemplate atomic as the body is not') + self.__atomicity = val + + def build_waveform(self, parameters: Dict[str, Parameter]) -> RepetitionWaveform: + return RepetitionWaveform(self.__body.build_waveform(parameters), + self.__repetition_count.get_value(parameters)) + def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], @@ -79,17 +149,23 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - repetition_count = self.__repetition_count - if isinstance(repetition_count, ParameterDeclaration): - repetition_count = repetition_count.get_value(parameters) - if not repetition_count.is_integer(): - raise ParameterNotIntegerException(self.__repetition_count.name, repetition_count) - - body_block = InstructionBlock() - body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) - - instruction_block.add_instruction_repj(int(repetition_count), body_block) - sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) + if self.atomicity: + self.atomic_build_sequence(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + else: + repetition_count = self.__repetition_count + if isinstance(repetition_count, ParameterDeclaration): + repetition_count = repetition_count.get_value(parameters) + if not repetition_count.is_integer(): + raise ParameterNotIntegerException(self.__repetition_count.name, repetition_count) + + body_block = InstructionBlock() + body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) + + instruction_block.add_instruction_repj(int(repetition_count), body_block) + sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) def requires_stop(self, parameters: Dict[str, Parameter], @@ -106,18 +182,22 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: return dict( type=serializer.get_type_identifier(self), body=serializer.dictify(self.__body), - repetition_count=repetition_count + repetition_count=repetition_count, + atomicity=self.atomicity ) @staticmethod def deserialize(serializer: Serializer, repetition_count: Dict[str, Any], body: Dict[str, Any], + atomicity: bool, identifier: Optional[str]=None) -> 'Serializable': body = serializer.deserialize(body) if isinstance(repetition_count, dict): repetition_count = serializer.deserialize(repetition_count) - return RepetitionPulseTemplate(body, repetition_count, identifier=identifier) + result = RepetitionPulseTemplate(body, repetition_count, identifier=identifier) + result.atomicity = atomicity + return result class ParameterNotIntegerException(Exception): diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index 643041864..e284807ac 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -1,22 +1,95 @@ """This module defines SequencePulseTemplate, a higher-order hierarchical pulse template that combines several other PulseTemplate objects for sequential execution.""" - +import numpy as np from typing import Dict, List, Tuple, Set, Optional, Any, Iterable, Union +import itertools from qctoolkit.serialization import Serializer -from qctoolkit.pulses.pulse_template import PulseTemplate, DoubleParameterNameException +from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow, PossiblyAtomicPulseTemplate from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.sequencing import InstructionBlock, Sequencer from qctoolkit.pulses.conditions import Condition from qctoolkit.pulses.pulse_template_parameter_mapping import \ MissingMappingException, MappingTemplate, ChannelID, MissingParameterDeclarationException +from qctoolkit.pulses.instructions import Waveform __all__ = ["SequencePulseTemplate"] -class SequencePulseTemplate(PulseTemplate): +class SequenceWaveform(Waveform): + """This class allows putting multiple PulseTemplate together in one waveform on the hardware.""" + def __init__(self, subwaveforms: List[Waveform]): + """ + + :param subwaveforms: All waveforms must have the same defined channels + """ + if not subwaveforms: + raise ValueError( + "SequenceWaveform cannot be constructed without channel waveforms." + ) + + def flattened_sub_waveforms(): + for sub_waveform in subwaveforms: + if isinstance(sub_waveform, SequenceWaveform): + yield from sub_waveform.__sequenced_waveforms + else: + yield sub_waveform + + self.__sequenced_waveforms = tuple(flattened_sub_waveforms()) + self.__duration = sum(waveform.duration for waveform in self.__sequenced_waveforms) + if not all(waveform.defined_channels == self.defined_channels for waveform in self.__sequenced_waveforms[1:]): + raise ValueError( + "SequenceWaveform cannot be constructed from waveforms of different" + "defined channels." + ) + + @property + def defined_channels(self) -> Set[ChannelID]: + return self.__sequenced_waveforms[0].defined_channels + + def unsafe_sample(self, + channel: ChannelID, + sample_times: np.ndarray, + output_array: Union[np.ndarray, None]=None): + if output_array is None: + output_array = np.empty(len(sample_times)) + time = 0 + for subwaveform in self.__sequenced_waveforms: + # before you change anything here, make sure to understand the difference between basic and advanced + # indexing in numpy and their copy/reference behaviour + indices = slice(*np.searchsorted(sample_times, (time, time+subwaveform.duration), 'left')) + subwaveform.unsafe_sample(channel=channel, + sample_times=sample_times[indices], + output_array=output_array[indices]) + time += subwaveform.duration + return output_array + + @property + def compare_key(self): + return self.__sequenced_waveforms + + @property + def duration(self): + return self.__duration + + def get_measurement_windows(self) -> Iterable[MeasurementWindow]: + def updated_measurement_window_generator(sequenced_waveforms): + offset = 0 + for sub_waveform in sequenced_waveforms: + for (name, begin, length) in sub_waveform.get_measurement_windows(): + yield (name, begin+offset, length) + offset += sub_waveform.duration + return updated_measurement_window_generator(self.__sequenced_waveforms) + + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + return SequenceWaveform( + sub_waveform.unsafe_get_subset_for_channels(channels & sub_waveform.defined_channels) + for sub_waveform in self.__sequenced_waveforms if sub_waveform.defined_channels & channels) + + +class SequencePulseTemplate(PossiblyAtomicPulseTemplate): """A sequence of different PulseTemplates. SequencePulseTemplate allows to group several @@ -81,6 +154,7 @@ def __init__(self, remaining = remaining - subtemplate.parameter_names if remaining: MissingMappingException(subtemplate.template,remaining.pop()) + self.__atomicity = False @property def parameter_names(self) -> Set[str]: @@ -107,6 +181,18 @@ def defined_channels(self) -> Set[ChannelID]: def measurement_names(self) -> Set[str]: return set.union(*(st.measurement_names for st in self.subtemplates)) + @property + def atomicity(self) -> bool: + if any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): + self.__atomicity = False + return self.__atomicity + + @atomicity.setter + def atomicity(self, val: bool) -> None: + if val and any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): + raise ValueError('Cannot make atomic as not all sub templates are atomic') + self.__atomicity = val + def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: @@ -114,17 +200,26 @@ def requires_stop(self, SequencePulseTemplate can be partially sequenced.""" return self.__subtemplates[0].requires_stop(parameters,conditions) if self.__subtemplates else False + def build_waveform(self, parameters: Dict[str, Parameter]): + return SequenceWaveform([subtemplate.build_waveform(parameters) for subtemplate in self.__subtemplates]) + def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], - measurement_mapping : Dict[str, str], + measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: # todo: currently ignores is_interruptable - - for subtemplate in reversed(self.subtemplates): - sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) + if self.atomicity: + self.atomic_build_sequence(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + else: + for subtemplate in reversed(self.subtemplates): + sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, + instruction_block) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict() diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index 700d2262d..488996f01 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -7,7 +7,7 @@ - Sequencer: Controller of the sequencing/translation process. """ -from abc import ABCMeta, abstractmethod +from abc import ABCMeta, abstractmethod, abstractproperty from typing import Tuple, Dict, Union, Optional import numbers @@ -28,6 +28,10 @@ class SequencingElement(metaclass=ABCMeta): def __init__(self) -> None: super().__init__() + @abstractproperty + def atomicity(self) -> bool: + """Is the element translated to a single EXECInstruction with one waveform""" + @abstractmethod def build_sequence(self, sequencer: "Sequencer", diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index c2f82ff0b..a67374dcf 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -19,12 +19,12 @@ from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow from qctoolkit.pulses.interpolation import InterpolationStrategy, LinearInterpolationStrategy, \ HoldInterpolationStrategy, JumpInterpolationStrategy -from qctoolkit.pulses.instructions import SingleChannelWaveform +from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.conditions import Condition from qctoolkit.expressions import Expression from qctoolkit.pulses.multi_channel_pulse_template import ChannelID, MultiChannelWaveform -__all__ = ["TablePulseTemplate", "TableSingleChannelWaveform", "WaveformTableEntry"] +__all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] WaveformTableEntry = NamedTuple( # pylint: disable=invalid-name "WaveformTableEntry", @@ -32,10 +32,13 @@ ) -class TableSingleChannelWaveform(SingleChannelWaveform): +class TableWaveform(Waveform): """Waveform obtained from instantiating a TablePulseTemplate.""" - def __init__(self, waveform_table: Tuple[WaveformTableEntry]) -> None: + def __init__(self, + channel: ChannelID, + waveform_table: Iterable[WaveformTableEntry], + measurement_windows: Iterable[MeasurementWindow]) -> None: """Create a new TableWaveform instance. Args: @@ -45,25 +48,41 @@ def __init__(self, waveform_table: Tuple[WaveformTableEntry]) -> None: if len(waveform_table) < 2: raise ValueError("A given waveform table has less than two entries.") super().__init__() - self.__table = waveform_table + self.__table = tuple(waveform_table) + self.__channel_id = channel + self.__measurement_windows = tuple(measurement_windows) @property def compare_key(self) -> Any: - return self.__table + return self.__channel_id, self.__table, self.__measurement_windows @property def duration(self) -> float: return self.__table[-1].t - def sample(self, sample_times: np.ndarray, first_offset: float=0) -> np.ndarray: - sample_times = sample_times.copy() - sample_times -= (sample_times[0] - first_offset) - voltages = np.empty(len(sample_times)) + def unsafe_sample(self, + channel: ChannelID, + sample_times: np.ndarray, + output_array: Union[np.ndarray, None]=None) -> np.ndarray: + if output_array is None: + output_array = np.empty(len(sample_times)) + for entry1, entry2 in zip(self.__table[:-1], self.__table[1:]): - indices = np.logical_and(sample_times >= entry1.t, sample_times <= entry2.t) - voltages[indices] = \ + indices = slice(np.searchsorted(sample_times, entry1.t, 'left'), + np.searchsorted(sample_times, entry2.t, 'right')) + output_array[indices] = \ entry2.interp((entry1.t, entry1.v), (entry2.t, entry2.v), sample_times[indices]) - return voltages + return output_array + + @property + def defined_channels(self) -> Set[ChannelID]: + return {self.__channel_id} + + def get_measurement_windows(self) -> Iterable[MeasurementWindow]: + return self.__measurement_windows + + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + return self TableValue = Union[float, ParameterDeclaration] # pylint: disable=invalid-name @@ -376,7 +395,9 @@ def parameter_declarations(self) -> Set[ParameterDeclaration]: return set(self.__time_parameter_declarations.values()) | \ set(self.__voltage_parameter_declarations.values()) - def get_measurement_windows(self, parameters: Dict[str, Parameter]) -> List[MeasurementWindow]: + def get_measurement_windows(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: def get_val(v): return v if not isinstance(v, Expression) else v.evaluate( **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] @@ -388,12 +409,11 @@ def get_val(v): resulting_windows = [] for name, windows in self.__measurement_windows.items(): for begin, end in windows: - resulting_windows.append( (name, get_val(begin), get_val(end)) ) + resulting_windows.append((measurement_mapping[name], get_val(begin), get_val(end))) if resulting_windows[-1][2] > t_max: raise ValueError('Measurement window out of pulse') return resulting_windows - @property def measurement_declarations(self): """ @@ -404,7 +424,6 @@ def measurement_declarations(self): for begin, end in windows] for name, windows in self.__measurement_windows.items() } - @property def measurement_names(self) -> Set[str]: """ @@ -434,7 +453,6 @@ def add_measurement_declaration(self, name: str, begin: Union[float,str], end: U else: self.__measurement_windows[name] = [(begin,end)] - @property def is_interruptable(self) -> bool: return False @@ -448,7 +466,7 @@ def num_channels(self) -> int: return len(self.__entries) def get_entries_instantiated(self, parameters: Dict[str, Parameter]) \ - -> Dict[ChannelID,List[Tuple[float, float]]]: + -> Dict[ChannelID, List[Tuple[float, float]]]: """Compute an instantiated list of the table's entries. Args: @@ -521,10 +539,22 @@ def __clean_entries(entries: List[Tuple[float, float]]) -> List[Tuple[float, flo entries.pop(index) return entries - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[MultiChannelWaveform]: - instantiated = self.get_entries_instantiated(parameters) - return MultiChannelWaveform({channel: TableSingleChannelWaveform(instantiated_channel) - for channel, instantiated_channel in instantiated.items()}) + def build_waveform(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + instantiated = [(channel_mapping[channel], instantiated_channel) + for channel, instantiated_channel in self.get_entries_instantiated(parameters).items()] + measurement_windows = self.get_measurement_windows(parameters=parameters, + measurement_mapping=measurement_mapping) + + if len(instantiated) == 1: + return TableWaveform(*instantiated.pop(), measurement_windows) + else: + return MultiChannelWaveform( + [TableWaveform(*instantiated.pop(), measurement_windows)] + + + [TableWaveform(channel, instantiated_channel, [])for channel, instantiated_channel in instantiated]) def requires_stop(self, parameters: Dict[str, Parameter], diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index c4e19a660..0633848b9 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -1,7 +1,7 @@ import unittest from qctoolkit.pulses.function_pulse_template import FunctionPulseTemplate,\ - FunctionSingleChannelWaveform + FunctionWaveform from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException from qctoolkit.expressions import Expression from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform @@ -81,11 +81,12 @@ def setUp(self) -> None: self.args = dict(a=DummyParameter(3),y=DummyParameter(1)) self.fpt = FunctionPulseTemplate(self.f, self.duration) + @unittest.skip def test_build_waveform(self) -> None: - wf = self.fpt.build_waveform(self.args) + wf = self.fpt.build_waveform(self.args, {}, channel_mapping={'default': 'default'}) self.assertIsNotNone(wf) self.assertIsInstance(wf, MultiChannelWaveform) - expected_waveform = MultiChannelWaveform({'default':FunctionSingleChannelWaveform(dict(a=3, y=1), Expression(self.f), Expression(self.duration))}) + expected_waveform = MultiChannelWaveform({'default':FunctionWaveform(dict(a=3, y=1), Expression(self.f), Expression(self.duration))}) self.assertEqual(expected_waveform, wf) def test_requires_stop(self) -> None: @@ -98,30 +99,35 @@ def test_requires_stop(self) -> None: class FunctionWaveformTest(unittest.TestCase): def test_equality(self) -> None: - wf1a = FunctionSingleChannelWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b')) - wf1b = FunctionSingleChannelWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b')) - wf2 = FunctionSingleChannelWaveform(dict(a=3, b=1), Expression('a*t'), Expression('b')) - wf3 = FunctionSingleChannelWaveform(dict(a=2, b=1), Expression('a*t+2'), Expression('b')) - wf4 = FunctionSingleChannelWaveform(dict(a=2, c=2), Expression('a*t'), Expression('c')) + wf1a = FunctionWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b'), channel='A', measurement_windows=[]) + wf1b = FunctionWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b'), channel='A', measurement_windows=[]) + wf2 = FunctionWaveform(dict(a=3, b=1), Expression('a*t'), Expression('b'), channel='A', measurement_windows=[]) + wf3 = FunctionWaveform(dict(a=2, b=1), Expression('a*t+2'), Expression('b'), channel='A', measurement_windows=[]) + wf4 = FunctionWaveform(dict(a=2, c=2), Expression('a*t'), Expression('c'), channel='A', measurement_windows=[]) self.assertEqual(wf1a, wf1a) self.assertEqual(wf1a, wf1b) self.assertNotEqual(wf1a, wf2) self.assertNotEqual(wf1a, wf3) self.assertNotEqual(wf1a, wf4) - def test_num_channels(self) -> None: - wf = FunctionSingleChannelWaveform(dict(), Expression('t'), Expression('4')) - self.assertEqual(1, wf.num_channels) + def test_defined_channels(self) -> None: + wf = FunctionWaveform(dict(), Expression('t'), Expression('4'), channel='A', measurement_windows=[]) + self.assertEqual({'A'}, wf.defined_channels) def test_duration(self) -> None: - wf = FunctionSingleChannelWaveform(dict(foo=2.5), Expression('2*t'), Expression('4*foo/5')) + wf = FunctionWaveform(parameters=dict(foo=2.5), + expression=Expression('2*t'), + duration_expression=Expression('4*foo/5'), + channel='A', + measurement_windows=[]) self.assertEqual(2, wf.duration) + @unittest.skip def test_sample(self) -> None: f = Expression("(t+1)**b") length = Expression("c**b") par = {"b":2,"c":10} - fw = FunctionSingleChannelWaveform(par, f, length) + fw = FunctionWaveform(par, f, length, channel='A', measurement_windows=[]) a = np.arange(4) expected_result = [[1, 4, 9, 16]] result = fw.sample(a) diff --git a/tests/pulses/instructions_tests.py b/tests/pulses/instructions_tests.py index 07a0dd23b..9f671da63 100644 --- a/tests/pulses/instructions_tests.py +++ b/tests/pulses/instructions_tests.py @@ -1,12 +1,72 @@ import unittest - +import numpy from typing import Dict, Any, List from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer,\ Trigger, CJMPInstruction, REPJInstruction, GOTOInstruction, EXECInstruction, STOPInstruction,\ InstructionSequence, AbstractInstructionBlock, ImmutableInstructionBlock, Instruction -from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstructionBlock + + +class WaveformTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + super().__init__(*args, **kwargs) + + def test_get_sampled_exceptions(self): + wf = DummyWaveform(duration=2., sample_output=[1, 2], defined_channels={'A', 'B'}) + + with self.assertRaises(ValueError): + wf.get_sampled(channel='A', + sample_times=numpy.asarray([2, 1], dtype=float)) + with self.assertRaises(ValueError): + wf.get_sampled(channel='A', + sample_times=numpy.asarray([-12, 1], dtype=float)) + with self.assertRaises(KeyError): + wf.get_sampled(channel='C', + sample_times=numpy.asarray([0.5, 1], dtype=float)) + with self.assertRaises(ValueError): + wf.get_sampled(channel='A', + sample_times=numpy.asarray([0.5, 1], dtype=float), + output_array=numpy.empty(1)) + + def test_get_sampled_caching(self): + wf = DummyWaveform(duration=2., sample_output=[1, 2], defined_channels={'A', 'B'}) + + self.assertIs(wf.get_sampled('A', sample_times=numpy.arange(2)), + wf.get_sampled('A', sample_times=numpy.arange(2))) + + def test_get_sampled_argument_forwarding(self): + wf = DummyWaveform(duration=2., sample_output=[1, 2], defined_channels={'A', 'B'}) + + out_expected = numpy.empty(2) + + out_received = wf.get_sampled('A', sample_times=numpy.arange(2), output_array=out_expected) + + self.assertIs(out_expected, out_received) + self.assertEqual(len(wf.sample_calls), 1) + self.assertIs(wf.sample_calls[0][-1], out_expected) + self.assertEqual(out_received.tolist(), [1, 2]) + + def test_get_subset_for_channels(self): + wf_ab = DummyWaveform(defined_channels={'A', 'B'}) + wf_a = DummyWaveform(defined_channels={'A'}) + + with self.assertRaises(KeyError): + wf_ab.get_subset_for_channels({'C'}) + with self.assertRaises(KeyError): + wf_ab.get_subset_for_channels({'A', 'C'}) + with self.assertRaises(KeyError): + wf_a.get_subset_for_channels({'C'}) + with self.assertRaises(KeyError): + wf_a.get_subset_for_channels({'A', 'C'}) + + self.assertIs(wf_ab, wf_ab.get_subset_for_channels({'A', 'B'})) + self.assertIs(wf_a, wf_a.get_subset_for_channels({'A'})) + + wf_sub = wf_ab.get_subset_for_channels({'A'}) + self.assertEqual(wf_sub.defined_channels, {'A'}) class InstructionPointerTest(unittest.TestCase): @@ -172,13 +232,13 @@ def test_str(self) -> None: class EXECInstructionTest(unittest.TestCase): def test_initialization(self): - waveform = DummySingleChannelWaveform() + waveform = DummyWaveform() instr = EXECInstruction(waveform) self.assertIs(waveform, instr.waveform) def test_equality(self): - wf1 = DummySingleChannelWaveform() - wf2 = DummySingleChannelWaveform() + wf1 = DummyWaveform() + wf2 = DummyWaveform() instr11 = EXECInstruction(wf1) instr12 = EXECInstruction(wf1) instr20 = EXECInstruction(wf2) @@ -191,7 +251,7 @@ def test_equality(self): self.assertNotEqual(hash(instr11), hash(instr20)) def test_str(self) -> None: - wf = DummySingleChannelWaveform() + wf = DummyWaveform() instr = EXECInstruction(wf) self.assertEqual("exec {}".format(str(wf)), str(instr)) @@ -239,7 +299,7 @@ def test_len_empty(self) -> None: self.assertEqual(0, len(block.instructions)) def test_len(self) -> None: - block = AbstractInstructionBlockStub([EXECInstruction(DummySingleChannelWaveform())], None) + block = AbstractInstructionBlockStub([EXECInstruction(DummyWaveform())], None) self.assertEqual(2, len(block)) self.assertEqual(1, len(block.instructions)) @@ -262,7 +322,7 @@ def test_iterable_empty_return(self) -> None: count += 1 def test_iterable_no_return(self) -> None: - wf = DummySingleChannelWaveform() + wf = DummyWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], None) count = 0 for expected_instruction, instruction in zip([EXECInstruction(wf), STOPInstruction()], block): @@ -272,7 +332,7 @@ def test_iterable_no_return(self) -> None: def test_iterable_return(self) -> None: parent_block = InstructionBlock() - wf = DummySingleChannelWaveform() + wf = DummyWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], InstructionPointer(parent_block, 11)) count = 0 for expected_instruction, instruction in zip([EXECInstruction(wf), GOTOInstruction(InstructionPointer(parent_block, 11))], block): @@ -300,7 +360,7 @@ def test_item_access_empty_return(self) -> None: block[-2] def test_item_access_no_return(self) -> None: - wf = DummySingleChannelWaveform() + wf = DummyWaveform() block = AbstractInstructionBlockStub([EXECInstruction(wf)], None) self.assertEqual(EXECInstruction(wf), block[0]) self.assertEqual(STOPInstruction(), block[1]) @@ -312,7 +372,7 @@ def test_item_access_no_return(self) -> None: block[-3] def test_item_access_return(self) -> None: - wf = DummySingleChannelWaveform() + wf = DummyWaveform() parent_block = InstructionBlock() block = AbstractInstructionBlockStub([EXECInstruction(wf)], InstructionPointer(parent_block, 29)) self.assertEqual(EXECInstruction(wf), block[0]) @@ -325,7 +385,7 @@ def test_item_access_return(self) -> None: block[-3] def test_sliced_item_access(self) -> None: - wf = DummySingleChannelWaveform() + wf = DummyWaveform() parent_block = InstructionBlock() block = AbstractInstructionBlockStub([EXECInstruction(wf), EXECInstruction(wf)], InstructionPointer(parent_block, 29)) for instruction in block[:-1]: @@ -380,7 +440,7 @@ def test_add_instruction_exec(self) -> None: block = InstructionBlock() expected_instructions = [] - waveforms = [DummySingleChannelWaveform(), DummySingleChannelWaveform(), DummySingleChannelWaveform()] + waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] LOOKUP = [0, 1, 1, 0, 2, 1, 0, 0, 0, 1, 2, 2] for id in LOOKUP: waveform = waveforms[id] @@ -454,7 +514,7 @@ def test_nested_block_construction(self) -> None: blocks = [] - waveforms = [DummySingleChannelWaveform(), DummySingleChannelWaveform(), DummySingleChannelWaveform()] + waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] main_block.add_instruction_exec(waveforms[0]) expected_instructions[0].append(EXECInstruction(waveforms[0])) @@ -607,7 +667,7 @@ def test_nested_goto(self) -> None: def test_multiple_nested_block_construction(self) -> None: main_block = InstructionBlock() blocks = [] - waveforms = [DummySingleChannelWaveform(), DummySingleChannelWaveform(), DummySingleChannelWaveform()] + waveforms = [DummyWaveform(), DummyWaveform(), DummyWaveform()] main_block.add_instruction_exec(waveforms[0]) @@ -652,7 +712,7 @@ class InstructionStringRepresentation(unittest.TestCase): def test_str(self) -> None: IB = InstructionBlock() T = Trigger() - W = DummySingleChannelWaveform() + W = DummyWaveform() a = [W, T, diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index 3dd499226..0588fb54a 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -9,7 +9,7 @@ from qctoolkit.expressions import Expression from qctoolkit.pulses.instructions import CHANInstruction, EXECInstruction -from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummySingleChannelWaveform +from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform from tests.serialization_dummies import DummySerializer @@ -22,76 +22,112 @@ def test_init_no_args(self) -> None: MultiChannelWaveform(None) def test_init_single_channel(self) -> None: - dwf = DummySingleChannelWaveform(duration=1.3) + dwf = DummyWaveform(duration=1.3, defined_channels={'A'}) - waveform = MultiChannelWaveform({0: dwf}) - self.assertEqual({0}, waveform.defined_channels) + waveform = MultiChannelWaveform([dwf]) + self.assertEqual({'A'}, waveform.defined_channels) self.assertEqual(1.3, waveform.duration) def test_init_several_channels(self) -> None: - dwfa = DummySingleChannelWaveform(duration=4.2) - dwfb = DummySingleChannelWaveform(duration=4.2) - dwfc = DummySingleChannelWaveform(duration=2.3) + dwf_a = DummyWaveform(duration=2.2, defined_channels={'A'}) + dwf_b = DummyWaveform(duration=2.2, defined_channels={'B'}) + dwf_c = DummyWaveform(duration=2.3, defined_channels={'C'}) - waveform = MultiChannelWaveform({0: dwfa, 1: dwfb}) - self.assertEqual({0, 1}, waveform.defined_channels) - self.assertEqual(4.2, waveform.duration) + waveform = MultiChannelWaveform([dwf_a, dwf_b]) + self.assertEqual({'A', 'B'}, waveform.defined_channels) + self.assertEqual(2.2, waveform.duration) with self.assertRaises(ValueError): - MultiChannelWaveform({0: dwfa, 1: dwfc}) + MultiChannelWaveform([dwf_a, dwf_c]) + with self.assertRaises(ValueError): + MultiChannelWaveform([waveform, dwf_c]) + with self.assertRaises(ValueError): + MultiChannelWaveform((dwf_a, dwf_a)) + + dwf_c_valid = DummyWaveform(duration=2.2, defined_channels={'C'}) + waveform_flat = MultiChannelWaveform((waveform, dwf_c_valid)) + self.assertEqual(len(waveform_flat.compare_key), 3) - @unittest.skip("Think where to put equivalent code.") - def test_sample(self) -> None: + def test_unsafe_sample(self) -> None: sample_times = numpy.linspace(98.5, 103.5, num=11) - samples_a = numpy.array([ - [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], - [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5] -# [0, 0.5], [1, 0.6], [2, 0.7], [3, 0.8], [4, 0.9], [5, 1.0], -# [6, 1.1], [7, 1.2], [8, 1.3], [9, 1.4], [10, 1.5] - ]) - samples_b = numpy.array([ - [-10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20] -# [-10], [-11], [-12], [-13], [-14], [-15], [-16], [-17], [-18], [-19], [-20] - ]) - dwf_a = DummySingleChannelWaveform(duration=3.2, sample_output=samples_a, defined_channels={'A'}) - dwf_b = DummySingleChannelWaveform(duration=3.2, sample_output=samples_b, defined_channels={'A'}) - waveform = MultiChannelWaveform([(dwf_a, [2, 0]), (dwf_b, [1])]) - self.assertEqual(3, waveform.num_channels) - self.assertEqual(3.2, waveform.duration) - - result = waveform.sample(sample_times, 0.7) - self.assertEqual([(list(sample_times), 0.7)], dwf_a.sample_calls) - self.assertEqual([(list(sample_times), 0.7)], dwf_b.sample_calls) - - # expected = numpy.array([ - # [0.5, -10, 0], - # [0.6, -11, 1], - # [0.7, -12, 2], - # [0.8, -13, 3], - # [0.9, -14, 4], - # [1.0, -15, 5], - # [1.1, -16, 6], - # [1.2, -17, 7], - # [1.3, -18, 8], - # [1.4, -19, 9], - # [1.5, -20, 10], - # ]) - expected = numpy.array([ - [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5], - [-10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20], - [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] - ]) - self.assertTrue(numpy.all(expected == result)) + samples_a = numpy.linspace(4, 5, 11) + samples_b = numpy.linspace(2, 3, 11) + dwf_a = DummyWaveform(duration=3.2, sample_output=samples_a, defined_channels={'A'}) + dwf_b = DummyWaveform(duration=3.2, sample_output=samples_b, defined_channels={'B', 'C'}) + waveform = MultiChannelWaveform((dwf_a, dwf_b)) + + result_a = waveform.unsafe_sample('A', sample_times) + numpy.testing.assert_equal(result_a, samples_a) + + result_b = waveform.unsafe_sample('B', sample_times) + numpy.testing.assert_equal(result_b, samples_b) + + self.assertEqual(len(dwf_a.sample_calls), 1) + self.assertEqual(len(dwf_b.sample_calls), 1) + + numpy.testing.assert_equal(sample_times, dwf_a.sample_calls[0][1]) + numpy.testing.assert_equal(sample_times, dwf_b.sample_calls[0][1]) + + self.assertEqual('A', dwf_a.sample_calls[0][0]) + self.assertEqual('B', dwf_b.sample_calls[0][0]) + + self.assertIs(dwf_a.sample_calls[0][2], None) + self.assertIs(dwf_b.sample_calls[0][2], None) + + reuse_output = numpy.empty_like(samples_a) + result_a = waveform.unsafe_sample('A', sample_times, reuse_output) + self.assertEqual(len(dwf_a.sample_calls), 2) + self.assertIs(result_a, reuse_output) + self.assertIs(result_a, dwf_a.sample_calls[1][2]) + numpy.testing.assert_equal(result_b, samples_b) def test_equality(self) -> None: - dwf_a = DummySingleChannelWaveform(duration=246.2) - waveform_a1 = MultiChannelWaveform({0: dwf_a, 1: dwf_a}) - waveform_a2 = MultiChannelWaveform({0: dwf_a, 1: dwf_a}) - waveform_a3 = MultiChannelWaveform({0: dwf_a, 2: dwf_a}) + dwf_a = DummyWaveform(duration=246.2, defined_channels={'A'}) + dwf_b = DummyWaveform(duration=246.2, defined_channels={'B'}) + dwf_c = DummyWaveform(duration=246.2, defined_channels={'C'}) + waveform_a1 = MultiChannelWaveform([dwf_a, dwf_b]) + waveform_a2 = MultiChannelWaveform([dwf_a, dwf_b]) + waveform_a3 = MultiChannelWaveform([dwf_a, dwf_c]) self.assertEqual(waveform_a1, waveform_a1) self.assertEqual(waveform_a1, waveform_a2) self.assertNotEqual(waveform_a1, waveform_a3) + def test_get_measurement_windows(self): + def meas_window(i): + return str(int(i)), i, i+1 + + dwf_a = DummyWaveform(duration=246.2, defined_channels={'A'}, + measurement_windows=[meas_window(1), meas_window(2)]) + dwf_b = DummyWaveform(duration=246.2, defined_channels={'B'}, + measurement_windows=[meas_window(3), meas_window(4), meas_window(5)]) + dwf_c = DummyWaveform(duration=246.2, defined_channels={'C'}) + + mcwf = MultiChannelWaveform((dwf_a, dwf_b, dwf_c)) + expected_windows = set(meas_window(i) for i in range(1, 6)) + received_windows = tuple(mcwf.get_measurement_windows()) + self.assertEqual(len(received_windows), 5) + self.assertEqual(set(received_windows), expected_windows) + + def test_unsafe_get_subset_for_channels(self): + dwf_a = DummyWaveform(duration=246.2, defined_channels={'A'}) + dwf_b = DummyWaveform(duration=246.2, defined_channels={'B'}) + dwf_c = DummyWaveform(duration=246.2, defined_channels={'C'}) + + mcwf = MultiChannelWaveform((dwf_a, dwf_b, dwf_c)) + with self.assertRaises(KeyError): + mcwf.unsafe_get_subset_for_channels({'D'}) + with self.assertRaises(KeyError): + mcwf.unsafe_get_subset_for_channels({'A', 'D'}) + + self.assertIs(mcwf.unsafe_get_subset_for_channels({'A'}), dwf_a) + self.assertIs(mcwf.unsafe_get_subset_for_channels({'B'}), dwf_b) + self.assertIs(mcwf.unsafe_get_subset_for_channels({'C'}), dwf_c) + + sub_ab = mcwf.unsafe_get_subset_for_channels({'A', 'B'}) + self.assertEqual(sub_ab.defined_channels, {'A', 'B'}) + self.assertIsInstance(sub_ab, MultiChannelWaveform) + self.assertIs(sub_ab.unsafe_get_subset_for_channels({'A'}), dwf_a) + self.assertIs(sub_ab.unsafe_get_subset_for_channels({'B'}), dwf_b) class MultiChannelPulseTemplateTest(unittest.TestCase): def __init__(self,*args,**kwargs): @@ -191,8 +227,8 @@ def test_requires_stop_true_mapped_parameters(self) -> None: self.assertTrue(pulse.requires_stop(parameters, dict())) def test_build_sequence(self) -> None: - dummy_wf1 = DummySingleChannelWaveform(duration=2.3) - dummy_wf2 = DummySingleChannelWaveform(duration=2.3) + dummy_wf1 = DummyWaveform(duration=2.3) + dummy_wf2 = DummyWaveform(duration=2.3) dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1) dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2) @@ -278,8 +314,10 @@ def test_get_serialization_data(self) -> None: {'foo'}, identifier='herbert' ) + template.atomicity = True expected_data = dict( - subtemplates = [str(id(self.dummy1)), str(id(self.dummy2))], + subtemplates=[str(id(self.dummy1)), str(id(self.dummy2))], + atomicity=True, type=serializer.get_type_identifier(template)) data = template.get_serialization_data(serializer) self.assertEqual(expected_data, data) @@ -290,7 +328,8 @@ def test_deserialize(self) -> None: serializer.subelements[str(id(self.dummy2))] = self.dummy2 data = dict( - subtemplates=[str(id(self.dummy1)), str(id(self.dummy2))]) + subtemplates=[str(id(self.dummy1)), str(id(self.dummy2))], + atomicity=False) template = MultiChannelPulseTemplate.deserialize(serializer, **data) for st_expected, st_found in zip( [self.dummy1, self.dummy2], template.subtemplates ): diff --git a/tests/pulses/plotting_tests.py b/tests/pulses/plotting_tests.py index 22d83bb2a..d5fc42ce9 100644 --- a/tests/pulses/plotting_tests.py +++ b/tests/pulses/plotting_tests.py @@ -7,7 +7,7 @@ from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate from qctoolkit.pulses.sequencing import Sequencer -from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummyInstruction +from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstruction class PlotterTests(unittest.TestCase): @@ -23,24 +23,44 @@ def test_render_no_waveforms(self) -> None: self.assertEqual(([], []), Plotter().render(InstructionBlock())) def test_render(self) -> None: - wf1 = DummySingleChannelWaveform(duration=19) - wf2 = DummySingleChannelWaveform(duration=21) + wf1 = DummyWaveform(duration=19) + wf2 = DummyWaveform(duration=21) block = InstructionBlock() block.add_instruction_exec(wf1) block.add_instruction_exec(wf2) + wf1_expected = ('A', [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]) + wf2_expected = ('A', [x-19 for x in [20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40]]) + wf1_output_array_len_expected = len(wf1_expected[1]) + wf2_output_array_len_expected = len(wf2_expected[1]) + + wf1.sample_output = numpy.linspace(start=4, stop=5, num=len(wf1_expected[1])) + wf2.sample_output = numpy.linspace(6, 7, num=len(wf2_expected[1])) + + expected_times = numpy.arange(start=0, stop=42, step=2) + expected_result = numpy.concatenate((wf1.sample_output, wf2.sample_output)) + plotter = Plotter(sample_rate=0.5) times, voltages = plotter.render(block) - wf1_expected = [([0, 2, 4, 6, 8, 10, 12, 14, 16, 18], 0)] - wf2_expected = [([20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40], 1)] - expected_result = numpy.array([range(0, 41, 2)]) - self.assertEqual(wf1_expected, wf1.sample_calls) - self.assertEqual(wf2_expected, wf2.sample_calls) - self.assertTrue(numpy.all(expected_result == times)) - self.assertTrue(numpy.all(expected_result == voltages)) - self.assertEqual(expected_result.shape, voltages.shape) + self.assertEqual(len(wf1.sample_calls), 1) + self.assertEqual(len(wf2.sample_calls), 1) + + self.assertEqual(wf1_expected[0], wf1.sample_calls[0][0]) + self.assertEqual(wf2_expected[0], wf2.sample_calls[0][0]) + + numpy.testing.assert_almost_equal(wf1_expected[1], wf1.sample_calls[0][1]) + numpy.testing.assert_almost_equal(wf2_expected[1], wf2.sample_calls[0][1]) + + self.assertEqual(wf1_output_array_len_expected, len(wf1.sample_calls[0][2])) + self.assertEqual(wf2_output_array_len_expected, len(wf2.sample_calls[0][2])) + + self.assertEqual(voltages.keys(), dict(A=0).keys()) + + numpy.testing.assert_almost_equal(expected_times, times) + numpy.testing.assert_almost_equal(expected_result, voltages['A']) + self.assertEqual(expected_result.shape, voltages['A'].shape) def integrated_test_with_sequencer_and_pulse_templates(self) -> None: # Setup test data diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index 94eef6e3e..cf7e3ea73 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -1,13 +1,14 @@ import unittest +import copy from typing import Optional, Dict, Set, Any, List from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow -from qctoolkit.pulses.instructions import SingleChannelWaveform, EXECInstruction +from qctoolkit.pulses.instructions import Waveform, EXECInstruction from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummySequencer, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummyWaveform, DummySequencer, DummyInstructionBlock class AtomicPulseTemplateStub(AtomicPulseTemplate): @@ -15,15 +16,16 @@ class AtomicPulseTemplateStub(AtomicPulseTemplate): def is_interruptable(self) -> bool: return super().is_interruptable() - def __init__(self, waveform: SingleChannelWaveform, measurement_windows: List[MeasurementWindow] = [], + def __init__(self, waveform: Waveform, measurement_windows: List[MeasurementWindow] = [], identifier: Optional[str]=None) -> None: super().__init__(identifier=identifier) self.waveform = waveform self.measurement_windows = measurement_windows - def build_waveform(self, parameters: Dict[str, Parameter]): + def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping, channel_mapping): return self.waveform + def get_measurement_windows(self, parameters: Dict[str, Parameter] = None): return self.measurement_windows @@ -59,7 +61,7 @@ def deserialize(serializer: 'Serializer', **kwargs) -> 'AtomicPulseTemplateStub' class AtomicPulseTemplateTests(unittest.TestCase): def test_is_interruptable(self) -> None: - wf = DummySingleChannelWaveform() + wf = DummyWaveform() template = AtomicPulseTemplateStub(wf) self.assertFalse(template.is_interruptable()) template = AtomicPulseTemplateStub(wf, identifier="arbg4") @@ -74,16 +76,16 @@ def test_build_sequence_no_waveform(self) -> None: self.assertFalse(block.instructions) def test_build_sequence(self) -> None: - single_wf = DummySingleChannelWaveform(duration=6) - wf = MultiChannelWaveform(dict(A=single_wf)) measurement_windows = [('M', 0, 5)] + single_wf = DummyWaveform(duration=6, defined_channels={'A'}, measurement_windows=measurement_windows) + wf = MultiChannelWaveform([single_wf]) + sequencer = DummySequencer() block = DummyInstructionBlock() - template = AtomicPulseTemplateStub(wf,measurement_windows) - template.build_sequence(sequencer, {}, {}, measurement_mapping={'M': 'M2'}, channel_mapping={'A': 'B'}, instruction_block=block) + template = AtomicPulseTemplateStub(wf, measurement_windows) + template.build_sequence(sequencer, {}, {}, measurement_mapping={}, channel_mapping={}, instruction_block=block) self.assertEqual(len(block.instructions), 1) self.assertIsInstance(block.instructions[0], EXECInstruction) - self.assertEqual(block.instructions[0].waveform.defined_channels, {'B'}) - self.assertEqual(block.instructions[0].waveform['B'], single_wf) - self.assertEqual(block.instructions[0].measurement_windows, [('M2', 0, 5)]) \ No newline at end of file + self.assertEqual(block.instructions[0].waveform.defined_channels, {'A'}) + self.assertEqual(list(block.instructions[0].waveform.get_measurement_windows()), [('M', 0, 5)]) \ No newline at end of file diff --git a/tests/pulses/repetition_pulse_template_tests.py b/tests/pulses/repetition_pulse_template_tests.py index 26ecd6877..fc5dde550 100644 --- a/tests/pulses/repetition_pulse_template_tests.py +++ b/tests/pulses/repetition_pulse_template_tests.py @@ -1,13 +1,45 @@ import unittest -from qctoolkit.pulses.repetition_pulse_template import RepetitionPulseTemplate,ParameterNotIntegerException +import numpy as np + +from qctoolkit.pulses.repetition_pulse_template import RepetitionPulseTemplate,ParameterNotIntegerException, RepetitionWaveform from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException from qctoolkit.pulses.instructions import REPJInstruction, InstructionPointer -from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummySequencer, DummyInstructionBlock, DummyParameter, DummyCondition +from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummySequencer, DummyInstructionBlock, DummyParameter,\ + DummyCondition, DummyWaveform from tests.serialization_dummies import DummySerializer +class RepetitionWaveformTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_unsafe_sample(self): + body_wf = DummyWaveform(duration=7) + + rwf = RepetitionWaveform(body=body_wf, repetition_count=10) + + sample_times = np.arange(80) * 70./80. + np.testing.assert_equal(rwf.unsafe_sample(channel='A', sample_times=sample_times), sample_times) + + output_expected = np.empty_like(sample_times) + output_received = rwf.unsafe_sample(channel='A', sample_times=sample_times, output_array=output_expected) + self.assertIs(output_expected, output_received) + np.testing.assert_equal(output_received, sample_times) + + def test_get_measurement_windows(self): + body_wf = DummyWaveform(duration=7, measurement_windows=[('M', .1, .2), ('N', .5, .7)]) + + rwf = RepetitionWaveform(body=body_wf, repetition_count=3) + + expected_windows = [('M', .1, .2), ('N', .5, .7), + ('M', 7.1, .2), ('N', 7.5, .7), + ('M', 14.1, .2), ('N', 14.5, .7)] + received_windows = list(rwf.get_measurement_windows()) + self.assertEqual(received_windows, expected_windows) + + class RepetitionPulseTemplateTest(unittest.TestCase): def test_init(self) -> None: @@ -145,7 +177,8 @@ def test_get_serialization_data_constant(self) -> None: expected_data = dict( type=self.serializer.get_type_identifier(template), body=str(id(self.body)), - repetition_count=repetition_count + repetition_count=repetition_count, + atomicity=False ) data = template.get_serialization_data(self.serializer) self.assertEqual(expected_data, data) @@ -153,10 +186,12 @@ def test_get_serialization_data_constant(self) -> None: def test_get_serialization_data_declaration(self) -> None: repetition_count = ParameterDeclaration('foo') template = RepetitionPulseTemplate(self.body, repetition_count) + template.atomicity = True expected_data = dict( type=self.serializer.get_type_identifier(template), body=str(id(self.body)), - repetition_count=str(id(repetition_count)) + repetition_count=str(id(repetition_count)), + atomicity=True ) data = template.get_serialization_data(self.serializer) self.assertEqual(expected_data, data) @@ -166,7 +201,8 @@ def test_deserialize_constant(self) -> None: data = dict( repetition_count=repetition_count, body=dict(name=str(id(self.body))), - identifier='foo' + identifier='foo', + atomicity=True ) # prepare dependencies for deserialization self.serializer.subelements[str(id(self.body))] = self.body @@ -175,13 +211,15 @@ def test_deserialize_constant(self) -> None: # compare! self.assertEqual(self.body, template.body) self.assertEqual(repetition_count, template.repetition_count) + self.assertTrue(template.atomicity) def test_deserialize_declaration(self) -> None: repetition_count = ParameterDeclaration('foo') data = dict( repetition_count=dict(name='foo'), body=dict(name=str(id(self.body))), - identifier='foo' + identifier='foo', + atomicity=False ) # prepare dependencies for deserialization self.serializer.subelements[str(id(self.body))] = self.body @@ -191,6 +229,7 @@ def test_deserialize_declaration(self) -> None: # compare! self.assertEqual(self.body, template.body) self.assertEqual(repetition_count, template.repetition_count) + self.assertFalse(template.atomicity) class ParameterNotIntegerExceptionTests(unittest.TestCase): diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index 0e5e589bd..d88b49be5 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -1,16 +1,68 @@ import unittest import copy +import numpy as np + from qctoolkit.pulses.pulse_template import DoubleParameterNameException from qctoolkit.expressions import Expression from qctoolkit.pulses.table_pulse_template import TablePulseTemplate -from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate +from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate, SequenceWaveform from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ConstantParameter -from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyNoValueParameter +from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate,\ + DummyNoValueParameter, DummyWaveform from tests.serialization_dummies import DummySerializer + +class SequenceWaveformTest(unittest.TestCase): + def __init__(self, *args, **kwargs) -> None: + super().__init__(*args, **kwargs) + + def test_init(self): + dwf_ab = DummyWaveform(duration=1.1, defined_channels={'A', 'B'}) + dwf_abc = DummyWaveform(duration=2.2, defined_channels={'A', 'B', 'C'}) + + with self.assertRaises(ValueError): + SequenceWaveform((dwf_ab, dwf_abc)) + + swf1 = SequenceWaveform((dwf_ab, dwf_ab)) + self.assertEqual(swf1.duration, 2*dwf_ab.duration) + self.assertEqual(len(swf1.compare_key), 2) + + swf2 = SequenceWaveform((swf1, dwf_ab)) + self.assertEqual(swf2.duration, 3 * dwf_ab.duration) + + self.assertEqual(len(swf2.compare_key), 3) + + def test_unsafe_sample(self): + dwfs = (DummyWaveform(duration=1., sample_output=np.linspace(5, 6, num=10)), + DummyWaveform(duration=3., sample_output=np.linspace(1, 2, num=30)), + DummyWaveform(duration=2., sample_output=np.linspace(8, 9, num=20))) + + swf = SequenceWaveform(dwfs) + + sample_times = np.arange(0, 60)*0.1 + expected_output = np.concatenate(tuple(dwf.sample_output for dwf in dwfs)) + + output = swf.unsafe_sample('A', sample_times=sample_times) + np.testing.assert_equal(expected_output, output) + + output_2 = swf.unsafe_sample('A', sample_times=sample_times, output_array=output) + self.assertIs(output_2, output) + + def test_get_measurement_windows(self): + dwfs = (DummyWaveform(duration=1., measurement_windows=[('M', 0.2, 0.5)]), + DummyWaveform(duration=3., measurement_windows=[('N', 0.6, 0.7)]), + DummyWaveform(duration=2., measurement_windows=[('M', 0.1, 0.2), ('N', 0.5, 0.6)])) + swf = SequenceWaveform(dwfs) + + expected_windows = (('M', 0.2, 0.5), ('N', 1.6, 1.7), ('M', 4.1, 4.2), ('N', 4.5, 4.6)) + received_windows = tuple(swf.get_measurement_windows()) + self.assertEqual(len(received_windows), len(expected_windows)) + self.assertEqual(set(received_windows), set(expected_windows)) + + class SequencePulseTemplateTest(unittest.TestCase): def __init__(self, *args, **kwargs) -> None: @@ -126,15 +178,6 @@ def test_deserialize(self) -> None: class SequencePulseTemplateSequencingTests(SequencePulseTemplateTest): - @unittest.skip("The test for missing parameters is performed on the lowest level.") - def test_missing_parameter(self): - sequencer = DummySequencer() - block = DummyInstructionBlock() - parameters = copy.deepcopy(self.parameters) - parameters.pop('uptime') - with self.assertRaises(ParameterNotProvidedException): - self.sequence.build_sequence(sequencer, parameters, {}, {}, {}, block) - def test_build_sequence(self) -> None: sub1 = DummyPulseTemplate(requires_stop=False) sub2 = DummyPulseTemplate(requires_stop=True, parameter_names={'foo'}) diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index d1d41ed8b..61fb1bf0c 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -1,11 +1,12 @@ """STANDARD LIBRARY IMPORTS""" from typing import Tuple, List, Dict, Optional, Set, Any +import copy import numpy """LOCAL IMPORTS""" from qctoolkit.serialization import Serializer -from qctoolkit.pulses.instructions import SingleChannelWaveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction +from qctoolkit.pulses.instructions import Waveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock, SequencingElement from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow @@ -68,6 +69,7 @@ def __init__(self, requires_stop: bool = False, push_elements: Tuple[Instruction self.push_elements = push_elements self.parameter_names = set() self.condition_names = set() + self.atomicity_ = False def build_sequence(self, sequencer: Sequencer, @@ -93,6 +95,10 @@ def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, self.conditions = conditions return self.requires_stop_ + @property + def atomicity(self): + return self.atomicity_ + class DummyInstruction(Instruction): @@ -117,19 +123,20 @@ def add_instruction(self, instruction: Instruction) -> None: self.embedded_blocks.append(instruction.target.block) -class DummySingleChannelWaveform(SingleChannelWaveform): +class DummyWaveform(Waveform): - def __init__(self, duration: float=0, sample_output: numpy.ndarray=None, num_channels: int=1) -> None: + def __init__(self, duration: float=0, sample_output: numpy.ndarray=None, defined_channels={'A'}, measurement_windows=[]) -> None: super().__init__() self.duration_ = duration self.sample_output = sample_output - self.num_channels_ = num_channels + self.defined_channels_ = defined_channels self.sample_calls = [] + self.measurement_windows_ = measurement_windows @property def compare_key(self) -> Any: if self.sample_output is not None: - return tuple(self.sample_output.tolist()) + return hash(bytes(self.sample_output)) else: return id(self) @@ -137,19 +144,36 @@ def compare_key(self) -> Any: def duration(self) -> float: return self.duration_ - @property - def num_channels(self) -> int: - return self.num_channels_ - @property def measurement_windows(self): return [] - def sample(self, sample_times: numpy.ndarray, first_offset: float=0) -> numpy.ndarray: - self.sample_calls.append((list(sample_times), first_offset)) + def unsafe_sample(self, + channel: ChannelID, + sample_times: numpy.ndarray, + output_array: numpy.ndarray = None) -> numpy.ndarray: + self.sample_calls.append((channel, list(sample_times), output_array)) + if output_array is None: + output_array = numpy.empty_like(sample_times) if self.sample_output is not None: - return self.sample_output - return sample_times + output_array[:] = self.sample_output + else: + output_array[:] = sample_times + return output_array + + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + if not channels <= self.defined_channels_: + raise KeyError('channels not in defined_channels') + c = copy.copy(self) + c.defined_channels_ = channels + return c + + @property + def defined_channels(self): + return self.defined_channels_ + + def get_measurement_windows(self): + return self.measurement_windows_ class DummySequencer(Sequencer): @@ -256,7 +280,7 @@ def __init__(self, parameter_names: Set[str]={}, defined_channels: Set[ChannelID]={'default'}, duration: float=0, - waveform: SingleChannelWaveform=None, + waveform: Waveform=None, measurement_names: Set[str] = set()) -> None: super().__init__() self.requires_stop_ = requires_stop @@ -308,11 +332,14 @@ def build_sequence(self, instruction_block: InstructionBlock): self.build_sequence_arguments.append((sequencer,parameters,conditions, measurement_mapping, channel_mapping, instruction_block)) - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional[SingleChannelWaveform]: - self.build_waveform_calls.append(parameters) + def build_waveform(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]): + self.build_waveform_calls.append((parameters, measurement_mapping, channel_mapping)) if self.waveform is not None: return self.waveform - return DummySingleChannelWaveform(duration=self.duration, num_channels=self.num_channels) + return DummyWaveform(duration=self.duration, defined_channels=self.defined_channels) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition]) -> bool: self.requires_stop_arguments.append((parameters,conditions)) diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 721faac15..160af2242 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -2,7 +2,7 @@ import numpy from qctoolkit.pulses.instructions import EXECInstruction -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableSingleChannelWaveform, TableEntry, WaveformTableEntry +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform @@ -44,15 +44,15 @@ def test_measurement_windows(self) -> None: pulse.add_entry(3, 0) pulse.add_entry(5, 0) pulse.add_measurement_declaration('mw',0,5) - windows = pulse.get_measurement_windows({}) - self.assertEqual([('mw',0,5)], windows) + windows = pulse.get_measurement_windows(parameters={}, measurement_mapping={'mw': 'asd'}) + self.assertEqual([('asd', 0, 5)], windows) def test_no_measurement_windows(self) -> None: pulse = TablePulseTemplate() pulse.add_entry(1, 1) pulse.add_entry(3, 0) pulse.add_entry(5, 0) - windows = pulse.get_measurement_windows({}) + windows = pulse.get_measurement_windows({}, {'mw': 'asd'}) self.assertEqual([], windows) def test_measurement_windows_with_parameters(self) -> None: @@ -61,8 +61,8 @@ def test_measurement_windows_with_parameters(self) -> None: pulse.add_entry('length', 0) pulse.add_measurement_declaration('mw',1,'(1+length)/2') parameters = dict(length=100) - windows = pulse.get_measurement_windows(parameters) - self.assertEqual(windows, [('mw', 1, 101/2)]) + windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) + self.assertEqual(windows, [('asd', 1, 101/2)]) def test_add_entry_empty_time_is_negative(self) -> None: table = TablePulseTemplate() @@ -457,7 +457,9 @@ def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries_doe ] self.assertEqual(expected, entries) - result_sampled = TableSingleChannelWaveform(entries).sample(numpy.linspace(0, 10, 11), 0) + result_sampled = TableWaveform(channel='A', waveform_table=entries, measurement_windows=[]).get_sampled( + channel='A', + sample_times=numpy.linspace(0, 10, 11)) numbers = [5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0] expected = [float(x) for x in numbers] @@ -611,8 +613,8 @@ def test_measurement_windows_multi(self) -> None: pulse.add_entry(10, 0, channel=1) pulse.add_measurement_declaration('mw',1,7) - windows = pulse.get_measurement_windows({}) - self.assertEqual([('mw',1,7)], windows) + windows = pulse.get_measurement_windows({}, measurement_mapping={'mw': 'asd'}) + self.assertEqual([('asd',1,7)], windows) def test_measurement_windows_multi_out_of_pulse(self) -> None: pulse = TablePulseTemplate(channels=[0, 1]) @@ -626,7 +628,7 @@ def test_measurement_windows_multi_out_of_pulse(self) -> None: with self.assertRaises(ValueError): pulse.add_measurement_declaration('mw', 1, 't_meas') - pulse.get_measurement_windows({'t_meas':20}) + pulse.get_measurement_windows({'t_meas': 20}, measurement_mapping={'mw': 'asd'}) class TablePulseTemplateSerializationTests(unittest.TestCase): @@ -696,13 +698,21 @@ def test_build_sequence(self) -> None: table.add_entry(bar_decl, 0, 'jump') parameters = {'v': 2.3, 'foo': 1, 'bar': 4} instantiated_entries = table.get_entries_instantiated(parameters) - waveform = table.build_waveform(parameters) + channel_mapping = {'default': 'default'} + waveform = table.build_waveform(parameters, + measurement_mapping={}, + channel_mapping=channel_mapping) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() - channel_mapping = {'default': 'default'} + table.build_sequence(sequencer, parameters, {}, {}, channel_mapping, instruction_block) - expected_waveform = MultiChannelWaveform({channel: TableSingleChannelWaveform(inst) - for channel,inst in instantiated_entries.items()}) + if len(instantiated_entries) == 1: + expected_waveform = TableWaveform(*instantiated_entries.popitem(), measurement_windows=[]) + else: + expected_waveform = MultiChannelWaveform([TableWaveform(channel=channel, + waveform_table=inst, + measurement_windows=[]) + for channel, inst in instantiated_entries.items()]) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) @@ -762,8 +772,9 @@ def test_build_sequence_multi(self) -> None: instantiated_entries = table.get_entries_instantiated(parameters) expected_waveform = MultiChannelWaveform( - {('CH' + channel): TableSingleChannelWaveform(instantiated) for channel, instantiated in - instantiated_entries.items()}) + [TableWaveform(channel=('CH'+channel), waveform_table=instantiated, measurement_windows=[]) + for channel, instantiated in + instantiated_entries.items()]) sequencer = DummySequencer() instruction_block = DummyInstructionBlock() @@ -775,9 +786,9 @@ def test_build_sequence_multi(self) -> None: instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) - waveform = table.build_waveform(parameters) + waveform = table.build_waveform(parameters, measurement_mapping={}, channel_mapping=channel_mapping) for ch in waveform.defined_channels: - self.assertEqual(expected_waveform['CH' + ch], waveform[ch]) + self.assertEqual(expected_waveform, waveform) def test_build_sequence_multi_one_channel_empty(self) -> None: table = TablePulseTemplate(channels={'A', 'B'}) @@ -794,55 +805,72 @@ def test_build_sequence_multi_one_channel_empty(self) -> None: measurement_mapping={}, channel_mapping=channel_mapping, instruction_block=instruction_block) - expected_waveform = MultiChannelWaveform({('CH'+channel):TableSingleChannelWaveform(instantiated) for channel, instantiated in instantiated_entries.items()}) + expected_waveform = MultiChannelWaveform([TableWaveform('CH'+channel, instantiated, []) for channel, instantiated in instantiated_entries.items()]) self.assertEqual(1, len(instruction_block.instructions)) instruction = instruction_block.instructions[0] self.assertIsInstance(instruction, EXECInstruction) self.assertEqual(expected_waveform, instruction.waveform) - waveform = table.build_waveform(parameters) - for ch in waveform.defined_channels: - self.assertEqual(expected_waveform['CH' + ch], waveform[ch]) + waveform = table.build_waveform(parameters, measurement_mapping={}, channel_mapping=channel_mapping) + + self.assertEqual(expected_waveform, waveform) class TableWaveformDataTests(unittest.TestCase): def test_duration(self) -> None: entries = [WaveformTableEntry(0, 0, HoldInterpolationStrategy()), WaveformTableEntry(5, 1, HoldInterpolationStrategy())] - waveform = TableSingleChannelWaveform(entries) + waveform = TableWaveform('A', entries, []) self.assertEqual(5, waveform.duration) @unittest.skip("What is the point of empty waveforms?") def test_duration_no_entries(self) -> None: - waveform = TableSingleChannelWaveform([]) + waveform = TableWaveform([]) self.assertEqual(0, waveform.duration) def test_duration_no_entries_exception(self) -> None: with self.assertRaises(ValueError): - waveform = TableSingleChannelWaveform([]) + waveform = TableWaveform('A', [], []) self.assertEqual(0, waveform.duration) def test_few_entries(self) -> None: with self.assertRaises(ValueError): - TableSingleChannelWaveform([[]]) + TableWaveform('A', [[]], []) with self.assertRaises(ValueError): - TableSingleChannelWaveform([WaveformTableEntry(0, 0, HoldInterpolationStrategy())]) + TableWaveform('A', [WaveformTableEntry(0, 0, HoldInterpolationStrategy())], []) - def test_sample(self) -> None: + def test_unsafe_sample(self) -> None: interp = DummyInterpolationStrategy() entries = [WaveformTableEntry(0, 0, interp), WaveformTableEntry(2.1, -33.2, interp), WaveformTableEntry(5.7, 123.4, interp)] - waveform = TableSingleChannelWaveform(entries) - sample_times = numpy.linspace(98.5, 103.5, num=11) + waveform = TableWaveform('A', entries, []) + sample_times = numpy.linspace(98.5, 103.5, num=11)-98 - offset = 0.5 - result = waveform.sample(sample_times, offset) + result = waveform.unsafe_sample('A', sample_times) expected_data = [((0, 0), (2.1, -33.2), [0.5, 1.0, 1.5, 2.0]), ((2.1, -33.2), (5.7, 123.4), [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5])] self.assertEqual(expected_data, interp.call_arguments) - expected_result = [sample_times - 98] + expected_result = sample_times self.assertTrue(numpy.all(expected_result == result)) + output_expected = numpy.empty_like(expected_data) + output_received = waveform.unsafe_sample('A', sample_times, output_array=output_expected) + self.assertIs(output_expected, output_received) + self.assertTrue(numpy.all(expected_result == output_received)) + + def test_simple_properties(self): + interp = DummyInterpolationStrategy() + entries = [WaveformTableEntry(0, 0, interp), + WaveformTableEntry(2.1, -33.2, interp), + WaveformTableEntry(5.7, 123.4, interp)] + meas = [('M', 1, 2)] + chan = 'A' + waveform = TableWaveform(chan, entries, meas) + + self.assertEqual(waveform.defined_channels, {chan}) + self.assertEqual(waveform.get_measurement_windows(), meas) + self.assertIs(waveform.unsafe_get_subset_for_channels('A'), waveform) + class ParameterValueIllegalExceptionTest(unittest.TestCase): diff --git a/tests/pulses/table_sequence_sequencer_intergration_tests.py b/tests/pulses/table_sequence_sequencer_intergration_tests.py index 01b18d870..b2a0c5acc 100644 --- a/tests/pulses/table_sequence_sequencer_intergration_tests.py +++ b/tests/pulses/table_sequence_sequencer_intergration_tests.py @@ -76,4 +76,4 @@ def test_table_sequence_sequencer_integration(self) -> None: for instruction in instructions: if isinstance(instruction,EXECInstruction): - self.assertIn(instruction.measurement_windows[0], [('my', 2, 5),('thy', 4, 5)]) + self.assertIn(instruction.waveform.get_measurement_windows()[0], [('my', 2, 5), ('thy', 4, 5)]) diff --git a/tests/qcmatlab/pulse_control_tests.py b/tests/qcmatlab/pulse_control_tests.py index 2df5945ee..dc731c35a 100644 --- a/tests/qcmatlab/pulse_control_tests.py +++ b/tests/qcmatlab/pulse_control_tests.py @@ -2,7 +2,7 @@ import numpy import numpy.random from qctoolkit.qcmatlab.pulse_control import PulseControlInterface -from tests.pulses.sequencing_dummies import DummySingleChannelWaveform, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstructionBlock class PulseControlInterfaceTests(unittest.TestCase): @@ -12,12 +12,12 @@ def test_create_waveform_struct(self) -> None: sample_rate = 10 expected_samples = numpy.random.rand(11) - waveform = DummySingleChannelWaveform(duration=1, sample_output=expected_samples) + waveform = DummyWaveform(duration=1, sample_output=expected_samples) pci = PulseControlInterface(sample_rate, time_scaling=1) result = pci.create_waveform_struct(waveform, name=name) expected_sample_times = numpy.linspace(0, 1, 11).tolist() - self.assertEqual((expected_sample_times, 0), waveform.sample_calls[0]) + self.assertAlmostEqual(expected_sample_times, waveform.sample_calls[0][1]) expected_result = dict(name=name, data=dict(wf=expected_samples.tolist(), marker=numpy.zeros_like(expected_samples).tolist(), @@ -48,9 +48,9 @@ def test_create_pulse_group(self) -> None: expected_samples_wf1 = numpy.random.rand(11) expected_samples_wf2 = numpy.random.rand(11) block = DummyInstructionBlock() - wf1a = DummySingleChannelWaveform(duration=1, sample_output=expected_samples_wf1) - wf1b = DummySingleChannelWaveform(duration=1, sample_output=expected_samples_wf1) - wf2 = DummySingleChannelWaveform(duration=1, sample_output=expected_samples_wf2) + wf1a = DummyWaveform(duration=1, sample_output=expected_samples_wf1) + wf1b = DummyWaveform(duration=1, sample_output=expected_samples_wf1) + wf2 = DummyWaveform(duration=1, sample_output=expected_samples_wf2) block.add_instruction_exec(wf1a) block.add_instruction_exec(wf1b) block.add_instruction_exec(wf2) From 944acf636310488941552481e22d277c6a8ae69b Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 15 Feb 2017 13:51:13 +0100 Subject: [PATCH 014/116] First untested implementation for the alazar DAC --- qctoolkit/hardware/dacs/__init__.py | 8 ++- qctoolkit/hardware/dacs/alazar.py | 104 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 qctoolkit/hardware/dacs/alazar.py diff --git a/qctoolkit/hardware/dacs/__init__.py b/qctoolkit/hardware/dacs/__init__.py index b3348eaa7..22ad47fa6 100644 --- a/qctoolkit/hardware/dacs/__init__.py +++ b/qctoolkit/hardware/dacs/__init__.py @@ -1,20 +1,22 @@ from abc import ABCMeta, abstractmethod from typing import Dict - +from collections import deque __all__ = ['DAC'] -class DAC(ABCMeta): +class DAC(metaclass=ABCMeta): """Representation of a data acquisition card""" @abstractmethod - def register_measurement_windows(self, program_name: str, windows: Dict[str, 'numpy.ndarray']): + def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]): """""" + @abstractmethod def register_operations(self, program_name: str, operations): """""" + @abstractmethod def arm_program(self, program_name: str): """""" diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py new file mode 100644 index 000000000..23c03ce42 --- /dev/null +++ b/qctoolkit/hardware/dacs/alazar.py @@ -0,0 +1,104 @@ +from typing import Union, Dict, NamedTuple, List +from collections import deque + +import numpy as np + +from atsaverage.config import ScanlineConfiguration +from atsaverage.masks import CrossBufferMask, Mask +from atsaverage.operations import OperationDefinition + +from qctoolkit.hardware.dacs import DAC + + +class AlazarProgram: + def __init__(self, masks=None, operations=None, total_length=None): + self.masks = masks + self.operations = operations + self.total_length = total_length + def __iter__(self): + yield self.masks + yield self.operations + yield self.total_length + + +class AlazarCard(DAC): + def __init__(self, card, config: Union[ScanlineConfiguration, None] = None): + self.__card = card + + self.__definitions = dict() + self.config = config + + self.__mask_prototypes = dict() # type: Dict[str, Tuple[Any, str]] + + self.__registered_programs = dict() # type: Dict[str, AlazarProgram] + + @property + def card(self): + return self.__card + + def __make_mask(self, mask_id: str, window_deque: deque): + if mask_id not in self.__mask_prototypes: + raise KeyError('Measurement window {} can not be converted as it is not registered.'.format(mask_id)) + + hardware_channel, mask_type = self.__mask_prototypes[mask_id] + if mask_type not in ('auto', 'cross_buffer', None): + raise ValueError('Currently only can do cross buffer mask') + begins_lengths = np.asarray(window_deque) + + begins = begins_lengths[:, 0] + lengths = begins_lengths[:, 1] + + sorting_indices = np.argsort(begins) + begins = begins[sorting_indices] + lengths = lengths[sorting_indices] + + if np.any(begins[:-1]+lengths[:-1] >= begins[1:]): + raise ValueError('Found overlapping windows in begins') + + mask = CrossBufferMask() + mask.begin = begins + mask.length = lengths + mask.channel = hardware_channel + return mask + + def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]): + for mask_id, window_deque in windows.items(): + begins_lengths = np.asarray(window_deque) + begins = begins_lengths[:, 0] + lengths = begins_lengths[:, 1] + sorting_indices = np.argsort(begins) + begins = begins[sorting_indices] + lengths = lengths[sorting_indices] + + windows[mask_id] = (begins, lengths) + total_length = max(total_length, begins[-1]+lengths[-1]) + + total_length = np.ceil(total_length/self.__card.minimum_record_size) * self.__card.minimum_record_size + + self.__registered_programs.get(program_name, + default=AlazarProgram()).masks = [ + self.__make_mask(mask_id, window_deque) + for mask_id, window_deque in windows.items()] + self.__registered_programs[program_name].total_length = total_length + + def register_operations(self, program_name: str, operations): + self.__registered_programs.get(program_name, + default=AlazarProgram() + ).operations = self.__registered_programs.get(program_name, self) + + def arm_program(self, program_name: str): + config = self.config + config.masks, config.operations, total_record_size = self.__registered_programs[program_name] + + if config.totalRecordSize is None: + config.totalRecordSize = total_record_size + elif config.totalRecordSize < total_record_size: + raise ValueError('specified total record size is smaller than needed {} < {}'.format(config.totalRecordSize, + total_record_size)) + + config.apply(self.__card) + self.__card.startAcquisition(1) + + def delete_program(self, program_name: str): + self.__registered_operations.pop(program_name, None) + self.__registered_masks.pop(program_name, None) From e9c7c4b0ae7f54c21d902425b6364028a9965153 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 15 Feb 2017 13:51:59 +0100 Subject: [PATCH 015/116] backup program --- qctoolkit/hardware/program.py | 45 ++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 65bfef267..5a7ece9b0 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -1,12 +1,14 @@ from qctoolkit.pulses.instructions import AbstractInstructionBlock, InstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable -from qctoolkit.hardware.awgs import AWG +from collections import deque + from qctoolkit.comparable import Comparable from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform import itertools from collections import namedtuple from copy import deepcopy, copy as shallowcopy +from ctypes import c_int64 as MutableInt ChannelID = int @@ -65,17 +67,6 @@ def split_one_child(self): return raise Exception('Could not split of one child', self) - def is_leaf(self) -> bool: - return len(self.__children) == 0 - - def depth(self) -> int: - return 0 if self.is_leaf() else (1 + max((e.depth() for e in self))) - - def is_balanced(self) -> bool: - if self.is_leaf(): - return True - return all((e.depth() == self.__children[0].depth() and e.is_balanced()) for e in self) - def merge(self): """Merge successive loops that are repeated once and are no leafs into one""" raise Exception("Not tested.") @@ -201,8 +192,7 @@ def __parse_child(self, child): def assert_tree_integrity(self): if self.__parent: - children_ids = [id(c) for c in self.__parent.children] - if id(self) not in children_ids: + if id(self) not in (id(c) for c in self.__parent.children): raise Exception() for child in self.__children: child.assert_tree_integrity() @@ -213,6 +203,33 @@ def copy_tree_structure(self, new_parent: Union['Loop', bool]=False): repetition_count=self.repetition_count, children=(child.copy_tree_structure() for child in self.__children)) + def get_measurement_windows(self, offset: MutableInt = MutableInt(0), measurement_windows=dict()): + if self.is_leaf(): + for _ in range(self.repetition_count): + for (mw_name, begin, length) in self.instruction.waveform.get_measurement_windows(): + measurement_windows.get(mw_name, default=deque()).append((begin + offset.value, length)) + offset.value += self.instruction.waveform.duration + else: + for _ in range(self.repetition_count): + for child in self.__children: + child.get_measurement_windows(offset, measurement_windows=measurement_windows) + return measurement_windows + +""" + def extract_measurement_windows(loop: 'Loop', offset: MutableInt): + if loop.is_leaf(): + for _ in range(loop.repetition_count): + for (mw_name, begin, length) in loop.instruction.measurement_windows: + measurement_windows.get(mw_name, default=[]).append(begin + offset.value, length) + offset.value += loop.instruction.waveform.duration + else: + for _ in range(loop.repetition_count): + for sub_loop in loop: + extract_measurement_windows(sub_loop, offset) + + for program in mcp.programs.values(): + extract_measurement_windows(program, MutableInt(0)) +""" class ChannelSplit(Exception): def __init__(self, channels_and_blocks): From 79a5942d9ebcd185d579d0f46a229717b7d4a065 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 21 Feb 2017 18:48:59 +0100 Subject: [PATCH 016/116] Finalize AWG interface (I hope) Separate tree functionality from Loop functionality --- qctoolkit/hardware/awgs/base.py | 58 ++++-- qctoolkit/hardware/awgs/tabor.py | 330 ++++++++++++++++++++++++++---- qctoolkit/hardware/program.py | 333 +++++++++++-------------------- qctoolkit/utils/tree.py | 126 ++++++++++++ tests/utils/tree_tests.py | 86 ++++++++ 5 files changed, 666 insertions(+), 267 deletions(-) create mode 100644 qctoolkit/utils/tree.py create mode 100644 tests/utils/tree_tests.py diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index 2d5f3aced..d4bff6d6a 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -10,6 +10,8 @@ from abc import ABCMeta, abstractmethod, abstractproperty from typing import Set, Tuple, List +from qctoolkit.hardware.program import Loop +from qctoolkit.comparable import Comparable from qctoolkit.pulses.instructions import InstructionSequence, EXECInstruction __all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", @@ -18,24 +20,44 @@ Program = InstructionSequence -class AWG(metaclass=ABCMeta): +class AWG(Comparable): """An arbitrary waveform generator abstraction class. + It represents a set of channels that have to have(hardware enforced) the same: + -control flow + -sample rate It keeps track of the AWG state and manages waveforms and programs on the hardware. """ + def __init__(self, identifier: str): + self.identifier = identifier + + @abstractproperty + def num_channels(self): + """Number of channels""" + + @abstractproperty + def num_markers(self): + """Number of marker channels""" + @abstractmethod - def upload(self, name: str, program: Program, force: bool=False) -> None: + def upload(self, name: str, + program: Loop, + channels: List[ChannelID], + markers: List[ChannelID], + force: bool=False) -> None: """Upload a program to the AWG. Physically uploads all waveforms required by the program - excluding those already present - to the device and sets up playback sequences accordingly. This method should be cheap for program already on the device and can therefore be used - for syncing. + for syncing. Programs that are uploaded should be fast(~1 sec) to arm. Args: name (str): A name for the program on the AWG. - program (Program): The program (a sequence of instructions) to upload. + program (Loop): The program (a sequence of instructions) to upload. + channels (List): List of channels in the program to use. Index of channel ID corresponds to the AWG channel + markers (List): List of channels in the program to use. Index of channel ID corresponds to the AWG channel force (bool): If a different sequence is already present with the same name, it is overwritten if force is set to True. (default = False) """ @@ -51,9 +73,8 @@ def remove(self, name: str) -> None: """ @abstractmethod - def run(self, name: str) -> None: - """Load the program 'name' and either arm the device for running it or run it.""" - # todo: isn't this semantically unlcear and should be separated into two explicit methods + def arm(self, name: str) -> None: + """Load the program 'name' and arm the device for running it.""" @abstractproperty def programs(self) -> Set[str]: @@ -63,13 +84,14 @@ def programs(self) -> Set[str]: def sample_rate(self) -> float: """The sample rate of the AWG.""" - @abstractproperty - def identifier(self) -> str: - """Return a hardware identifier string.""" + def compare_key(self) -> int: + return id(self) - @abstractproperty - def output_range(self) -> Tuple[float, float]: - """The minimal/maximal voltage the AWG can produce.""" + def __copy__(self) -> None: + raise NotImplementedError() + + def __deepcopy__(self, memodict={}) -> None: + raise NotImplementedError() class DummyAWG(AWG): @@ -78,7 +100,8 @@ class DummyAWG(AWG): def __init__(self, memory: int=100, sample_rate: float=10, - output_range: Tuple[float, float]=(-5,5)) -> None: + output_range: Tuple[float, float]=(-5,5), + num_channels: int=1) -> None: """Create a new DummyAWG instance. Args: @@ -93,6 +116,7 @@ def __init__(self, self.__program_wfs = {} # contains program names and necessary waveforms indices self.__sample_rate = sample_rate self.__output_range = output_range + self.__num_channels = num_channels def add_waveform(self, waveform) -> int: try: @@ -131,7 +155,7 @@ def clean(self) -> None: self.__waveform_indices.pop(wf) self.__waveform_memory = None - def run(self, name: str) -> None: + def arm(self, name: str) -> None: raise NotImplementedError() @property @@ -150,6 +174,10 @@ def identifier(self) -> str: def sample_rate(self) -> float: return self.__sample_rate + @property + def num_channels(self): + return self.__num_channels + class ProgramOverwriteException(Exception): diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 9244e4a88..aebf1f085 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,10 +1,12 @@ from qctoolkit.pulses.pulse_template import ChannelID +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from qctoolkit.hardware.program import Loop, MultiChannelProgram from qctoolkit.hardware.util import voltage_to_uint16 +from qctoolkit.hardware.awgs import AWG import sys import numpy as np -from typing import List, Tuple, Iterable +from typing import List, Tuple, Iterable, Set, NamedTuple # Provided by Tabor electronics for python 2.7 # a python 3 version is in a private repository on https://git.rwth-aachen.de/qutech @@ -15,25 +17,50 @@ assert(sys.byteorder == 'little') +class TaborSegment(tuple): + def __new__(cls, ch_a, ch_b): + return tuple.__new__(cls, (ch_a, ch_b)) + + def __init__(self, ch_a, ch_b): + pass + + def num_points(self) -> int: + return max(len(self[0]), len(self[1])) + + def __hash__(self) -> int: + return hash((bytes(self[0]), bytes(self[1]))) + + class TaborProgram: WAVEFORM_MODES = ('single', 'advanced', 'sequence') - def __init__(self, program: MultiChannelProgram, device_properties, channels: Tuple[ChannelID, ChannelID]): - if len(channels) > 2: - raise Exception('TaborProgram only supports 2 channels') - channel_set = frozenset(channel for channel in channels if channel is not None) + def __init__(self, + program: MultiChannelProgram, + device_properties, + channels: Tuple[ChannelID, ChannelID], + markers: Tuple[ChannelID, ChannelID]): + if len(channels) != device_properties['chan_per_part']: + raise TaborException('TaborProgram only supports {} channels'.format(device_properties['chan_per_part'])) + if len(markers) != device_properties['chan_per_part']: + raise TaborException('TaborProgram only supports {} markers'.format(device_properties['chan_per_part'])) + channel_set = frozenset(channel for channel in channels if channel is not None) | frozenset(marker + for marker in + markers if marker is not None) self.__root_loop = None for known_channels in program.programs.keys(): if known_channels.issuperset(channel_set): self.__root_loop = program.programs[known_channels] + break if self.__root_loop is None: raise TaborException("{} not found in program.".format(channel_set)) self.__waveform_mode = 'advanced' self.__channels = channels + self.__markers = markers + self.__used_channels = channel_set self.__device_properties = device_properties - self.__waveforms = [] + self.__waveforms = [] # type: List[MultiChannelWaveform] self.__sequencer_tables = [] self.__advanced_sequencer_table = [] @@ -44,16 +71,59 @@ def __init__(self, program: MultiChannelProgram, device_properties, channels: Tu else: self.setup_advanced_sequence_mode() - def setup_single_waveform_mode(self): + def setup_single_waveform_mode(self) -> None: raise NotImplementedError() - def setup_single_sequence_mode(self): + def sampled_segments(self, + sample_rate: float, + voltage_amplitude: Tuple[float, float], + voltage_offset: Tuple[float, float]) -> List[TaborSegment]: + + segment_lengths = np.fromiter((waveform.duration for waveform in self.__waveforms), + dtype=float, count=len(self.__waveforms)) * sample_rate + if not all(segment_length.is_integer() for segment_length in segment_lengths): + raise TaborException('At least one waveform has a length that is no multiple of the time per sample') + segment_lengths = segment_lengths.astype(dtype=int) + time_array = np.arange(np.max(segment_lengths)) / sample_rate + + def voltage_to_data(waveform, time, channel): + if self.__channels[channel]: + return voltage_to_uint16( + waveform[self.__channels[channel]].get_sampled(channel=self.__channels[channel], + sample_times=time), + voltage_amplitude[channel], + voltage_offset[channel], + resolution=14) + else: + return np.zeros(len(time), dtype=np.uint16) + + def get_marker_data(waveform: MultiChannelWaveform, time): + marker_data = np.zeros(len(time), dtype=np.uint16) + for marker_index, markerID in enumerate(self.__markers): + if markerID is not None: + marker_data |= (waveform.get_sampled(channel=markerID, sample_times=time) != 0).\ + astype(dtype=np.uint16) << marker_index+14 + return marker_data + + segments = len(self.__waveforms)*[None] + for i, waveform in enumerate(self.__waveforms): + t = time_array[:int(waveform.duration*sample_rate)] + segment_a = voltage_to_data(waveform, t, 0) + segment_b = voltage_to_data(waveform, t, 1) + assert (len(segment_a) == len(t)) + assert (len(segment_b) == len(t)) + seg_data = get_marker_data(waveform, t) + segment_b |= seg_data + segments[i] = TaborSegment(segment_a, segment_b) + return segments, segment_lengths + + def setup_single_sequence_mode(self) -> None: self.__waveform_mode = 'sequence' if len(self.program) < self.__device_properties['min_seq_len']: raise TaborException('SEQuence:LENGth has to be >={min_seq_len}'.format(**self.__device_properties)) raise NotImplementedError() - def setup_advanced_sequence_mode(self): + def setup_advanced_sequence_mode(self) -> None: while self.program.depth() > 2 or not self.program.is_balanced(): for i, sequence_table in enumerate(self.program): if sequence_table.depth() == 0: @@ -116,13 +186,15 @@ def setup_advanced_sequence_mode(self): waveforms = [] for sequencer_table_loop in self.program: current_sequencer_table = [] - for waveform_loop in sequencer_table_loop: - if waveform_loop.instruction.waveform in waveforms: - segment_no = waveforms.index(waveform_loop.instruction.waveform) + 1 + for waveform, repetition_count in ((waveform_loop.waveform.get_subset_for_channels(self.__used_channels), + waveform_loop.repetition_count) + for waveform_loop in sequencer_table_loop): + if waveform in waveforms: + segment_no = waveforms.index(waveform) + 1 else: segment_no = len(waveforms) + 1 - waveforms.append(waveform_loop.instruction.waveform) - current_sequencer_table.append((waveform_loop.repetition_count, segment_no, 0)) + waveforms.append(waveform) + current_sequencer_table.append((repetition_count, segment_no, 0)) if current_sequencer_table in sequencer_tables: sequence_no = sequencer_tables.index(current_sequencer_table) + 1 @@ -137,14 +209,14 @@ def setup_advanced_sequence_mode(self): self.__waveforms = waveforms @property - def program(self): + def program(self) -> Loop: return self.__root_loop - def get_sequencer_tables(self): + def get_sequencer_tables(self) -> List[Tuple[int, int, int]]: return self.__sequencer_tables @staticmethod - def increase_sequence_table_length(sequence_table: Loop, device_properties): + def increase_sequence_table_length(sequence_table: Loop, device_properties) -> None: assert(sequence_table.depth() == 1) if len(sequence_table) < device_properties['min_seq_len']: @@ -156,16 +228,20 @@ def increase_sequence_table_length(sequence_table: Loop, device_properties): else: TaborException('Sequence table too short: ', sequence_table) - def get_advanced_sequencer_table(self): + def get_advanced_sequencer_table(self) -> List[Tuple[int, int, int]]: """Advanced sequencer table that can be used via the download_adv_seq_table pytabor command""" return self.__advanced_sequencer_table - def get_waveform_data(self, device_properties, samplerate: float, voltage_amplitude: Tuple[float, float], voltage_offset: Tuple[float, float]): - if any(not(waveform.duration*samplerate).is_integer() for waveform in self.__waveforms): + def get_waveform_data(self, + device_properties, + sample_rate: float, + voltage_amplitude: Tuple[float, float], + voltage_offset: Tuple[float, float]) -> Tuple[np.ndarray, np.ndarray]: + if any(not(waveform.duration*sample_rate).is_integer() for waveform in self.__waveforms): raise TaborException('At least one waveform has a length that is no multiple of the time per sample') - maximal_length = int(max(waveform.duration for waveform in self.__waveforms) * samplerate) + maximal_length = int(max(waveform.duration for waveform in self.__waveforms) * sample_rate) time_array = np.arange(0, maximal_length, 1) - maximal_size = int(2 * (samplerate*sum(waveform.duration for waveform in self.__waveforms) + 16*len(self.__waveforms))) + maximal_size = int(2 * (sample_rate*sum(waveform.duration for waveform in self.__waveforms) + 16*len(self.__waveforms))) data = np.empty(maximal_size, dtype=np.uint16) offset = 0 segment_lengths = np.zeros(len(self.__waveforms), dtype=np.uint32) @@ -177,7 +253,7 @@ def voltage_to_data(waveform, time, channel): resolution=14) if self.__channels[channel] else None for i, waveform in enumerate(self.__waveforms): - t = time_array[:int(waveform.duration*samplerate)] + t = time_array[:int(waveform.duration*sample_rate)] segment1 = voltage_to_data(waveform, t, 0) segment2 = voltage_to_data(waveform, t, 1) segment_lengths[i] = len(segment1) if segment1 is not None else len(segment2) @@ -194,17 +270,17 @@ def voltage_to_data(waveform, time, channel): return data[:offset], segment_lengths - def upload_to_device(self, device: 'TaborAWG', channel_pair): + def upload_to_device(self, device: 'TaborAWG', channel_pair) -> None: if channel_pair not in ((1, 2), (3, 4)): raise Exception('Invalid channel pair', channel_pair) if self.__waveform_mode == 'advanced': - samplerate = device.samplerate(channel_pair[0]) + sample_rate = device.sample_rate(channel_pair[0]) amplitude = (device.amplitude(channel_pair[0]), device.amplitude(channel_pair[1])) offset = (device.offset(channel_pair[0]), device.offset(channel_pair[1])) wf_data, segment_lengths = self.get_waveform_data(device_properties=device.dev_properties, - samplerate=samplerate, + sample_rate=sample_rate, voltage_amplitude=amplitude, voltage_offset=offset) # download the waveform data as one big waveform @@ -225,39 +301,221 @@ def upload_to_device(self, device: 'TaborAWG', channel_pair): device.send_cmd('SEQ:SEL 1') - -class TaborAWG(teawg.TEWXAwg): +class TaborAWGRepresentation(teawg.TEWXAwg): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__selected_channel = None @property - def is_open(self): + def is_open(self) -> bool: return self.visa_inst is not None - def select_channel(self, channel): + def select_channel(self, channel) -> None: if channel not in (1, 2, 3, 4): raise TaborException('Invalid channel: {}'.format(channel)) if self.__selected_channel != channel: self.send_cmd(':INST:SEL {channel}'.format(channel=channel)) self.__selected_channel = channel - def samplerate(self, channel): + def sample_rate(self, channel) -> int: self.select_channel(channel) return int(float(self.send_query(':FREQ:RAST?'.format(channel=channel)))) - def amplitude(self, channel): + def amplitude(self, channel) -> float: self.select_channel(channel) - couplig = self.send_query(':OUTP:COUP?'.format(channel=channel)) - if couplig == 'DC': + coupling = self.send_query(':OUTP:COUP?'.format(channel=channel)) + if coupling == 'DC': return float(self.send_query(':VOLT?')) - elif couplig == 'HV': + elif coupling == 'HV': return float(self.send_query(':VOLD:HV?')) - def offset(self, channel): + def offset(self, channel) -> float: self.select_channel(channel) return float(self.send_query(':VOLT:OFFS?'.format(channel=channel))) +TaborProgramMemory = NamedTuple('TaborProgramMemory', [('segment_indices', np.ndarray), + ]) + +class TaborChannelPair(AWG): + def __init__(self, tabor_device: TaborAWGRepresentation, channels: Tuple[int, int], identifier: str): + super().__init__(identifier) + self.__device = tabor_device + + if channels not in ((1, 2), (3, 4)): + raise ValueError('Invalid channel pair: {}'.format(channels)) + self.__channels = channels + + self.__segment_lengths = np.zeros(0, dtype=int) + self.__segment_capacity = np.zeros(0, dtype=int) + self.__segment_hashes = np.zeros(0, dtype=int) + self.__segment_references = np.zeros(0, dtype=int) + + self.total_capacity = int(16e6) + + self.__known_programs = dict() # type: Dict[str, TaborProgramMemory] + + def free_program(self, name: str) -> TaborProgramMemory: + program = self.__known_programs.pop(name) + self.__segment_references[program.segment_indices] -= 1 + return program + + @property + def __segment_reserved(self) -> np.ndarray: + return self.__segment_references > 0 + + @property + def __free_points_in_total(self) -> int: + return self.total_capacity - np.sum(self.__segment_capacity[self.__segment_reserved]) + + @property + def __free_points_at_end(self) -> int: + reserved_index = np.where(self.__segment_reserved) + if reserved_index: + return self.total_capacity - np.sum(self.__segment_capacity[:reserved_index[-1]]) + else: + return self.total_capacity + + def upload(self, name: str, + program: Loop, + channels: List[ChannelID], + markers: List[ChannelID], + force: bool=False) -> None: + """Upload a program to the AWG. + + The policy is to prefer amending the unknown waveforms to overwriting old ones.""" + + if len(channels) != self.num_channels: + raise ValueError('Channel ID not specified') + if len(markers) != self.num_markers: + raise ValueError('Markers not specified') + + # helper to restore previous state if upload is impossible + to_restore = None + if name in self.__known_programs: + if force: + # save old program to restore in on error + to_restore = self.free_program(name) + else: + raise ValueError('{} is already known on {}'.format(name, self.identifier)) + + try: + # parse to tabor program + tabor_program = TaborProgram(program, channels=tuple(channels)) + sample_rate = self.__device.sample_rate(self.__channels[0]) + voltage_amplitudes = (self.__device.amplitude(self.__channels[0]), + self.__device.amplitude(self.__channels[1])) + voltage_offsets = (self.__device.offset(self.__channels[0]), + self.__device.offset(self.__channels[1])) + segments, segment_lengths = tabor_program.sampled_segments(sample_rate=sample_rate, + voltage_amplitude=voltage_amplitudes, + voltage_offset=voltage_offsets) + segment_hashes = np.fromiter((hash(bytes(segment)) for segment in segments), count=len(segments), dtype=int) + + known_waveforms = np.in1d(segment_hashes, self.__segment_hashes, assume_unique=True) + to_upload_size = np.sum(segment_lengths[~known_waveforms] + 16) + + waveform_to_segment = np.full(len(segments), -1, dtype=int) + waveform_to_segment[known_waveforms] = np.where( + np.in1d(self.__segment_hashes, segment_hashes[known_waveforms])) + + if name not in self.__known_programs: + if self.__free_points_in_total < to_upload_size: + raise MemoryError('Not enough free memory') + if self.__free_points_at_end < to_upload_size: + reserved_indices = np.where(self.__segment_reserved) + if len(reserved_indices) == 0: + raise MemoryError('Fragmentation does not allow upload.') + + last_reserved = reserved_indices[-1] if reserved_indices else 0 + free_segments = np.where(self.__segment_references[:last_reserved] == 0)[ + np.argsort(self.__segment_capacity[:last_reserved])[::-1]] + + to_amend = ~known_waveforms + to_insert = [] + for wf_index in np.argsort(segment_lengths[~known_waveforms])[::-1]: + if segment_lengths[wf_index] <= self.__segment_capacity[free_segments[0]]: + to_insert.append((wf_index, free_segments[0])) + free_segments = free_segments[1:] + to_amend[wf_index] = False + + if np.sum(segment_lengths[to_amend] + 16) > self.__free_points_at_end: + raise MemoryError('Fragmentation does not allow upload.') + + except: + if to_restore: + self.__known_programs[name] = to_restore + self.__segment_reserved[to_restore.segment_indices] += 1 + raise + + self.__segment_references[waveform_to_segment[waveform_to_segment >= 0]] += 1 + + if to_insert: + # as we have to insert waveforms the waveforms behind the last referenced are discarded + self.cleanup() + + for wf_index, segment_index in to_insert: + self.__upload_segment(segment_index, segments[wf_index]) + waveform_to_segment[wf_index] = segment_index + + if np.any(to_amend): + segments_to_amend = segments[to_amend] + self.__amend_segments(segments_to_amend) + waveform_to_segment[to_amend] = np.arange(len(self.__segment_capacity)-np.sum(to_amend), + len(self.__segment_capacity), dtype=int) + + self.__known_programs[name] = TaborProgramMemory(segment_index=waveform_to_segment, + ) + raise NotImplementedError() + + def __upload_segment(self, segment_index: int, segment: TaborSegment) -> None: + self.__segment_references[segment_index] = 1 + self.__segment_hashes[segment_index] + raise NotImplementedError() + + def __amend_segments(self, segments: List[TaborSegment]) -> None: + raise NotImplementedError() + + def cleanup(self) -> None: + """Discard all segments after the last which is still referenced""" + reserved_indices = np.where(self.__segment_references > 0) + + new_end = reserved_indices[-1]+1 if reserved_indices else 0 + self.__segment_lengths = self.__segment_lengths[:new_end] + self.__segment_capacity = self.__segment_capacity[:new_end] + self.__segment_hashes = self.__segment_capacity[:new_end] + self.__segment_references = self.__segment_capacity[:new_end] + + def remove(self, name: str) -> None: + """Remove a program from the AWG. + + Also discards all waveforms referenced only by the program identified by name. + + Args: + name (str): The name of the program to remove. + """ + self.free_program(name) + + def arm(self, name: str) -> None: + raise NotImplementedError() + + @property + def programs(self) -> Set[str]: + """The set of program names that can currently be executed on the hardware AWG.""" + raise set(program.name for program in self.__known_programs.keys()) + + @property + def sample_rate(self) -> float: + return self.__device.sample_rate(self.__channels[0]) + + @property + def num_channels(self) -> int: + return 2 + + @property + def num_markers(self) -> int: + return 2 + + class TaborException(Exception): pass diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 5a7ece9b0..4bb06ffff 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -1,239 +1,141 @@ -from qctoolkit.pulses.instructions import AbstractInstructionBlock, InstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction -from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable -from collections import deque - -from qctoolkit.comparable import Comparable - -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform import itertools -from collections import namedtuple -from copy import deepcopy, copy as shallowcopy +from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable, Tuple +from collections import deque +from copy import deepcopy from ctypes import c_int64 as MutableInt +from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.pulses.instructions import AbstractInstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction, Waveform +from qctoolkit.comparable import Comparable +from qctoolkit.utils.tree import Node, is_tree_circular -ChannelID = int -__all__ = ['ChannelID', 'Loop'] +__all__ = ['Loop', 'MultiChannelProgram', ''] -class Loop(Comparable): +class Loop(Comparable, Node): """Build a loop tree. The leaves of the tree are loops with one element.""" - def __init__(self, parent=None, instruction=None, children=list(), repetition_count=1): - super().__init__() + def __init__(self, + parent: Union['Loop', None]=None, + children: Iterable['Loop']=list(), + waveform: Union[EXECInstruction, Waveform]=None, + repetition_count=1): + super().__init__(parent=parent, children=children) - self.__parent = parent - self.__children = [self.__parse_child(child) for child in children] - self.__instruction = instruction - self.__repetition_count = repetition_count + self._waveform = waveform + self._repetition_count = repetition_count - if not (instruction is None or isinstance(instruction, (EXECInstruction, MultiChannelWaveform))): + if not isinstance(waveform, (type(None), Waveform)): raise Exception() - def unroll(self): - for i, e in enumerate(self.__parent): - if e is self: - self.__parent[i:i+1] = (child.copy_tree_structure(new_parent=self.__parent) - for _ in range(self.repetition_count) - for child in self.children) - self.__parent.assert_tree_integrity() - self.__parent = None - return - raise Exception('self not found in parent') - - def unroll_children(self): - self.__children = [Loop(parent=self, instruction=child.instruction, children=child.children) - for _ in range(self.repetition_count) - for child in self.children] - self.__repetition_count = 1 - self.assert_tree_integrity() - - def encapsulate(self): - self.__children = [Loop(children=self.__children, - parent=self, - repetition_count=self.__repetition_count, - instruction=self.__instruction)] - self.__repetition_count = 1 - self.__instruction = None - self.assert_tree_integrity() - - def split_one_child(self): - self.assert_tree_integrity() - for i in reversed(range(len(self))): - if self[i].repetition_count > 1: - self[i].repetition_count -= 1 - self[i+1:i+1] = (self[i].copy_tree_structure(),) - self[i+1].repetition_count = 1 - self.assert_tree_integrity() - return - raise Exception('Could not split of one child', self) - - def merge(self): - """Merge successive loops that are repeated once and are no leafs into one""" - raise Exception("Not tested.") - if self.depth() < 2: - return - # TODO: make this pythonic - i = 0 - while i < len(self.__children): - if self.__children[i].repetition_count == 1 and not self.__children[i].is_leaf(): - j = i + 1 - while j < len(self.__children) and self.__children[j].repetition_count == 1 and not self.__children[j].is_leaf(): - j += 1 - if j > i + 1: - self.__children[i:j] = Loop(parent=self, - children=[cc for child in self.__children[i:j] for cc in child], - repetition_count=1) - i += 1 - self.assert_tree_integrity() - - def compare_key(self): - return self.__instruction, self.__repetition_count, tuple(c.compare_key() for c in self.__children) - @property - def children(self) -> List['Loop']: - """ - :return: shallow copy of children - """ - return shallowcopy(self.__children) - - def append_child(self, **kwargs): - self.__children.append(Loop(parent=self, **kwargs)) - self.assert_tree_integrity() + def compare_key(self) -> Tuple: + return self._waveform, self._repetition_count, tuple(c.compare_key for c in self) - def __check_circular(self, visited: List['Loop']): - for v in visited: - if self is v: - raise Exception(self, visited) - visited.append(self) - for c in self.__children: - c.__check_circular(shallowcopy(visited)) - - def check_circular(self): - self.__check_circular([]) + def append_child(self, **kwargs) -> None: + self[len(self):len(self)] = (kwargs, ) @property - def instruction(self): - return self.__instruction + def waveform(self) -> Waveform: + return self._waveform - @instruction.setter - def instruction(self, val): - self.__instruction = val + @waveform.setter + def waveform(self, val) -> None: + self._waveform = val @property - def repetition_count(self): - return self.__repetition_count + def repetition_count(self) -> int: + return self._repetition_count @repetition_count.setter - def repetition_count(self, val): - self.__repetition_count = val - - def get_root(self): - if self.__parent: - return self.__parent.get_root() - else: - return self - - def get_depth_first_iterator(self): - if not self.is_leaf(): - for e in self.__children: - yield from e.get_depth_first_iterator() - yield self - - def get_breadth_first_iterator(self, queue: List['Loop']=[]): - yield self - if not self.is_leaf(): - queue += self.__children - if queue: - yield from queue.pop(0).get_breadth_first_iterator(queue) - - def __iter__(self) -> Iterable['Loop']: - return iter(self.children) - - def __setitem__(self, idx, value): - if isinstance(idx, slice): - if isinstance(value, Loop): - raise TypeError('can only assign an iterable (Loop does not count)') - value = (self.__parse_child(child) for child in value) - else: - value = self.__parse_child(value) - self.__children.__setitem__(idx, value) + def repetition_count(self, val) -> None: + self._repetition_count = val + + def unroll(self) -> None: + for i, e in enumerate(self.parent): + if id(e) == id(self): + self.parent[i:i+1] = (child.copy_tree_structure(new_parent=self.parent) + for _ in range(self.repetition_count) + for child in self) + self.parent.assert_tree_integrity() + return + raise Exception('self not found in parent') - def __getitem__(self, *args, **kwargs) ->Union['Loop', List['Loop']]: - return self.__children.__getitem__(*args, **kwargs) + def unroll_children(self) -> None: + old_children = self.children + self[:] = (child.copy_tree_structure() + for _ in range(self.repetition_count) + for child in old_children) + self._repetition_count = 1 + self.assert_tree_integrity() - def __len__(self): - return len(self.__children) + def encapsulate(self) -> None: + self[:] = [Loop(children=self.children, + repetition_count=self._repetition_count, + waveform=self._waveform)] + self._repetition_count = 1 + self._waveform = None + self.assert_tree_integrity() - def __repr__(self): - try: - self.check_circular() - except Exception as e: - if len(e.args) == 2: - return '{}: Circ {}'.format(id(self), len(e.args[1])) + def __repr__(self) -> str: + is_circular = is_tree_circular(self) + if is_circular: + return '{}: Circ {}'.format(id(self), is_circular) if self.is_leaf(): - return 'EXEC {} {} times'.format(self.__instruction, self.__repetition_count) + return 'EXEC {} {} times'.format(self._waveform, self._repetition_count) else: - repr = ['LOOP {} times:'.format(self.__repetition_count)] - for elem in self.__children: + repr = ['LOOP {} times:'.format(self._repetition_count)] + for elem in self: sub_repr = elem.__repr__().splitlines() sub_repr = [' ->' + sub_repr[0]] + [' ' + line for line in sub_repr[1:]] repr += sub_repr return '\n'.join(repr) - def __parse_child(self, child): - if isinstance(child, dict): - return Loop(parent=self, **child) - elif isinstance(child, Loop): - child.__parent = self - return child - else: - raise TypeError('Invalid child type', type(child)) - - def assert_tree_integrity(self): - if self.__parent: - if id(self) not in (id(c) for c in self.__parent.children): - raise Exception() - for child in self.__children: - child.assert_tree_integrity() - - def copy_tree_structure(self, new_parent: Union['Loop', bool]=False): - return type(self)(parent=self.__parent if new_parent is not False else new_parent, - instruction=self.__instruction, + def copy_tree_structure(self, new_parent: Union['Loop', bool]=False) -> 'Loop': + return type(self)(parent=self.parent if new_parent is False else new_parent, + waveform=self._waveform, repetition_count=self.repetition_count, - children=(child.copy_tree_structure() for child in self.__children)) + children=(child.copy_tree_structure() for child in self)) - def get_measurement_windows(self, offset: MutableInt = MutableInt(0), measurement_windows=dict()): + def get_measurement_windows(self, offset: MutableInt = MutableInt(0), measurement_windows=dict()) -> Dict[str, + deque]: if self.is_leaf(): for _ in range(self.repetition_count): - for (mw_name, begin, length) in self.instruction.waveform.get_measurement_windows(): + for (mw_name, begin, length) in self.waveform.get_measurement_windows(): measurement_windows.get(mw_name, default=deque()).append((begin + offset.value, length)) - offset.value += self.instruction.waveform.duration + offset.value += self.waveform.duration else: for _ in range(self.repetition_count): - for child in self.__children: + for child in self: child.get_measurement_windows(offset, measurement_windows=measurement_windows) return measurement_windows -""" - def extract_measurement_windows(loop: 'Loop', offset: MutableInt): - if loop.is_leaf(): - for _ in range(loop.repetition_count): - for (mw_name, begin, length) in loop.instruction.measurement_windows: - measurement_windows.get(mw_name, default=[]).append(begin + offset.value, length) - offset.value += loop.instruction.waveform.duration + def split_one_child(self, child_index=None) -> None: + """Take the last child that has a repetition count larger one, decrease it's repetition count and insert a copy + with repetition cout one after it""" + if child_index: + if self[child_index].repetition_count < 2: + raise ValueError('Cannot split child {} as the repetition count is not larger 1') else: - for _ in range(loop.repetition_count): - for sub_loop in loop: - extract_measurement_windows(sub_loop, offset) + try: + child_index = next(i for i in reversed(range(len(self))) + if self[i].repetition_count > 1) + except StopIteration: + raise RuntimeError('There is no child with repetition count > 1') + + new_child = self[child_index].copy_tree_structure() + new_child.repetition_count = 1 + + self[child_index].repetition_count -= 1 + + self[child_index+1:child_index+1] = (new_child,) + self.assert_tree_integrity() - for program in mcp.programs.values(): - extract_measurement_windows(program, MutableInt(0)) -""" class ChannelSplit(Exception): - def __init__(self, channels_and_blocks): - self.channels_and_stacks = channels_and_blocks + def __init__(self, channel_sets): + self.channel_sets = channel_sets class MultiChannelProgram: @@ -261,18 +163,20 @@ def find_defined_channels(instruction_list): channels = frozenset(channels) - stacks = {channels: [(Loop(), [*instruction_block[:-1]])]} + root = Loop() + stacks = {channels: (root, [([], [*instruction_block[:-1]])])} self.__programs = dict() while len(stacks) > 0: - chans, stack = stacks.popitem() + chans, (root_loop, stack) = stacks.popitem() try: - self.__programs[chans] = MultiChannelProgram.__split_channels(stack, chans) - except ChannelSplit as c: - for new_chans, new_stack in c.channels_and_stacks.items(): - assert (new_chans not in stacks) - assert (chans.issuperset(new_chans)) - stacks[new_chans] = new_stack + self.__programs[chans] = MultiChannelProgram.__split_channels(chans, root_loop, stack) + except ChannelSplit as split: + for new_channel_set in split.channel_sets: + assert (new_channel_set not in stacks) + assert (chans.issuperset(new_channel_set)) + + stacks[new_channel_set] = (root_loop.copy_tree_structure(), deepcopy(stack)) for channels, program in self.__programs.items(): iterable = program.get_breadth_first_iterator() @@ -280,7 +184,7 @@ def find_defined_channels(instruction_list): try: loop = next(iterable) if len(loop) == 1: - loop.instruction = loop[0].instruction + loop.waveform = loop[0].waveform loop.repetition_count = loop.repetition_count * loop[0].repetition_count loop[:] = loop[0][:] @@ -288,32 +192,32 @@ def find_defined_channels(instruction_list): except StopIteration: break - for loop in program.get_breadth_first_iterator(): - loop.assert_tree_integrity() - @property - def programs(self): + def programs(self) -> Dict[FrozenSet[ChannelID], Loop]: return self.__programs @property - def channels(self): + def channels(self) -> Set[ChannelID]: return set(itertools.chain(*self.__programs.keys())) @staticmethod - def __split_channels(block_stack, channels): + def __split_channels(channels, root_loop, block_stack) -> Loop: while block_stack: - current_loop, current_instruction_block = block_stack.pop() + current_loop_location, current_instruction_block = block_stack.pop() + current_loop = root_loop.locate(current_loop_location) + while current_instruction_block: instruction = current_instruction_block.pop(0) + if isinstance(instruction, EXECInstruction): if not instruction.waveform.defined_channels.issuperset(channels): raise Exception(instruction.waveform.defined_channels, channels) - current_loop.append_child(instruction=instruction) + current_loop.append_child(waveform=instruction.waveform) elif isinstance(instruction, REPJInstruction): current_loop.append_child(repetition_count=instruction.count) block_stack.append( - (current_loop.children[-1], + (current_loop[-1].get_location(), [*instruction.target.block[instruction.target.offset:-1]]) ) @@ -325,18 +229,15 @@ def __split_channels(block_stack, channels): current_instruction_block[0:0] = new_instruction_list else: - block_stack.append((current_loop, current_instruction_block)) + block_stack.append((current_loop_location, [instruction] + current_instruction_block)) + + raise ChannelSplit(instruction.channel_to_instruction_block.keys()) - channel_to_stack = dict() - for (chs, instruction_ptr) in instruction.channel_to_instruction_block.items(): - channel_to_stack[chs] = deepcopy(block_stack) - channel_to_stack[chs][-1][1][0:0] = [*instruction_ptr.block[instruction_ptr.offset:-1]] - raise ChannelSplit(channel_to_stack) else: raise Exception('Encountered unhandled instruction {} on channel(s) {}'.format(instruction, channels)) - return current_loop.get_root() + return root_loop - def __getitem__(self, item: Union[ChannelID, Set[ChannelID], FrozenSet[ChannelID]]): + def __getitem__(self, item: Union[ChannelID, Set[ChannelID], FrozenSet[ChannelID]]) -> Loop: if not isinstance(item, (set, frozenset)): item = frozenset((item,)) elif isinstance(item, set): diff --git a/qctoolkit/utils/tree.py b/qctoolkit/utils/tree.py new file mode 100644 index 000000000..d401c798a --- /dev/null +++ b/qctoolkit/utils/tree.py @@ -0,0 +1,126 @@ +from typing import Iterable, Union, List, Generator, Tuple +from collections import deque, namedtuple +from copy import copy as shallow_copy +import weakref + + +__all__ = ['Node'] + + +def make_empty_weak_reference() -> weakref.ref: + return weakref.ref(lambda: None) + + +class Node: + def __init__(self, parent: Union['Node', None]=None, children: Iterable=list()): + self.__parent = make_empty_weak_reference() if parent is None else weakref.ref(parent) + self.__children = [self.parse_child(child) for child in children] + + def parse_child(self, child) -> 'Node': + if isinstance(child, dict): + return type(self)(parent=self, **child) + elif type(child) is type(self): + child.__parent = weakref.ref(self) + return child + else: + raise TypeError('Invalid child type', type(child)) + + def is_leaf(self) -> bool: + return len(self.__children) == 0 + + def depth(self) -> int: + return 0 if self.is_leaf() else (1 + max(e.depth() for e in self.__children)) + + def is_balanced(self) -> bool: + if self.is_leaf(): + return True + return all((e.depth() == self.__children[0].depth() and e.is_balanced()) for e in self.__children) + + def __iter__(self) -> Iterable['Node']: + return iter(self.children) + + def __setitem__(self, idx: Union[int, slice], value: Union['Node', Iterable['Node']]): + if isinstance(idx, slice): + if isinstance(value, Node): + raise TypeError('can only assign an iterable (Loop does not count)') + value = (self.parse_child(child) for child in value) + else: + value = self.parse_child(value) + self.__children.__setitem__(idx, value) + + def __getitem__(self, *args, **kwargs) ->Union['Node', List['Node']]: + return self.__children.__getitem__(*args, **kwargs) + + def __len__(self) -> int: + return len(self.__children) + + def get_depth_first_iterator(self) -> Generator['Node', None, None]: + if not self.is_leaf(): + for e in self.__children: + yield from e.get_depth_first_iterator() + yield self + + def get_breadth_first_iterator(self, queue=deque()) -> Generator['Node', None, None]: + yield self + if not self.is_leaf(): + queue.extend(self.__children) + if queue: + yield from queue.popleft().get_breadth_first_iterator(queue) + + def assert_tree_integrity(self) -> None: + for child in self.__children: + if id(child.parent) != id(self): + raise AssertionError('Child is missing parent reference') + child.assert_tree_integrity() + if self.parent: + if id(self) not in (id(c) for c in self.parent.__children): + raise AssertionError('Parent is missing child reference') + + @property + def children(self) -> List['Node']: + """ + :return: shallow copy of children + """ + return shallow_copy(self.__children) + + @property + def parent(self) -> Union[None, 'Node']: + return self.__parent() + + def get_root(self) -> 'Node': + if self.parent: + return self.parent.get_root() + else: + return self + + def get_location(self) -> Tuple[int, ...]: + if self.parent: + for i, c in enumerate(self.parent.__children): + if id(c) == id(self): + return (*self.parent.get_location(), i) + raise AssertionError('Self not found in parent') + else: + return tuple() + + def locate(self, location: Tuple[int, ...]): + if location: + return self.__children[location[0]].locate(location[1:]) + else: + return self + + +def is_tree_circular(root: Node) -> Union[None, Tuple[List[Node], int]]: + NodeStack = namedtuple('NodeStack', ['node', 'stack']) + + nodes_to_visit = deque((NodeStack(root, deque()), )) + + while nodes_to_visit: + node, stack = nodes_to_visit.pop() + + stack.append(id(node)) + for child in node: + if id(child) in stack: + return stack, id(child) + + nodes_to_visit.append((child, stack)) + return None diff --git a/tests/utils/tree_tests.py b/tests/utils/tree_tests.py new file mode 100644 index 000000000..e5a1dbe9d --- /dev/null +++ b/tests/utils/tree_tests.py @@ -0,0 +1,86 @@ +import unittest + +from qctoolkit.utils.tree import Node + + +class SpecialNode(Node): + def __init__(self, my_argument=None, **kwargs): + super().__init__(**kwargs) + self.init_arg = my_argument + + +class NodeTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_init(self): + + children = [Node(parent=None, children=[]) for _ in range(5)] + + root = Node(parent=None, children=children) + + for c1, c2 in zip(children, root): + self.assertIs(c1, c2) + self.assertIs(c1.parent, root) + + def test_parse_children(self): + + root = Node() + to_parse = Node() + parsed = root.parse_child(to_parse) + self.assertIs(parsed.parent, root) + # maybe change this behaviour? + self.assertIs(parsed, to_parse) + + sub_node = Node() + to_parse = dict(children=[sub_node]) + parsed = root.parse_child(to_parse) + self.assertIs(parsed.parent, root) + self.assertIs(parsed.children[0], sub_node) + with self.assertRaises(TypeError): + root.parse_child(SpecialNode()) + + def test_parse_children_derived(self): + root = SpecialNode() + to_parse = SpecialNode() + parsed = root.parse_child(to_parse) + self.assertIs(parsed.parent, root) + # maybe change this behaviour? + self.assertIs(parsed, to_parse) + + sub_node = SpecialNode() + to_parse = dict(children=[sub_node], my_argument=6) + parsed = root.parse_child(to_parse) + self.assertIs(parsed.parent, root) + self.assertEqual(parsed.init_arg, 6) + with self.assertRaises(TypeError): + root.parse_child(Node()) + + def test_set_item(self): + root = SpecialNode() + + with self.assertRaises(TypeError): + root[:] = SpecialNode() + to_insert = [SpecialNode(), SpecialNode()] + + root[:] = to_insert + for c, e in zip(root, to_insert): + self.assertIs(c, e) + self.assertIs(c.parent, root) + + to_overwrite = SpecialNode() + root[1] = to_overwrite + self.assertIs(root[0], to_insert[0]) + self.assertIs(root[1], to_overwrite) + + def test_assert_integrity(self): + root = Node(children=(Node(), Node())) + + root.assert_tree_integrity() + + root_children = getattr(root, '_Node__children') + + root_children[1] = Node() + + with self.assertRaises(AssertionError): + root.assert_tree_integrity() From cce02ec32634678c19482ee76a159d2e6680364b Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 21 Feb 2017 18:50:57 +0100 Subject: [PATCH 017/116] Move ChannelID to root package Add return annotations --- qctoolkit/__init__.py | 8 +- qctoolkit/hardware/awgs/base.py | 1 + qctoolkit/hardware/awgs/tabor.py | 2 +- qctoolkit/hardware/awgs/tektronix.py | 6 +- qctoolkit/hardware/dacs/__init__.py | 8 +- qctoolkit/hardware/dacs/alazar.py | 14 +- qctoolkit/hardware/setup.py | 91 +++++++---- qctoolkit/hardware/util.py | 2 + qctoolkit/pulses/function_pulse_template.py | 16 +- qctoolkit/pulses/instructions.py | 15 +- .../pulses/multi_channel_pulse_template.py | 8 +- qctoolkit/pulses/plotting.py | 3 +- qctoolkit/pulses/pulse_template.py | 10 +- .../pulse_template_parameter_mapping.py | 17 ++- qctoolkit/pulses/repetition_pulse_template.py | 15 +- qctoolkit/pulses/sequence_pulse_template.py | 11 +- qctoolkit/pulses/table_pulse_template.py | 5 +- qctoolkit/qcmatlab/pulse_control.py | 14 +- setup.py | 30 ++-- tests/format_tests.py | 2 +- tests/hardware/awg_tests.py | 2 +- tests/hardware/program_tests.py | 143 ++++++++---------- tests/hardware/tabor_tests.py | 126 +++++++++++---- tests/pulses/pulse_template_tests.py | 3 +- tests/pulses/sequence_pulse_template_tests.py | 7 +- tests/pulses/sequencing_dummies.py | 4 +- tests/pulses/table_pulse_template_tests.py | 24 +-- 27 files changed, 342 insertions(+), 245 deletions(-) diff --git a/qctoolkit/__init__.py b/qctoolkit/__init__.py index ae4f43d71..8007d00a7 100644 --- a/qctoolkit/__init__.py +++ b/qctoolkit/__init__.py @@ -1 +1,7 @@ -__all__ = ["hardware", "pulses", "utils", "qcmatlab", "expressions", "serialization"] \ No newline at end of file +import typing + +__all__ = ["hardware", "pulses", "utils", "qcmatlab", "expressions", "serialization", + "MeasurementWindow", "ChannelID"] + +MeasurementWindow = typing.Tuple[str, float, float] +ChannelID = typing.Union[str, int] diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index d4bff6d6a..ab39d6ec0 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -10,6 +10,7 @@ from abc import ABCMeta, abstractmethod, abstractproperty from typing import Set, Tuple, List +from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.hardware.program import Loop from qctoolkit.comparable import Comparable from qctoolkit.pulses.instructions import InstructionSequence, EXECInstruction diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index aebf1f085..2fe091137 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,4 +1,4 @@ -from qctoolkit.pulses.pulse_template import ChannelID +from qctoolkit import ChannelID from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from qctoolkit.hardware.program import Loop, MultiChannelProgram from qctoolkit.hardware.util import voltage_to_uint16 diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index 71ebc5754..1de731838 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -15,7 +15,7 @@ hasVisa = False from qctoolkit.hardware.awgs.base import AWG, ProgramOverwriteException, Program -from qctoolkit.pulses.instructions import EXECInstruction, SingleChannelWaveform +from qctoolkit.pulses.instructions import EXECInstruction, Waveform __all__ = ['TektronixAWG', 'AWGSocket', 'EchoTestServer'] @@ -173,10 +173,10 @@ def rescale(self, voltages: np.ndarray) -> np.ndarray: # data = data + marker.astype(np.uint16) * 2**14 return data - def waveform2name(self, waveform: SingleChannelWaveform) -> str: + def waveform2name(self, waveform: Waveform) -> str: return str(hash(waveform)) - def add_waveform(self, waveform: SingleChannelWaveform, offset: float) -> None: + def add_waveform(self, waveform: Waveform, offset: float) -> None: """Samples a Waveform object to actual data and sends it to the AWG.""" # check if waveform is on the AWG already if waveform in self.__waveform_memory: diff --git a/qctoolkit/hardware/dacs/__init__.py b/qctoolkit/hardware/dacs/__init__.py index 22ad47fa6..25ba39513 100644 --- a/qctoolkit/hardware/dacs/__init__.py +++ b/qctoolkit/hardware/dacs/__init__.py @@ -9,16 +9,16 @@ class DAC(metaclass=ABCMeta): """Representation of a data acquisition card""" @abstractmethod - def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]): + def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]) -> None: """""" @abstractmethod - def register_operations(self, program_name: str, operations): + def register_operations(self, program_name: str, operations) -> None: """""" @abstractmethod - def arm_program(self, program_name: str): + def arm_program(self, program_name: str) -> None: """""" - def delete_program(self, program_name): + def delete_program(self, program_name) -> None: """""" diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py index 23c03ce42..ee0265b84 100644 --- a/qctoolkit/hardware/dacs/alazar.py +++ b/qctoolkit/hardware/dacs/alazar.py @@ -1,4 +1,4 @@ -from typing import Union, Dict, NamedTuple, List +from typing import Union, Dict, NamedTuple, List, Any from collections import deque import numpy as np @@ -33,10 +33,10 @@ def __init__(self, card, config: Union[ScanlineConfiguration, None] = None): self.__registered_programs = dict() # type: Dict[str, AlazarProgram] @property - def card(self): + def card(self) -> Any: return self.__card - def __make_mask(self, mask_id: str, window_deque: deque): + def __make_mask(self, mask_id: str, window_deque: deque) -> Mask: if mask_id not in self.__mask_prototypes: raise KeyError('Measurement window {} can not be converted as it is not registered.'.format(mask_id)) @@ -61,7 +61,7 @@ def __make_mask(self, mask_id: str, window_deque: deque): mask.channel = hardware_channel return mask - def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]): + def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]) -> None: for mask_id, window_deque in windows.items(): begins_lengths = np.asarray(window_deque) begins = begins_lengths[:, 0] @@ -81,12 +81,12 @@ def register_measurement_windows(self, program_name: str, windows: Dict[str, deq for mask_id, window_deque in windows.items()] self.__registered_programs[program_name].total_length = total_length - def register_operations(self, program_name: str, operations): + def register_operations(self, program_name: str, operations) -> None: self.__registered_programs.get(program_name, default=AlazarProgram() ).operations = self.__registered_programs.get(program_name, self) - def arm_program(self, program_name: str): + def arm_program(self, program_name: str) -> None: config = self.config config.masks, config.operations, total_record_size = self.__registered_programs[program_name] @@ -99,6 +99,6 @@ def arm_program(self, program_name: str): config.apply(self.__card) self.__card.startAcquisition(1) - def delete_program(self, program_name: str): + def delete_program(self, program_name: str) -> None: self.__registered_operations.pop(program_name, None) self.__registered_masks.pop(program_name, None) diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index f5b4d86bb..a1ddd05b1 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -1,4 +1,4 @@ -from typing import NamedTuple, Any, Callable +from typing import NamedTuple, Any, Set, Callable, Dict, Tuple from ctypes import c_int64 as MutableInt @@ -6,6 +6,8 @@ from qctoolkit.hardware.dacs import DAC from qctoolkit.hardware.program import MultiChannelProgram, Loop +from qctoolkit import ChannelID + import numpy as np @@ -18,11 +20,17 @@ PlaybackChannel.__new__.__defaults__ = (lambda v: v,) PlaybackChannel.__doc__ += ': Properties of an actual hardware channel' PlaybackChannel.awg.__doc__ = 'The AWG the channel is defined on' -PlaybackChannel.channel_on_awg.__doc__ = 'The channel\'s ID on the AWG.' +PlaybackChannel.channel_on_awg.__doc__ = 'The channel\'s index(starting with 0) on the AWG.' PlaybackChannel.voltage_transformation.__doc__ = \ 'A transformation that is applied to the pulses on the channel.\ One use case is to scale up the voltage if an amplifier is inserted.' +PlaybackChannel.__hash__ = lambda pbc: hash((id(pbc.awg), pbc.channel_on_awg)) + +RegisteredProgram = NamedTuple('RegisteredProgram', [('program', MultiChannelProgram), + ('measurement_windows', Dict[str, Tuple[float, float]]), + ('run_callback', Callable), + ('awgs_to_upload_to', Set[AWG])]) class HardwareSetup: @@ -35,51 +43,72 @@ class HardwareSetup: def __init__(self): self.__dacs = [] - self.__channel_map = dict() # type: Dict[ChannelID, PlaybackChannel] - self.__awgs = [] # type: List[AWG, List[ChannelID, Any]] + self.__channel_map = dict() # type: Dict[ChannelID, Set[PlaybackChannel]] + + self.__registered_programs = dict() # type: Dict[str, RegisteredProgram] - self.__registered_programs = dict() + def register_program(self, name: str, instruction_block, run_callback=lambda: None, update=False): + if not callable(run_callback): + raise TypeError('The provided run_callback is not callable') - def register_program(self, name: str, instruction_block, run_callback=None): mcp = MultiChannelProgram(instruction_block) measurement_windows = dict() - def extract_measurement_windows(loop: Loop, offset: MutableInt): - if loop.is_leaf(): - for (mw_name, begin, length) in loop.instruction.measurement_windows: - measurement_windows.get(mw_name, default=[]).append(begin + offset.value, length) - offset.value += loop.instruction.waveform.duration - else: - for sub_loop in loop: - extract_measurement_windows(sub_loop, offset) for program in mcp.programs.values(): - extract_measurement_windows(program, MutableInt(0)) - - for channels, program in mcp.programs: - pass + program.get_measurement_windows(measurement_windows=measurement_windows) - try: - for dac in self.__dacs: - dac.register_measurement_windows(name, measurement_windows) - except: - raise + for mw_name, begin_length_list in measurement_windows.items(): + measurement_windows[mw_name] = sorted(set(begin_length_list)) - self.__registered_programs[name] = (mcp, measurement_windows, - lambda: None if run_callback is None else run_callback) - - raise NotImplementedError() + handled_awgs = set() + for channels, program in mcp.programs: + awgs_to_upload_to = dict() + for channel_id in channels: + if channel_id in channels: + pbc = self.__channel_map[channel_id] + awgs_to_upload_to.get(pbc.awg, [None]*pbc.awg.num_channels)[pbc.channel_on_awg] = channel_id + + for awg, channel_ids in awgs_to_upload_to.items(): + if awg in handled_awgs: + raise ValueError('AWG has two programs') + else: + handled_awgs.add(awg) + awg.upload(name, program=program, channels=channel_ids, force=update) + + for dac in self.__dacs: + dac.register_measurement_windows(name, measurement_windows) + + self.__registered_programs[name] = RegisteredProgram(program=mcp, + measurement_windows=measurement_windows, + run_callback=run_callback, + awgs_to_upload_to= + set(awg for awg, _ in awgs_to_upload_to.items())) def arm_program(self, name): """Assert program is in memory. Hardware will wait for trigger event""" - raise NotImplementedError() + for awg in self.__registered_programs[name].awgs_to_upload_to: + awg.arm(name) + for dac in self.__dacs: + dac.arm_program(name) def run_program(self, name): """Calls arm program and starts it using the run callback""" - raise NotImplementedError() + self.arm_program(name) + self.__registered_programs[name].run_callback() + + def set_channel(self, identifier: ChannelID, playback_channel: PlaybackChannel): + for ch_id, pbc_set in self.__channel_map.items(): + if playback_channel in pbc_set: + raise ValueError('Channel already registered as playback channel for channel {}'.format(ch_id)) + self.__channel_map[identifier] = playback_channel + + def rm_channel(self, identifier: ChannelID): + self.__channel_map.pop(identifier) + + def registered_playback_channels(self) -> Set[PlaybackChannel]: + return set(pbc for pbc_set in self.__channel_map.values() for pbc in pbc_set) - def set_channel(self, identifier: ChannelID, channel: PlaybackChannel): - self.__channel_map[identifier] = channel diff --git a/qctoolkit/hardware/util.py b/qctoolkit/hardware/util.py index 94be70b92..68a40bccf 100644 --- a/qctoolkit/hardware/util.py +++ b/qctoolkit/hardware/util.py @@ -12,6 +12,8 @@ def voltage_to_uint16(voltage: np.ndarray, output_amplitude: float, output_offse :param resolution: :return: """ + if resolution < 1 or not isinstance(resolution, int): + raise ValueError('The resolution must be an integer > 0') non_dc_voltage = voltage - output_offset if np.any(np.abs(non_dc_voltage) > output_amplitude): diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 108b565f6..64bee0bb1 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -14,11 +14,12 @@ from qctoolkit.expressions import Expression from qctoolkit.serialization import Serializer +from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow, ChannelID +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.pulse_template_parameter_mapping import ParameterNotProvidedException -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform + __all__ = ["FunctionPulseTemplate", "FunctionWaveform"] @@ -103,7 +104,7 @@ def defined_channels(self) -> Set['ChannelID']: def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]): + channel_mapping: Dict[ChannelID, ChannelID]) -> 'FunctionWaveform': return FunctionWaveform( channel=channel_mapping[self.__channel], parameters={parameter_name: parameter.get_value() @@ -113,7 +114,6 @@ def build_waveform(self, measurement_windows=[] ) - def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: @@ -171,13 +171,13 @@ def __init__(self, self.__measurement_windows = measurement_windows @property - def defined_channels(self): + def defined_channels(self) -> Set[ChannelID]: return {self.__channel_id} - def get_measurement_windows(self): + def get_measurement_windows(self) -> List[MeasurementWindow]: return self.__measurement_windows - def __evaluate_partially(self, t): + def __evaluate_partially(self, t) -> float: params = self.__parameters.copy() params.update({"t":t}) return self.__expression.evaluate(**params) @@ -200,5 +200,5 @@ def unsafe_sample(self, output_array[i] = self.__evaluate_partially(t) return output_array - def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> Waveform: return self diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index ab1cd3f8e..88ea683a4 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -16,14 +16,15 @@ - InstructionPointer: References an instruction's location in a sequence. """ -import itertools from abc import ABCMeta, abstractmethod, abstractproperty from typing import List, Any, Dict, Iterable, Optional, Tuple, Union, Set from weakref import WeakValueDictionary + import numpy +from qctoolkit import ChannelID from qctoolkit.comparable import Comparable -from qctoolkit.pulses.pulse_template import MeasurementWindow +from qctoolkit import MeasurementWindow __all__ = ["Waveform", "Trigger", "InstructionPointer", "Instruction", "CJMPInstruction", "EXECInstruction", @@ -31,8 +32,6 @@ "ImmutableInstructionBlock", "InstructionSequence", "ChannelID" ] -ChannelID = Union[str,int] - class Waveform(Comparable, metaclass=ABCMeta): """Represents an instantiated PulseTemplate which can be sampled to retrieve arrays of voltage @@ -316,14 +315,14 @@ class CHANInstruction(Instruction): switch statement. """ - def __init__(self, channel_to_instruction_block: Dict[ChannelID,InstructionPointer]): + def __init__(self, channel_to_instruction_block: Dict[ChannelID, InstructionPointer]): self.channel_to_instruction_block = channel_to_instruction_block @property - def compare_key(self): + def compare_key(self) -> Dict[ChannelID, InstructionPointer]: return self.channel_to_instruction_block - def __str__(self): + def __str__(self) -> str: return "chan " + ",".join("{target} for {channel}" .format(target=v,channel=k) for k,v in self.channel_to_instruction_block.items()) @@ -492,7 +491,7 @@ def add_instruction_stop(self) -> None: """Create and append a new STOPInstruction object at the end of this instruction block.""" self.add_instruction(STOPInstruction()) - def add_instruction_chan(self, channel_to_instruction: Dict[ChannelID,'InstructionBlock'] ): + def add_instruction_chan(self, channel_to_instruction: Dict[ChannelID, 'InstructionBlock']) -> None: """Create and append a new CHANInstruction at the end of this instruction block.""" self.add_instruction(CHANInstruction({ch: InstructionPointer(block) for ch, block in channel_to_instruction.items()})) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index b38562a70..aa4624031 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -15,10 +15,11 @@ from qctoolkit.serialization import Serializer +from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.pulses.instructions import InstructionBlock, Waveform, InstructionPointer from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingTemplate,\ - MissingParameterDeclarationException, ChannelID + MissingParameterDeclarationException from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ ParameterNotProvidedException from qctoolkit.pulses.conditions import Condition @@ -113,7 +114,7 @@ def unsafe_sample(self, output_array: Union[numpy.ndarray, None]=None) -> numpy.ndarray: return self[channel].unsafe_sample(channel, sample_times, output_array) - def get_measurement_windows(self): + def get_measurement_windows(self) -> Iterable[MeasurementWindow]: return itertools.chain.from_iterable(sub_waveform.get_measurement_windows() for sub_waveform in self.__sub_waveforms) @@ -309,5 +310,6 @@ def __init__(self, obj1, obj2, intersect_set): self.intersect_set = intersect_set self.obj1 = obj1 self.obj2 = obj2 - def __str__(self): + + def __str__(self) -> str: return 'Channels {chs} defined in {} and {}'.format(self.intersect_set, self.obj1, self.obj2) \ No newline at end of file diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index 1458bf7dd..cedb87e36 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -12,7 +12,8 @@ import numpy as np from matplotlib import pyplot as plt -from qctoolkit.pulses.pulse_template import PulseTemplate, ChannelID +from qctoolkit import ChannelID +from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.parameters import Parameter from qctoolkit.pulses.sequencing import Sequencer from qctoolkit.pulses.instructions import EXECInstruction, STOPInstruction, InstructionSequence, \ diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index 09059b402..845c9840a 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -7,18 +7,18 @@ directly translated into a waveform. """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict, List, Tuple, Set, Optional, Union +from typing import Dict, List, Tuple, Set, Optional, Union, NamedTuple import itertools -MeasurementWindow = Tuple[str, float, float] -ChannelID = Union[str, int] +from qctoolkit import ChannelID from qctoolkit.serialization import Serializable from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.sequencing import SequencingElement, InstructionBlock -__all__ = ["MeasurementWindow", "PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException", "ChannelID"] + +__all__ = ["PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException"] class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): @@ -99,7 +99,7 @@ def atomic_build_sequence(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], - instruction_block: InstructionBlock): + instruction_block: InstructionBlock) -> None: waveform = self.build_waveform(parameters, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping) diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index 61714d14d..48ad493ea 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -4,10 +4,11 @@ from typing import Optional, Set, Dict, Union, Iterable, List, Any import itertools +from qctoolkit import ChannelID from qctoolkit.expressions import Expression from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration, MappedParameter, ParameterNotProvidedException -from qctoolkit.pulses.instructions import ChannelID + __all__ = [ "MappingTemplate", @@ -51,11 +52,11 @@ def __init__(self, template: PulseTemplate, self.__channel_mapping = dict(((name,name) for name in missing_channel_mappings), **channel_mapping) @property - def template(self): + def template(self) -> PulseTemplate: return self.__template @property - def measurement_mapping(self): + def measurement_mapping(self) -> Dict[str, str]: return self.__measurement_mapping @property @@ -87,7 +88,7 @@ def atomicity(self) -> bool: def atomicity(self, val) -> None: self.__template.atomicity = val - def get_serialization_data(self, serializer: 'Serializer') -> Dict[str,Any]: + def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: return dict(template=serializer.dictify(self.template), parameter_mapping=self.__parameter_mapping, measurement_mapping=self.__measurement_mapping, @@ -123,10 +124,10 @@ def map_parameters(self, } return inner_parameters - def get_updated_measurement_mapping(self, measurement_mapping: Dict[str,str]): + def get_updated_measurement_mapping(self, measurement_mapping: Dict[str, str]) -> Dict[str, str]: return {k: measurement_mapping[v] for k, v in self.__measurement_mapping.items()} - def get_updated_channel_mapping(self, channel_mapping: Dict[ChannelID, ChannelID]): + def get_updated_channel_mapping(self, channel_mapping: Dict[ChannelID, ChannelID]) -> Dict[ChannelID, ChannelID]: return {inner_ch: channel_mapping[outer_ch] for inner_ch, outer_ch in self.__channel_mapping.items()} def build_sequence(self, @@ -146,7 +147,7 @@ def build_sequence(self, def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]): + channel_mapping: Dict[ChannelID, ChannelID]) -> 'Waveform': """This gets called if the parent is atomic""" return self.template.build_waveform( parameters=self.map_parameters(parameters), @@ -197,7 +198,7 @@ class UnnecessaryMappingException(Exception): """Indicates that a mapping was provided that does not correspond to any of a SequencePulseTemplate's subtemplate's parameter declarations and is thus obsolete.""" - def __init__(self, template: PulseTemplate, key: Union[str,Set[str]]) -> None: + def __init__(self, template: PulseTemplate, key: Union[str, Set[str]]) -> None: super().__init__() self.template = template self.key = key diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index 001ba8dc2..e83618924 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -1,13 +1,14 @@ """This module defines RepetitionPulseTemplate, a higher-order hierarchical pulse template that represents the n-times repetition of another PulseTemplate.""" -from typing import Dict, List, Set, Optional, Union, Any +from typing import Dict, List, Set, Optional, Union, Any, Iterable, Tuple import numpy as np from qctoolkit.serialization import Serializer -from qctoolkit.pulses.pulse_template import PulseTemplate, ChannelID, PossiblyAtomicPulseTemplate +from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate from qctoolkit.pulses.sequencing import Sequencer from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer, Waveform from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter @@ -36,7 +37,7 @@ def defined_channels(self) -> Set[ChannelID]: def unsafe_sample(self, channel: ChannelID, sample_times: np.ndarray, - output_array: Union[np.ndarray, None]=None): + output_array: Union[np.ndarray, None]=None) -> np.ndarray: if output_array is None: output_array = np.empty(len(sample_times)) body_duration = self.__body.duration @@ -48,14 +49,14 @@ def unsafe_sample(self, return output_array @property - def compare_key(self): + def compare_key(self) -> Tuple[Any, int]: return self.__body.compare_key, self.__repetition_count @property - def duration(self): + def duration(self) -> float: return self.__body.duration*self.__repetition_count - def get_measurement_windows(self): + def get_measurement_windows(self) -> Iterable[MeasurementWindow]: def get_measurement_window_generator(body: Waveform, repetition_count: int): body_windows = list(body.get_measurement_windows()) for i in range(repetition_count): @@ -63,7 +64,7 @@ def get_measurement_window_generator(body: Waveform, repetition_count: int): yield (name, begin+i*body.duration, length) return get_measurement_window_generator(self.__body, self.__repetition_count) - def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]): + def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'RepetitionWaveform': return RepetitionWaveform(body=self.__body.unsafe_get_subset_for_channels(channels), repetition_count=self.__repetition_count) diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index e284807ac..a165cec7a 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -7,12 +7,13 @@ from qctoolkit.serialization import Serializer -from qctoolkit.pulses.pulse_template import PulseTemplate, MeasurementWindow, PossiblyAtomicPulseTemplate +from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.sequencing import InstructionBlock, Sequencer from qctoolkit.pulses.conditions import Condition from qctoolkit.pulses.pulse_template_parameter_mapping import \ - MissingMappingException, MappingTemplate, ChannelID, MissingParameterDeclarationException + MissingMappingException, MappingTemplate, MissingParameterDeclarationException from qctoolkit.pulses.instructions import Waveform __all__ = ["SequencePulseTemplate"] @@ -67,11 +68,11 @@ def unsafe_sample(self, return output_array @property - def compare_key(self): + def compare_key(self) -> Tuple[Waveform]: return self.__sequenced_waveforms @property - def duration(self): + def duration(self) -> float: return self.__duration def get_measurement_windows(self) -> Iterable[MeasurementWindow]: @@ -200,7 +201,7 @@ def requires_stop(self, SequencePulseTemplate can be partially sequenced.""" return self.__subtemplates[0].requires_stop(parameters,conditions) if self.__subtemplates else False - def build_waveform(self, parameters: Dict[str, Parameter]): + def build_waveform(self, parameters: Dict[str, Parameter]) -> SequenceWaveform: return SequenceWaveform([subtemplate.build_waveform(parameters) for subtemplate in self.__subtemplates]) def build_sequence(self, diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index a67374dcf..1fa7919ea 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -13,16 +13,17 @@ import numpy as np +from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.serialization import Serializer from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ ParameterNotProvidedException -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate from qctoolkit.pulses.interpolation import InterpolationStrategy, LinearInterpolationStrategy, \ HoldInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.conditions import Condition from qctoolkit.expressions import Expression -from qctoolkit.pulses.multi_channel_pulse_template import ChannelID, MultiChannelWaveform +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform __all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] diff --git a/qctoolkit/qcmatlab/pulse_control.py b/qctoolkit/qcmatlab/pulse_control.py index c68ad78f8..4967a784e 100644 --- a/qctoolkit/qcmatlab/pulse_control.py +++ b/qctoolkit/qcmatlab/pulse_control.py @@ -7,7 +7,7 @@ import numpy -from qctoolkit.pulses.instructions import SingleChannelWaveform, EXECInstruction, \ +from qctoolkit.pulses.instructions import Waveform, EXECInstruction, \ STOPInstruction, InstructionSequence __all__ = ["PulseControlInterface"] @@ -34,24 +34,28 @@ def __init__(self, sample_rate: int, time_scaling: float=0.001) -> None: self.__time_scaling = time_scaling @staticmethod - def __get_waveform_name(waveform: SingleChannelWaveform) -> str: + def __get_waveform_name(waveform: Waveform) -> str: # returns a unique identifier for a waveform object return 'wf_{}'.format(hash(waveform)) def create_waveform_struct(self, - waveform: SingleChannelWaveform, + waveform: Waveform, name: str) -> 'PulseControlInterface.Pulse': """Construct a dictionary adhering to the waveform struct definition in pulse control. Arguments: - waveform (SingleChannelWaveform): The Waveform object to convert. + waveform (Waveform): The Waveform object to convert. name (str): Value for the name field in the resulting waveform dictionary. Returns: a dictionary representing waveform as a waveform struct for pulse control """ + if len(waveform.defined_channels) > 1: + raise ValueError('More than one channel') + channel = next(iter(waveform.defined_channels)) + sample_count = floor(waveform.duration * self.__time_scaling * self.__sample_rate) + 1 sample_times = numpy.linspace(0, waveform.duration, sample_count) - sampled_waveform = waveform.sample(sample_times) + sampled_waveform = waveform.get_sampled(channel, sample_times) struct = dict(name=name, data=dict(wf=sampled_waveform.tolist(), marker=numpy.zeros_like(sampled_waveform).tolist(), diff --git a/setup.py b/setup.py index 0b911475c..f393f6812 100644 --- a/setup.py +++ b/setup.py @@ -15,19 +15,19 @@ packages = ['qctoolkit'] + ['qctoolkit.' + subpackage for subpackage in subpackages] setup(name='qctoolkit', - version='0.1', - description='Quantum Computing Toolkit', - author='qutech', - package_dir ={'qctoolkit': 'qctoolkit'}, - packages=packages, - tests_require=['pytest'], - install_requires=['py_expression_eval', 'numpy'] + requires_typing, - extras_require={ - 'testing': ['pytest'], - 'plotting': ['matplotlib'], - 'faster expressions': ['numexpr'], - 'VISA': ['pyvisa'], - 'tabor instruments': ['pytabor', 'teawg'] - }, - test_suite="tests", + version='0.1', + description='Quantum Computing Toolkit', + author='qutech', + package_dir={'qctoolkit': 'qctoolkit'}, + packages=packages, + tests_require=['pytest'], + install_requires=['py_expression_eval', 'numpy'] + requires_typing, + extras_require={ + 'testing': ['pytest'], + 'plotting': ['matplotlib'], + 'faster expressions': ['numexpr'], + 'VISA': ['pyvisa'], + 'tabor instruments': ['pytabor', 'teawg'] + }, + test_suite="tests", ) diff --git a/tests/format_tests.py b/tests/format_tests.py index d3a0f7b12..41cd099c4 100644 --- a/tests/format_tests.py +++ b/tests/format_tests.py @@ -26,7 +26,7 @@ def _test_attribute(self,package,name): return bool def test_annotations(self): - whitelist = ["__init__"] + whitelist = ["__init__", "__new__"] for root, dirs, files in os.walk(srcPath): for filename in files: methods = {} diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 51cd9a89a..ddbf994c3 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -1,7 +1,7 @@ import unittest import numpy as np -import qctoolkit.hardware.awgs.awg as awg +import qctoolkit.hardware.awgs.base as awg import qctoolkit.hardware.awgs.tektronix as tek import qctoolkit.pulses as pls diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py index 3228e47a6..199edddd6 100644 --- a/tests/hardware/program_tests.py +++ b/tests/hardware/program_tests.py @@ -6,16 +6,24 @@ from qctoolkit.hardware.program import Loop, MultiChannelProgram from qctoolkit.pulses.instructions import REPJInstruction, InstructionBlock, ImmutableInstructionBlock -from tests.pulses.sequencing_dummies import DummySingleChannelWaveform +from tests.pulses.sequencing_dummies import DummyWaveform from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform class LoopTests(unittest.TestCase): - def __init__(self, *args, waveform_data_generator=itertools.repeat(None), waveform_duration=None, **kwargs): + def __init__(self, *args, waveform_data_generator=itertools.repeat(None), waveform_duration=None, num_channels=2, **kwargs): super().__init__(*args, **kwargs) - def generate_waveform(): - return DummySingleChannelWaveform(sample_output=next(waveform_data_generator), duration=waveform_duration) + names = 'ABCDEFGH'[:num_channels] + + def generate_waveform(chan): + return DummyWaveform(sample_output=next(waveform_data_generator), + duration=waveform_duration, + defined_channels={chan}) + + def generate_multi_channel_waveform(): + return MultiChannelWaveform([generate_waveform(names[ch_i]) for ch_i in range(num_channels)]) + self.old_description = \ """\ @@ -64,49 +72,49 @@ def generate_waveform(): ->LOOP 9 times: ->EXEC {} 10 times ->EXEC {} 11 times""" - + self.root_block = InstructionBlock() self.loop_block11 = InstructionBlock() - self.loop_block11.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block1 = InstructionBlock() - self.loop_block1.add_instruction_repj(5,ImmutableInstructionBlock(self.loop_block11)) - self.loop_block21 = InstructionBlock() - self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block2 = InstructionBlock() - self.loop_block2.add_instruction_repj(2,ImmutableInstructionBlock(self.loop_block21)) - self.loop_block2.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block3 = InstructionBlock() - self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block411 = InstructionBlock() - self.loop_block411.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) self.loop_block412 = InstructionBlock() - self.loop_block412.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block41 = InstructionBlock() + self.loop_block4 = InstructionBlock() + self.loop_block421 = InstructionBlock() + self.loop_block422 = InstructionBlock() + self.loop_block42 = InstructionBlock() + + self.root_block.add_instruction_exec(generate_multi_channel_waveform()) + + self.loop_block11.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(self.loop_block11)) + + self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) + + self.loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(self.loop_block21)) + self.loop_block2.add_instruction_exec(generate_multi_channel_waveform()) + + self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) + + self.loop_block411.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block412.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411)) self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412)) - self.loop_block421 = InstructionBlock() - self.loop_block421.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block422 = InstructionBlock() - self.loop_block422.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block421.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block422.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block42 = InstructionBlock() self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421)) self.loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(self.loop_block422)) - self.loop_block4 = InstructionBlock() self.loop_block4.add_instruction_repj(6, ImmutableInstructionBlock(self.loop_block41)) self.loop_block4.add_instruction_repj(9, ImmutableInstructionBlock(self.loop_block42)) - self.root_block = InstructionBlock() - self.root_block.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1)) self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2)) self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3)) @@ -121,7 +129,7 @@ def get_root_loop(self): def test_repr(self): root_loop = self.get_root_loop() repres = root_loop.__repr__() - expected = self.new_description.format(*(loop.instruction + expected = self.new_description.format(*(loop.waveform for loop in root_loop.get_depth_first_iterator() if loop.is_leaf())) self.assertEqual(repres, expected) @@ -129,10 +137,10 @@ def test_is_leaf(self): root_loop = self.get_root_loop() for loop in root_loop.get_depth_first_iterator(): - self.assertTrue(bool(loop.is_leaf()) != bool(loop.instruction is None)) + self.assertTrue(bool(loop.is_leaf()) != bool(loop.waveform is None)) for loop in root_loop.get_breadth_first_iterator(): - self.assertTrue(bool(loop.is_leaf()) != bool(loop.instruction is None)) + self.assertTrue(bool(loop.is_leaf()) != bool(loop.waveform is None)) def test_depth(self): root_loop = self.get_root_loop() @@ -153,44 +161,11 @@ def test_is_balanced(self): self.assertTrue(root_loop[3].is_balanced()) self.assertTrue(root_loop[4].is_balanced()) - @unittest.skip("Further thinking needed.") - def test_merge(self): - # TODO: this test sucks - root_loop1 = self.get_root_loop() - root_loop2 = self.get_root_loop() - - for l1, l2 in zip(root_loop1,root_loop2): - l1.merge() - self.assertEqual(l1.__repr__(),l2.__repr__()) - - if not l1.is_leaf(): - for m1, m2 in zip(l1, l2): - m1.merge() - self.assertEqual(m1.__repr__(), m2.__repr__()) - - merge_loop = self.get_root_loop() - merge_loop[1].__repetition_count = 1 - merge_loop[2].__repetition_count = 1 - merge_loop.merge() - self.assertEqual(len(merge_loop) + 1, len(root_loop1)) - self.assertEqual(merge_loop[1].repetition_count, 1) - self.assertEqual(merge_loop[2].repetition_count, 3) - self.assertEqual(merge_loop[1].depth(), 1) - - merge_loop = Loop(self.root_block) - merge_loop[1].__repetition_count = 1 - merge_loop[2].__repetition_count = 1 - merge_loop[3].__repetition_count = 1 - merge_loop.merge() - self.assertEqual(len(merge_loop) + 2,len(root_loop1)) - self.assertEqual(merge_loop[1].repetition_count,1) - self.assertEqual(merge_loop[2].repetition_count,4) - class MultiChannelTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - wf = DummySingleChannelWaveform() + wf = DummyWaveform() self.descriptionA = \ """\ LOOP 1 times: @@ -224,40 +199,43 @@ def __init__(self, *args, **kwargs): ->EXEC {} 10 times ->EXEC {} 11 times""" - def generate_waveform(): - return DummySingleChannelWaveform(sample_output=None, duration=None) + def generate_waveform(channel): + return DummyWaveform(sample_output=None, duration=None, defined_channels={channel}) + + def generate_multi_channel_waveform(): + return MultiChannelWaveform([generate_waveform('A'), generate_waveform('B')]) self.loop_block11 = InstructionBlock() - self.loop_block11.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block11.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block1 = InstructionBlock() self.loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(self.loop_block11)) self.loop_block21 = InstructionBlock() - self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block21.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block2 = InstructionBlock() self.loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(self.loop_block21)) - self.loop_block2.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block2.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block3 = InstructionBlock() - self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) - self.loop_block3.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) + self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) self.loop_block411 = InstructionBlock() - self.loop_block411.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform()))) + self.loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) self.loop_block412 = InstructionBlock() - self.loop_block412.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform()))) + self.loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) self.loop_block41 = InstructionBlock() self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411)) self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412)) self.loop_block421 = InstructionBlock() - self.loop_block421.add_instruction_exec(MultiChannelWaveform(dict(B=generate_waveform()))) + self.loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) self.loop_block422 = InstructionBlock() - self.loop_block422.add_instruction_exec(MultiChannelWaveform(dict(B=generate_waveform()))) + self.loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) self.loop_block42 = InstructionBlock() self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421)) @@ -274,7 +252,7 @@ def generate_waveform(): frozenset('B'): ImmutableInstructionBlock(self.chan_block4B)}) self.root_block = InstructionBlock() - self.root_block.add_instruction_exec(MultiChannelWaveform(dict(A=generate_waveform(), B=generate_waveform()))) + self.root_block.add_instruction_exec(generate_multi_channel_waveform()) self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1)) self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2)) self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3)) @@ -289,9 +267,10 @@ def get_root_loop(self, channels): def test_via_repr(self): root_loopA = self.get_root_loop('A') root_loopB = self.get_root_loop('B') - reprA = self.descriptionA.format(*(loop.instruction - for loop in root_loopA.get_depth_first_iterator() if loop.is_leaf())) - reprB = self.descriptionB.format(*(loop.instruction + waveformsA = tuple(loop.waveform + for loop in root_loopA.get_depth_first_iterator() if loop.is_leaf()) + reprA = self.descriptionA.format(*waveformsA) + reprB = self.descriptionB.format(*(loop.waveform for loop in root_loopB.get_depth_first_iterator() if loop.is_leaf())) self.assertEqual(root_loopA.__repr__(), reprA) self.assertEqual(root_loopB.__repr__(), reprB) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 3f9193ce7..9306ac072 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -1,41 +1,35 @@ import unittest -from qctoolkit.hardware.awgs.tabor import TaborAWG, TaborException, TaborProgram +from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair from qctoolkit.hardware.program import MultiChannelProgram import numbers import itertools import numpy as np +from copy import copy, deepcopy +from teawg import model_properties_dict +from qctoolkit.hardware.util import voltage_to_uint16 from .program_tests import LoopTests -instrument_address = '127.0.0.1' -#instrument_address = '192.168.1.223' -instrument = TaborAWG(instrument_address) -instrument._visa_inst.timeout = 25000 - -class TaborAWGTests(unittest.TestCase): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - - def test_samplerate(self): - if not instrument.is_open: - self.skipTest("No instrument found.") - for ch in (1, 2, 3, 4): - self.assertIsInstance(instrument.samplerate(ch), numbers.Number) - with self.assertRaises(TaborException): - instrument.samplerate(0) - - def test_amplitude(self): - for ch in range(1, 5): - self.assertIsInstance(instrument.amplitude(ch), float) +try: + instrument_address = '127.0.0.1' + #instrument_address = '192.168.1.223' + #instrument_address = 'ASRL10::INSTR' + instrument = TaborAWGRepresentation(instrument_address) + instrument._visa_inst.timeout = 25000 +except: + instrument = None class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.waveform_data_generator = itertools.cycle([np.linspace(-0.5, 0.5, num=4048), + self.instr_props = next(model_properties_dict.values().__iter__()) + + @property + def waveform_data_generator(self): + return itertools.cycle([np.linspace(-0.5, 0.5, num=4048), np.concatenate((np.linspace(-0.5, 0.5, num=2024), np.linspace(0.5, -0.5, num=2024))), -0.5*np.cos(np.linspace(0, 2*np.pi, num=4048))]) @@ -48,14 +42,64 @@ def root_block(self): def working_root_block(self): block = self.root_block - def setUp(self): - if not instrument.is_open: - self.skipTest("Instrument not found.") - def test_init(self): prog = MultiChannelProgram(self.root_block) - TaborProgram(prog, instrument.dev_properties, {'A', 'B'}) - + TaborProgram(prog, self.instr_props, ('A', 'B'), (None, None)) + with self.assertRaises(Exception): + TaborProgram(prog, self.instr_props, ('A',), (None, None)) + + @unittest.skip + def test_setup_single_waveform_mode(self): + pass + + def test_sampled_segments(self): + + def my_gen(gen): + alternating_on_off = itertools.cycle((np.ones(4048), np.zeros(4048))) + chan_gen = gen + while True: + for _ in range(2): + yield next(chan_gen) + yield next(alternating_on_off) + yield np.zeros(4048) + + sample_rate = 8096 + with self.assertRaises(TaborException): + TaborProgram(MultiChannelProgram( + LoopTests(waveform_data_generator=my_gen(self.waveform_data_generator), + waveform_duration=1e-9, + num_channels=4).root_block + ), self.instr_props, ('A', 'B'), (None, None)).sampled_segments(8000, (1., 1.), (0, 0)) + + mcp = MultiChannelProgram( + LoopTests(waveform_data_generator=my_gen(self.waveform_data_generator), + waveform_duration=0.5, + num_channels=4).root_block + ) + prog = TaborProgram(mcp, self.instr_props, ('A', 'B'), (None, None)) + + sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0)) + + self.assertEqual(len(sampled), 3) + + prog = TaborProgram(mcp, self.instr_props, ('A', 'B'), ('C', None)) + sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0)) + self.assertEqual(len(sampled), 6) + + iteroe = my_gen(self.waveform_data_generator) + for i, sampled_seg in enumerate(sampled): + data = [next(iteroe) for _ in range(4)] + data = (voltage_to_uint16(data[0], 1., 0., 14), voltage_to_uint16(data[1], 1., 0., 14), data[2], data[3]) + if i % 2 == 0: + self.assertTrue(np.all(sampled_seg[1] >> 14 == np.ones(4048, dtype=np.uint16))) + else: + self.assertTrue(np.all(sampled_seg[1] >> 14 == np.zeros(4048, dtype=np.uint16))) + self.assertTrue(np.all(sampled_seg[0] >> 15 == np.zeros(4048, dtype=np.uint16))) + + self.assertTrue(np.all(sampled_seg[0] << 2 == data[0] << 2)) + self.assertTrue(np.all(sampled_seg[1] << 2 == data[1] << 2)) + + @unittest.skipIf(instrument is None, "Instrument not present") def test_upload(self): prog = MultiChannelProgram(self.root_block) program = TaborProgram(prog, instrument.dev_properties, ('A', 'B')) @@ -63,7 +107,31 @@ def test_upload(self): program.upload_to_device(instrument, (1, 2)) +class TaborAWGRepresentationTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + @unittest.skipIf(instrument is None, "Instrument not present") + def test_sample_rate(self): + if not instrument.is_open: + self.skipTest("No instrument found.") + for ch in (1, 2, 3, 4): + self.assertIsInstance(instrument.sample_rate(ch), numbers.Number) + with self.assertRaises(TaborException): + instrument.sample_rate(0) + + @unittest.skipIf(instrument is None, "Instrument not present") + def test_amplitude(self): + for ch in range(1, 5): + self.assertIsInstance(instrument.amplitude(ch), float) +class TaborChannelPairTests(unittest.TestCase): + def test_copy(self): + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + with self.assertRaises(NotImplementedError): + copy(channel_pair) + with self.assertRaises(NotImplementedError): + deepcopy(channel_pair) diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index cf7e3ea73..df690e422 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -3,7 +3,8 @@ import copy from typing import Optional, Dict, Set, Any, List -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow +from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate from qctoolkit.pulses.instructions import Waveform, EXECInstruction from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index d88b49be5..d38e4b77a 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -57,10 +57,9 @@ def test_get_measurement_windows(self): DummyWaveform(duration=2., measurement_windows=[('M', 0.1, 0.2), ('N', 0.5, 0.6)])) swf = SequenceWaveform(dwfs) - expected_windows = (('M', 0.2, 0.5), ('N', 1.6, 1.7), ('M', 4.1, 4.2), ('N', 4.5, 4.6)) - received_windows = tuple(swf.get_measurement_windows()) - self.assertEqual(len(received_windows), len(expected_windows)) - self.assertEqual(set(received_windows), set(expected_windows)) + expected_windows = sorted((('M', 0.2, 0.5), ('N', 1.6, 0.7), ('M', 4.1, 0.2), ('N', 4.5, 0.6))) + received_windows = sorted(tuple(swf.get_measurement_windows())) + self.assertEqual(received_windows, expected_windows) class SequencePulseTemplateTest(unittest.TestCase): diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index 61fb1bf0c..a82c5128c 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -5,14 +5,14 @@ import numpy """LOCAL IMPORTS""" +from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.serialization import Serializer from qctoolkit.pulses.instructions import Waveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock, SequencingElement from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementWindow +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate from qctoolkit.pulses.interpolation import InterpolationStrategy from qctoolkit.pulses.conditions import Condition -from qctoolkit.pulses.multi_channel_pulse_template import ChannelID class DummyParameter(Parameter): diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 160af2242..4cd9168d4 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -841,22 +841,24 @@ def test_few_entries(self) -> None: def test_unsafe_sample(self) -> None: interp = DummyInterpolationStrategy() entries = [WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)] + WaveformTableEntry(2.1, -33.2, interp), + WaveformTableEntry(5.7, 123.4, interp)] waveform = TableWaveform('A', entries, []) - sample_times = numpy.linspace(98.5, 103.5, num=11)-98 + sample_times = numpy.linspace(.5, 5.5, num=11) + + expected_interp_arguments = [((0, 0), (2.1, -33.2), [0.5, 1.0, 1.5, 2.0]), + ((2.1, -33.2), (5.7, 123.4), [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5])] + expected_result = numpy.copy(sample_times) result = waveform.unsafe_sample('A', sample_times) - expected_data = [((0, 0), (2.1, -33.2), [0.5, 1.0, 1.5, 2.0]), - ((2.1, -33.2), (5.7, 123.4), [2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5])] - self.assertEqual(expected_data, interp.call_arguments) - expected_result = sample_times - self.assertTrue(numpy.all(expected_result == result)) - output_expected = numpy.empty_like(expected_data) + self.assertEqual(expected_interp_arguments, interp.call_arguments) + numpy.testing.assert_equal(expected_result, result) + + output_expected = numpy.empty_like(expected_result) output_received = waveform.unsafe_sample('A', sample_times, output_array=output_expected) self.assertIs(output_expected, output_received) - self.assertTrue(numpy.all(expected_result == output_received)) + numpy.testing.assert_equal(expected_result, output_received) def test_simple_properties(self): interp = DummyInterpolationStrategy() @@ -868,7 +870,7 @@ def test_simple_properties(self): waveform = TableWaveform(chan, entries, meas) self.assertEqual(waveform.defined_channels, {chan}) - self.assertEqual(waveform.get_measurement_windows(), meas) + self.assertEqual(list(waveform.get_measurement_windows()), meas) self.assertIs(waveform.unsafe_get_subset_for_channels('A'), waveform) From b66cfbe3e7c44616b8575108cb9e1de22d37e0a7 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 21 Feb 2017 21:37:09 +0100 Subject: [PATCH 018/116] Fix bug in parameter mapping serialization --- qctoolkit/pulses/pulse_template_parameter_mapping.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index 48ad493ea..b48f97d03 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -89,8 +89,9 @@ def atomicity(self, val) -> None: self.__template.atomicity = val def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: + parameter_mapping_dict = dict((key, str(expression)) for key, expression in self.__parameter_mapping.items()) return dict(template=serializer.dictify(self.template), - parameter_mapping=self.__parameter_mapping, + parameter_mapping=parameter_mapping_dict, measurement_mapping=self.__measurement_mapping, channel_mapping=self.__channel_mapping) From 830284391ca874bd9a9604a5a4f79ec67ce2da63 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 21 Feb 2017 21:39:43 +0100 Subject: [PATCH 019/116] Skip tektronix unittests Fix DummyAWG unittests --- qctoolkit/hardware/awgs/base.py | 52 ++++++++++++++++------------ qctoolkit/hardware/awgs/tektronix.py | 10 ++++++ tests/hardware/awg_tests.py | 8 +++-- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index ab39d6ec0..daf4e6ac2 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -102,7 +102,8 @@ def __init__(self, memory: int=100, sample_rate: float=10, output_range: Tuple[float, float]=(-5,5), - num_channels: int=1) -> None: + num_channels: int=1, + num_markers: int=1) -> None: """Create a new DummyAWG instance. Args: @@ -111,21 +112,22 @@ def __init__(self, output_range (float, float): A (min,max)-tuple of possible output values. (default = (-5,5)). """ - self.__programs = {} # contains program names and programs - self.__waveform_memory = [None for i in range(memory)] - self.__waveform_indices = {} # dict that maps from waveform hash to memory index - self.__program_wfs = {} # contains program names and necessary waveforms indices - self.__sample_rate = sample_rate - self.__output_range = output_range - self.__num_channels = num_channels + self._programs = {} # contains program names and programs + self._waveform_memory = [None for i in range(memory)] + self._waveform_indices = {} # dict that maps from waveform hash to memory index + self._program_wfs = {} # contains program names and necessary waveforms indices + self._sample_rate = sample_rate + self._output_range = output_range + self._num_channels = num_channels + self._num_markers = num_markers def add_waveform(self, waveform) -> int: try: - index = self.__waveform_memory.index(None) + index = self._waveform_memory.index(None) except ValueError: raise OutOfWaveformMemoryException() - self.__waveform_memory[index] = waveform - self.__waveform_indices[hash(waveform)] = index + self._waveform_memory[index] = waveform + self._waveform_indices[hash(waveform)] = index return index def upload(self, name, program, force=False) -> None: @@ -136,36 +138,36 @@ def upload(self, name, program, force=False) -> None: self.remove(name) self.upload(name, program) else: - self.__programs[name] = program + self._programs[name] = program exec_blocks = filter(lambda x: type(x) == EXECInstruction, program) indices = frozenset(self.add_waveform(block.waveform) for block in exec_blocks) - self.__program_wfs[name] = indices + self._program_wfs[name] = indices def remove(self,name) -> None: if name in self.programs: - self.__programs.pop(name) + self._programs.pop(name) self.program_wfs.pop(name) self.clean() def clean(self) -> None: - necessary_wfs = reduce(lambda acc, s: acc.union(s), self.__program_wfs.values(), set()) - all_wfs = set(self.__waveform_indices.values()) + necessary_wfs = reduce(lambda acc, s: acc.union(s), self._program_wfs.values(), set()) + all_wfs = set(self._waveform_indices.values()) delete = all_wfs - necessary_wfs for index in delete: - wf = self.__waveform_memory(index) - self.__waveform_indices.pop(wf) - self.__waveform_memory = None + wf = self._waveform_memory(index) + self._waveform_indices.pop(wf) + self._waveform_memory = None def arm(self, name: str) -> None: raise NotImplementedError() @property def programs(self) -> Set[str]: - return frozenset(self.__programs.keys()) + return frozenset(self._programs.keys()) @property def output_range(self) -> Tuple[float, float]: - return self.__output_range + return self._output_range @property def identifier(self) -> str: @@ -173,11 +175,15 @@ def identifier(self) -> str: @property def sample_rate(self) -> float: - return self.__sample_rate + return self._sample_rate @property def num_channels(self): - return self.__num_channels + return self._num_channels + + @property + def num_markers(self): + return self._num_markers class ProgramOverwriteException(Exception): diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py index 1de731838..c1900f872 100644 --- a/qctoolkit/hardware/awgs/tektronix.py +++ b/qctoolkit/hardware/awgs/tektronix.py @@ -145,7 +145,17 @@ def __init__(self, ip: str, port: int, sample_rate: float, first_index=None, sim self.__current_index = self.__first_index + 1 # TODO: load dummy pulse to first_index + @property + def num_channels(self): + raise NotImplementedError() + + @property + def num_markers(self): + raise NotImplementedError() + @property + def arm(self, name: str): + raise NotImplementedError() @property def output_range(self) -> Tuple[float, float]: diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index ddbf994c3..9ab13ab62 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -14,7 +14,7 @@ def setUp(self): self.sequencer = pls.Sequencer() for i in range(1,12): pars = dict(value=i) - self.sequencer.push(self.pulse_template, pars) + self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) self.program = self.sequencer.build() def test_OutOfMemoryException(self): @@ -32,7 +32,7 @@ def test_upload(self): dummy = awg.DummyAWG(100) dummy.upload('program',self.program) memory_part = [None for i in range(89)] - self.assertEqual(dummy._DummyAWG__waveform_memory[11:], memory_part) + self.assertEqual(dummy._waveform_memory[11:], memory_part) self.assertEqual(dummy.programs, set(['program'])) @@ -44,15 +44,17 @@ def setUp(self): self.sequencer = pls.Sequencer() for i in range(1,12): pars = dict(value=i) - self.sequencer.push(self.pulse_template, pars) + self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) self.program = self.sequencer.build() + @unittest.skip def test_ProgramOverwriteException(self): dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) dummy.upload('program', self.program) with self.assertRaises(awg.ProgramOverwriteException): dummy.upload('program', self.program) + @unittest.skip def test_upload(self): dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) dummy.upload('program', self.program) From 307ce68f4c439a60e782d698f333ba6f77d1dccb Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 21 Feb 2017 21:36:38 +0100 Subject: [PATCH 020/116] Use sympy as expression backend. This enables symbolic comparisons between expressions. --- qctoolkit/expressions.py | 84 +++++++++++++++++---- qctoolkit/pulses/function_pulse_template.py | 27 ++----- qctoolkit/pulses/parameters.py | 2 +- qctoolkit/pulses/table_pulse_template.py | 2 +- setup.py | 2 +- tests/expression_tests.py | 35 +++++++-- tests/pulses/function_pulse_tests.py | 8 -- 7 files changed, 106 insertions(+), 54 deletions(-) diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index c97e73ce0..1753f68d1 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -2,8 +2,10 @@ This module defines the class Expression to represent mathematical expression as well as corresponding exception classes. """ -from typing import Any, Dict, Iterable, Optional -import py_expression_eval +from typing import Any, Dict, Iterable, Optional, Union +from numbers import Number +import sympy +import numpy from qctoolkit.comparable import Comparable from qctoolkit.serialization import Serializable, Serializer @@ -19,23 +21,35 @@ def __init__(self, ex: str) -> None: Receives the mathematical expression which shall be represented by the object as a string which will be parsed using py_expression_eval. For available operators, functions and - constants see - https://github.com/AxiaCore/py-expression-eval/#available-operators-constants-and-functions. - In addition, the ** operator may be used for exponentiation instead of the ^ operator. + constants see SymPy documentation Args: ex (string): The mathematical expression represented as a string """ super().__init__() self.__string = str(ex) - self.__expression = py_expression_eval.Parser().parse(ex.replace('**', '^')) + self.__expression = sympy.sympify(ex) + self.__variables = tuple(str(var) for var in self.__expression.free_symbols) + self.__expression_lambda = sympy.lambdify(self.variables(), self.__expression, 'numpy') def __str__(self) -> str: return self.__string + def get_most_simple_representation(self): + if self.__expression.free_symbols: + return str(self.__expression) + elif self.__expression.is_integer: + return int(self.__expression) + elif self.__expression.is_complex: + return complex(self.__expression) + elif self.__expression.is_real: + return float(self.__expression) + else: + return str(self.__expression) + @property - def compare_key(self) -> Any: - return str(self) + def compare_key(self) -> str: + return self.__expression def variables(self) -> Iterable[str]: """ Get all free variables in the expression. @@ -43,28 +57,45 @@ def variables(self) -> Iterable[str]: Returns: A collection of all free variables occurring in the expression. """ - return self.__expression.variables() + return self.__variables - def evaluate(self, **kwargs) -> float: + def evaluate_numeric(self, **kwargs) -> Union[Number, numpy.ndarray]: """Evaluate the expression with the required variables passed in as kwargs. Args: (float): Values for the free variables of the expression as keyword arguments where stand for the name of the variable. For example, evaluation of the expression "2*x" could be implemented as - Expresson("2*x").evaluate(x=2.5). + Expression("2*x").evaluate(x=2.5). Returns: - The result of evaluating the expression with the given values for the free variables. + The numeric result of evaluating the expression with the given values for the free variables. Raises: ExpressionVariableMissingException if a value for a variable is not provided. """ try: - return self.__expression.evaluate(kwargs) - except Exception as excp: - raise ExpressionVariableMissingException(str(excp).split(' ')[2], self) from excp + # drop irrelevant variables before passing to lambda + result = self.__expression_lambda(**dict((v, kwargs[v]) for v in self.variables())) + except KeyError as key_error: + raise ExpressionVariableMissingException(key_error.args[0], self) from key_error + + if isinstance(result, numpy.ndarray) and issubclass(result.dtype.type, Number): + return result + if isinstance(result, Number): + return result + raise NonNumericEvaluation(self, result, kwargs) + + def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> sympy.Expr: + """Evaluate the expression symbolically. + + Args: + substitutions (dict): Substitutions to undertake + Returns: + + """ + return self.__expression.subs(substitutions) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - return dict(type=serializer.get_type_identifier(self), expression=str(self)) + return dict(expression=str(self)) @staticmethod def deserialize(serializer: 'Serializer', **kwargs) -> Serializable: @@ -90,3 +121,24 @@ def __init__(self, variable: str, expression: Expression) -> None: def __str__(self) -> str: return "Could not evaluate <{}>: A value for variable <{}> is missing!".format( str(self.expression), self.variable) + + +class NonNumericEvaluation(Exception): + """An exception that is raised if the result of evaluate_numeric is not a number. + + See also: + qctoolkit.expressions.Expression.evaluate_numeric + """ + + def __init__(self, expression: Expression, non_numeric_result: Any, call_arguments: Dict): + self.expression = expression + self.non_numeric_result = non_numeric_result + self.call_arguments = call_arguments + + def __str__(self) -> str: + if isinstance(self.non_numeric_result, numpy.ndarray): + dtype = self.non_numeric_result.dtype + else: + dtype = type(self.non_numeric_result) + return "The result of evaluate_numeric is of type {} " \ + "which is not a number".format(dtype) diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 64bee0bb1..38c8b95dc 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -73,22 +73,6 @@ def parameter_names(self) -> Set[str]: def parameter_declarations(self) -> Set[ParameterDeclaration]: return {ParameterDeclaration(param_name) for param_name in self.parameter_names} - def get_pulse_length(self, parameters: Dict[str, Parameter]) -> float: - """Return the length of this pulse for the given parameters. - - OBSOLETE/FLAWED? Just used in by get_measurement_windows which days are counted. - - Args: - parameters (Dict(str -> Parameter)): A mapping of parameter name to parameter objects. - """ - missing_parameters = self.__parameter_names - set(parameters.keys()) - for missing_parameter in missing_parameters: - raise ParameterNotProvidedException(missing_parameter) - return self.__duration_expression.evaluate( - **{parameter_name: parameter.get_value() - for (parameter_name, parameter) in parameters.items()} - ) - @property def measurement_names(self) -> Set[str]: return set() @@ -166,7 +150,7 @@ def __init__(self, super().__init__() self.__expression = expression self.__parameters = parameters - self.__duration = duration_expression.evaluate(**self.__parameters) + self.__duration = duration_expression.evaluate_numeric(**self.__parameters) self.__channel_id = channel self.__measurement_windows = measurement_windows @@ -177,10 +161,10 @@ def defined_channels(self) -> Set[ChannelID]: def get_measurement_windows(self) -> List[MeasurementWindow]: return self.__measurement_windows - def __evaluate_partially(self, t) -> float: + def __evaluate(self, t) -> float: params = self.__parameters.copy() - params.update({"t":t}) - return self.__expression.evaluate(**params) + params.update({"t": t}) + return self.__expression.evaluate_numeric(**params) @property def compare_key(self) -> Any: @@ -196,8 +180,7 @@ def unsafe_sample(self, output_array: Union[np.ndarray, None] = None) -> np.ndarray: if output_array is None: output_array = np.empty(len(sample_times)) - for i, t in enumerate(sample_times): - output_array[i] = self.__evaluate_partially(t) + output_array[:] = self.__evaluate(sample_times) return output_array def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> Waveform: diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index b2e337915..e35e7082f 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -127,7 +127,7 @@ def get_value(self) -> float: "cannot be evaluated.") dependencies = self.__collect_dependencies() variables = {k: float(dependencies[k]) for k in dependencies} - return self.__expression.evaluate(**variables) + return self.__expression.evaluate_numeric(**variables) @property def requires_stop(self) -> bool: diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 1fa7919ea..1deffc2b7 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -400,7 +400,7 @@ def get_measurement_windows(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: def get_val(v): - return v if not isinstance(v, Expression) else v.evaluate( + return v if not isinstance(v, Expression) else v.evaluate_numeric( **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] for name_ in v.variables()}) diff --git a/setup.py b/setup.py index f393f6812..91f314551 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ package_dir={'qctoolkit': 'qctoolkit'}, packages=packages, tests_require=['pytest'], - install_requires=['py_expression_eval', 'numpy'] + requires_typing, + install_requires=['sympy', 'numpy'] + requires_typing, extras_require={ 'testing': ['pytest'], 'plotting': ['matplotlib'], diff --git a/tests/expression_tests.py b/tests/expression_tests.py index ebe26d8db..c11e1738f 100644 --- a/tests/expression_tests.py +++ b/tests/expression_tests.py @@ -1,22 +1,47 @@ import unittest +import numpy as np +from sympy import sympify + from qctoolkit.expressions import Expression, ExpressionVariableMissingException class ExpressionTests(unittest.TestCase): - def test_evaluate(self) -> None: + def test_evaluate_numeric(self) -> None: e = Expression('a * b + c') params = { 'a': 2, 'b': 1.5, 'c': -7 } - self.assertEqual(2*1.5 - 7, e.evaluate(**params)) + self.assertEqual(2 * 1.5 - 7, e.evaluate_numeric(**params)) + + def test_evaluate_numpy(self): + self + e = Expression('a * b + c') + params = { + 'a': 2*np.ones(4), + 'b': 1.5*np.ones(4), + 'c': -7*np.ones(4) + } + np.testing.assert_equal((2 * 1.5 - 7) * np.ones(4), e.evaluate_numeric(**params)) + + def test_evaluate_symbolic(self): + e = Expression('a * b + c') + params = { + 'a': 'd', + 'c': -7 + } + result = e.evaluate_symbolic(params) + expected = sympify('d*b-7') + self.assertEqual(result, expected) def test_variables(self) -> None: - e = Expression('4 ** PI + x * foo') - self.assertEqual(sorted(['foo','x']), sorted(e.variables())) + e = Expression('4 ** pi + x * foo') + expected = sorted(['foo', 'x']) + received = sorted(e.variables()) + self.assertEqual(expected, received) def test_evaluate_variable_missing(self) -> None: e = Expression('a * b + c') @@ -24,4 +49,4 @@ def test_evaluate_variable_missing(self) -> None: 'b': 1.5 } with self.assertRaises(ExpressionVariableMissingException): - e.evaluate(**params) \ No newline at end of file + e.evaluate_numeric(**params) diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index 0633848b9..a6472b2e7 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -20,17 +20,9 @@ def setUp(self) -> None: self.fpt = FunctionPulseTemplate(self.s, self.s2,channel='A') self.pars = dict(a=DummyParameter(1), b=DummyParameter(2), c=DummyParameter(136.78)) - def test_get_pulse_length(self) -> None: - self.assertEqual(136.78, self.fpt.get_pulse_length(self.pars)) - - def test_get_pulse_length_missing_parameter(self) -> None: - with self.assertRaises(ParameterNotProvidedException): - self.fpt.get_pulse_length(dict(b=DummyParameter(26.3267))) - def test_is_interruptable(self) -> None: self.assertFalse(self.fpt.is_interruptable) - def test_defined_channels(self) -> None: self.assertEqual({'A'}, self.fpt.defined_channels) From 364c36d0bfeed819750494aeb26d0cfbe3d7168b Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 2 Mar 2017 17:14:00 +0100 Subject: [PATCH 021/116] Introduce ForLoopPulseTemplate and base all looping pulse templates on a common base class --- qctoolkit/pulses/loop_pulse_template.py | 281 +++++++++++++++--- qctoolkit/pulses/repetition_pulse_template.py | 118 ++++---- tests/pulses/loop_pulse_template_tests.py | 107 ++++++- .../pulses/repetition_pulse_template_tests.py | 1 + 4 files changed, 390 insertions(+), 117 deletions(-) diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index cc6897aba..1ee6b58d2 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -2,20 +2,221 @@ another PulseTemplate based on a condition.""" -from typing import Dict, Set, Optional, Any +from typing import Dict, Set, Optional, Any, Union, Tuple, NamedTuple from qctoolkit.serialization import Serializer -from qctoolkit.pulses.parameters import Parameter -from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.expressions import Expression +from qctoolkit.pulses.parameters import Parameter, ConstantParameter, InvalidParameterNameException +from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate, ChannelID from qctoolkit.pulses.conditions import Condition, ConditionMissingException from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.pulses.sequencing import Sequencer +from qctoolkit.pulses.sequence_pulse_template import SequenceWaveform as ForLoopWaveform -__all__ = ['LoopPulseTemplate', 'ConditionMissingException'] +__all__ = ['WhileLoopPulseTemplate', 'ConditionMissingException'] -class LoopPulseTemplate(PulseTemplate): +class LoopPulseTemplate(PossiblyAtomicPulseTemplate): + """Base class for loop based pulse templates""" + def __init__(self, body: PulseTemplate, identifier: Optional[str]=None): + super().__init__(identifier=identifier) + self.__body = body + self.__atomicity = False + + @property + def body(self) -> PulseTemplate: + return self.__body + + @property + def defined_channels(self) -> Set['ChannelID']: + return self.__body.defined_channels + + @property + def measurement_names(self) -> Set[str]: + return self.__body.measurement_names + + @property + def is_interruptable(self): + raise NotImplementedError() + + @property + def atomicity(self) -> bool: + if self.__body.atomicity is False: + self.__atomicity = False + return self.__atomicity + + @atomicity.setter + def atomicity(self, val) -> None: + if val and self.__body.atomicity is False: + raise ValueError('Cannot make {} atomic as the body is not'.format(type(self))) + self.__atomicity = val + + +class ParametrizedRange: + """Parametrized range """ + def __init__(self, *args, **kwargs): + if args and kwargs: + raise TypeError('ParametrizedRange only takes either positional or keyword arguments') + elif kwargs: + start = kwargs['start'] + stop = kwargs['stop'] + step = kwargs['step'] + elif len(args) in (1, 2, 3): + if len(args) == 3: + start, stop, step = args + elif len(args) == 2: + (start, stop), step = args, 1 + elif len(args) == 1: + start, (stop,), step = 0, args, 1 + else: + raise TypeError('ParametrizedRange expected 1 to 3 arguments, got {}'.format(len(args))) + + self.start = Expression(start) + self.stop = Expression(stop) + self.step = Expression(step) + + def to_tuple(self): + """Return a simple representation of the range which is useful for comparison and serialization""" + return (self.start.get_most_simple_representation(), + self.stop.get_most_simple_representation(), + self.step.get_most_simple_representation()) + + def to_range(self, parameters: Dict[str, Any]): + return range(self.start.evaluate_numeric(**parameters), + self.stop.evaluate_numeric(**parameters), + self.step.evaluate_numeric(**parameters)) + + @property + def parameter_names(self) -> Set[str]: + return set(self.start.variables()) | set(self.stop.variables()) | set(self.step.variables()) + + +class ForLoopPulseTemplate(LoopPulseTemplate): + def __init__(self, + body: PulseTemplate, + loop_index: str, + loop_range: Union[int, + range, + str, + Tuple[Any, Any], + Tuple[Any, Any, Any], + ParametrizedRange], + identifier: Optional[str]=None): + super().__init__(body=body, identifier=identifier) + + if isinstance(loop_range, ParametrizedRange): + self._loop_range = loop_range + elif isinstance(loop_range, (int, str)): + self._loop_range = ParametrizedRange(loop_range) + elif isinstance(loop_range, tuple): + self._loop_range = ParametrizedRange(*loop_range) + elif isinstance(loop_range, range): + self._loop_range = ParametrizedRange(start=loop_range.start, + stop=loop_range.stop, + step=loop_range.step) + else: + raise ValueError('loop_range is not valid') + + if not loop_index.isidentifier(): + raise InvalidParameterNameException(loop_index) + body_parameters = self.body.parameter_names + if loop_index not in body_parameters: + raise LoopIndexNotUsedException(loop_index, body_parameters) + self._loop_index = loop_index + + @property + def loop_index(self) -> str: + return self._loop_index + + @property + def loop_range(self) -> ParametrizedRange: + return self._loop_range + + @property + def parameter_names(self) -> Set[str]: + parameter_names = self.body.parameter_names + parameter_names.remove(self._loop_index) + return parameter_names | self._loop_range.parameter_names + + @property + def parameter_declarations(self): + raise NotImplementedError() + + def _body_parameter_generator(self, parameters: Dict[str, Parameter], forward=True): + loop_range_parameters = dict((parameter_name, parameters[parameter_name].get_value()) + for parameter_name in self._loop_range.parameter_names) + loop_range = self._loop_range.to_range(loop_range_parameters) + + parameters = dict((parameter_name, parameters[parameter_name]) + for parameter_name in self.body.parameter_names if parameter_name != self._loop_index) + loop_range = loop_range if forward else reversed(loop_range) + for loop_index_value in loop_range: + local_parameters = parameters.copy() + local_parameters[self._loop_index] = ConstantParameter(loop_index_value) + yield local_parameters + + def build_sequence(self, + sequencer: Sequencer, + parameters: Dict[str, Parameter], + conditions: Dict[str, Condition], + measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], + instruction_block: InstructionBlock) -> None: + + if self.atomicity: + # atomicity can only be enabled if the loop index is not used + self.atomic_build_sequence(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + else: + for local_parameters in self._body_parameter_generator(parameters, forward=False): + sequencer.push(self.body, + parameters=local_parameters, + conditions=conditions, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) + + def build_waveform(self, parameters: Dict[str, Parameter]) -> ForLoopWaveform: + return ForLoopWaveform([self.body.build_waveform(local_parameters) + for local_parameters in self._body_parameter_generator(parameters, forward=True)]) + + def requires_stop(self, + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition']): + if any(parameters[parameter_name].requires_stop() for parameter_name in self._loop_range.parameter_names): + return True + if self.atomicity: + return any(self.body.requires_stop(local_parameters, conditions) + for local_parameters in self._body_parameter_generator(parameters=parameters)) + else: + return False + + def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: + data = dict( + type=serializer.get_type_identifier(self), + body=serializer.dictify(self.body), + loop_range=self._loop_range.to_tuple(), + loop_index=self._loop_index + ) + return data + + @staticmethod + def deserialize(serializer: Serializer, + body: Dict[str, Any], + loop_range: Tuple, + loop_index: str, + identifier: Optional[str]=None) -> 'WhileLoopPulseTemplate': + body = serializer.deserialize(body) + return ForLoopPulseTemplate(body=body, + identifier=identifier, + loop_range=loop_range, + loop_index=loop_index) + + +class WhileLoopPulseTemplate(LoopPulseTemplate): """Conditional looping in a pulse. A LoopPulseTemplate is a PulseTemplate which body (subtemplate) is repeated @@ -32,52 +233,36 @@ def __init__(self, condition: str, body: PulseTemplate, identifier: Optional[str holds. identifier (str): A unique identifier for use in serialization. (optional) """ - super().__init__(identifier=identifier) - self.__condition = condition - self.__body = body + super().__init__(body=body, identifier=identifier) + self._condition = condition def __str__(self) -> str: - return "LoopPulseTemplate: Condition <{}>, Body <{}>".format(self.__condition, self.__body) - - @property - def body(self) -> PulseTemplate: - """This LoopPulseTemplate's body/subtemplate.""" - return self.__body + return "LoopPulseTemplate: Condition <{}>, Body <{}>".format(self._condition, self.body) @property def condition(self) -> str: """This LoopPulseTemplate's condition.""" - return self.__condition + return self._condition @property def parameter_names(self) -> Set[str]: - return self.__body.parameter_names + return self.body.parameter_names @property def parameter_declarations(self) -> Set[str]: - return self.__body.parameter_declarations - - @property - def is_interruptable(self) -> bool: - return self.__body.is_interruptable - - @property - def defined_channels(self) -> Set['ChannelID']: - return self.__body.defined_channels - - @property - def measurement_names(self) -> Set[str]: - return self.__body.measurement_names - - @property - def atomicity(self) -> bool: - False + return self.body.parameter_declarations def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: try: - return conditions[self.__condition] + return conditions[self._condition] except: - raise ConditionMissingException(self.__condition) + raise ConditionMissingException(self._condition) + + def build_waveform(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]): + raise NotImplementedError() def build_sequence(self, sequencer: Sequencer, @@ -87,7 +272,7 @@ def build_sequence(self, channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: self.__obtain_condition_object(conditions).build_sequence_loop(self, - self.__body, + self.body, sequencer, parameters, conditions, @@ -103,8 +288,9 @@ def requires_stop(self, def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict( type=serializer.get_type_identifier(self), - condition=self.__condition, - body=serializer.dictify(self.__body) + condition=self._condition, + body=serializer.dictify(self.body), + atomicity=self.atomicity ) return data @@ -112,6 +298,21 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: def deserialize(serializer: Serializer, condition: str, body: Dict[str, Any], - identifier: Optional[str]=None) -> 'LoopPulseTemplate': + atomicity: bool, + identifier: Optional[str]=None) -> 'WhileLoopPulseTemplate': body = serializer.deserialize(body) - return LoopPulseTemplate(condition, body, identifier=identifier) + result = WhileLoopPulseTemplate(condition=condition, + body=body, + identifier=identifier) + result.atomicity = atomicity + return result + + +class LoopIndexNotUsedException(Exception): + def __init__(self, loop_index: str, body_parameter_names: Set[str]): + self.loop_index = loop_index + self.body_parameter_names = body_parameter_names + + def __str__(self) -> str: + return "The parameter {} is missing in the body's parameter names: {}".format(self.loop_index, + self.body_parameter_names) diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index e83618924..fc7808492 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -9,9 +9,10 @@ from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate +from qctoolkit.pulses.loop_pulse_template import LoopPulseTemplate from qctoolkit.pulses.sequencing import Sequencer from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer, Waveform -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter +from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, ConstantParameter from qctoolkit.pulses.conditions import Condition @@ -25,14 +26,14 @@ def __init__(self, body: Waveform, repetition_count: int): :param subwaveforms: All waveforms must have the same defined channels """ - self.__body = body - self.__repetition_count = repetition_count + self._body = body + self._repetition_count = repetition_count if repetition_count < 1 or not isinstance(repetition_count, int): raise ValueError('Repetition count must be an integer >0') @property def defined_channels(self) -> Set[ChannelID]: - return self.__body.defined_channels + return self._body.defined_channels def unsafe_sample(self, channel: ChannelID, @@ -40,21 +41,21 @@ def unsafe_sample(self, output_array: Union[np.ndarray, None]=None) -> np.ndarray: if output_array is None: output_array = np.empty(len(sample_times)) - body_duration = self.__body.duration - for i in range(self.__repetition_count): + body_duration = self._body.duration + for i in range(self._repetition_count): indices = slice(*np.searchsorted(sample_times, (i*body_duration, (i+1)*body_duration))) - self.__body.unsafe_sample(channel=channel, + self._body.unsafe_sample(channel=channel, sample_times=sample_times[indices], output_array=output_array[indices]) return output_array @property def compare_key(self) -> Tuple[Any, int]: - return self.__body.compare_key, self.__repetition_count + return self._body.compare_key, self._repetition_count @property def duration(self) -> float: - return self.__body.duration*self.__repetition_count + return self._body.duration*self._repetition_count def get_measurement_windows(self) -> Iterable[MeasurementWindow]: def get_measurement_window_generator(body: Waveform, repetition_count: int): @@ -62,14 +63,14 @@ def get_measurement_window_generator(body: Waveform, repetition_count: int): for i in range(repetition_count): for (name, begin, length) in body_windows: yield (name, begin+i*body.duration, length) - return get_measurement_window_generator(self.__body, self.__repetition_count) + return get_measurement_window_generator(self._body, self._repetition_count) def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'RepetitionWaveform': - return RepetitionWaveform(body=self.__body.unsafe_get_subset_for_channels(channels), - repetition_count=self.__repetition_count) + return RepetitionWaveform(body=self._body.unsafe_get_subset_for_channels(channels), + repetition_count=self._repetition_count) -class RepetitionPulseTemplate(PossiblyAtomicPulseTemplate): +class RepetitionPulseTemplate(LoopPulseTemplate): """Repeat a PulseTemplate a constant number of times. The equivalent to a simple for-loop in common programming languages in qctoolkit's pulse @@ -78,7 +79,7 @@ class RepetitionPulseTemplate(PossiblyAtomicPulseTemplate): def __init__(self, body: PulseTemplate, - repetition_count: Union[int, ParameterDeclaration], + repetition_count: Union[int, ParameterDeclaration, str], identifier: Optional[str]=None) -> None: """Create a new RepetitionPulseTemplate instance. @@ -86,62 +87,53 @@ def __init__(self, body (PulseTemplate): The PulseTemplate which will be repeated. repetition_count (int or ParameterDeclaration): The number of repetitions either as a constant integer value or as a parameter declaration. + loop_index (str): If specified the loop index identifier (str): A unique identifier for use in serialization. (optional) """ - super().__init__(identifier=identifier) - self.__body = body - self.__repetition_count = repetition_count - self.__atomicity = False - - @property - def body(self) -> PulseTemplate: - """The PulseTemplate which is repeated by this RepetitionPulseTemplate.""" - return self.__body + super().__init__(identifier=identifier, body=body) + if isinstance(repetition_count, float) and repetition_count.is_integer(): + repetition_count = int(repetition_count) + + if isinstance(repetition_count, str): + self._repetition_count = ParameterDeclaration(repetition_count, min=0) + elif isinstance(repetition_count, (int, ParameterDeclaration)): + self._repetition_count = repetition_count + else: + raise ValueError('Invalid repetition count type: {}'.format(type(repetition_count))) @property - def repetition_count(self) -> Union[int, ParameterDeclaration]: + def repetition_count(self) -> ParameterDeclaration: """The amount of repetitions. Either a constant integer or a ParameterDeclaration object.""" - return self.__repetition_count + return self._repetition_count + + def get_repetition_count_value(self, parameters: Dict[str, Parameter]) -> int: + if isinstance(self._repetition_count, ParameterDeclaration): + value = self._repetition_count.get_value(parameters) + if isinstance(value, float) and not value.is_integer(): + raise ParameterNotIntegerException(self._repetition_count.name, value) + return value + else: return self._repetition_count def __str__(self) -> str: return "RepetitionPulseTemplate: <{}> times <{}>"\ - .format(self.__repetition_count, self.__body) + .format(self._repetition_count, self.body) @property def parameter_names(self) -> Set[str]: - return self.__body.parameter_names + return set(parameter.name for parameter in self.parameter_declarations) @property def parameter_declarations(self) -> Set[str]: - return self.__body.parameter_declarations - - @property - def is_interruptable(self) -> bool: - return self.__body.is_interruptable - - @property - def defined_channels(self) -> Set['ChannelID']: - return self.__body.defined_channels + return self.body.parameter_declarations | ({self._repetition_count} if isinstance(self._repetition_count, + ParameterDeclaration) + else set()) @property def measurement_names(self) -> Set[str]: - return self.__body.measurement_names - - @property - def atomicity(self) -> bool: - if self.__body.atomicity is False: - self.__atomicity = False - return self.__atomicity - - @atomicity.setter - def atomicity(self, val) -> None: - if val and self.__body.atomicity is False: - raise ValueError('Cannot make RepetitionPulseTemplate atomic as the body is not') - self.__atomicity = val + return self.body.measurement_names def build_waveform(self, parameters: Dict[str, Parameter]) -> RepetitionWaveform: - return RepetitionWaveform(self.__body.build_waveform(parameters), - self.__repetition_count.get_value(parameters)) + return RepetitionWaveform(self.body.build_waveform(parameters), self.get_repetition_count_value(parameters)) def build_sequence(self, sequencer: Sequencer, @@ -150,41 +142,37 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: + if self.atomicity: + # atomicity can only be enabled if the loop index is not used self.atomic_build_sequence(parameters=parameters, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping, instruction_block=instruction_block) else: - repetition_count = self.__repetition_count - if isinstance(repetition_count, ParameterDeclaration): - repetition_count = repetition_count.get_value(parameters) - if not repetition_count.is_integer(): - raise ParameterNotIntegerException(self.__repetition_count.name, repetition_count) - body_block = InstructionBlock() body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) - instruction_block.add_instruction_repj(int(repetition_count), body_block) + instruction_block.add_instruction_repj(self.get_repetition_count_value(parameters), body_block) sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition]) -> bool: - if isinstance(self.__repetition_count, ParameterDeclaration): - if parameters[self.__repetition_count.name].requires_stop: - return True - return False + if isinstance(self._repetition_count, ParameterDeclaration): + return parameters[self._repetition_count.name].requires_stop + else: + return False def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - repetition_count = self.__repetition_count + repetition_count = self._repetition_count if isinstance(repetition_count, ParameterDeclaration): repetition_count = serializer.dictify(repetition_count) return dict( type=serializer.get_type_identifier(self), - body=serializer.dictify(self.__body), + body=serializer.dictify(self.body), repetition_count=repetition_count, - atomicity=self.atomicity + atomicity=self.atomicity, ) @staticmethod diff --git a/tests/pulses/loop_pulse_template_tests.py b/tests/pulses/loop_pulse_template_tests.py index 4398a2fd6..a0171155e 100644 --- a/tests/pulses/loop_pulse_template_tests.py +++ b/tests/pulses/loop_pulse_template_tests.py @@ -1,16 +1,96 @@ import unittest -from qctoolkit.pulses.loop_pulse_template import LoopPulseTemplate, ConditionMissingException +from sympy import sympify + +from qctoolkit.pulses.loop_pulse_template import ForLoopPulseTemplate, WhileLoopPulseTemplate,\ + ConditionMissingException, ParametrizedRange, LoopIndexNotUsedException +from qctoolkit.pulses.parameters import ConstantParameter from tests.pulses.sequencing_dummies import DummyCondition, DummyPulseTemplate, DummySequencer, DummyInstructionBlock from tests.serialization_dummies import DummySerializer -class LoopPulseTemplateTest(unittest.TestCase): + +class ParametrizedRangeTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_init(self): + self.assertEqual(ParametrizedRange(7).to_tuple(), + (0, 7, 1)) + self.assertEqual(ParametrizedRange(4, 7).to_tuple(), + (4, 7, 1)) + self.assertEqual(ParametrizedRange(4, 'h', 5).to_tuple(), + (4, 'h', 5)) + + self.assertEqual(ParametrizedRange(start=7, stop=1, step=-1).to_tuple(), + (7, 1, -1)) + + with self.assertRaises(TypeError): + ParametrizedRange() + with self.assertRaises(TypeError): + ParametrizedRange(1, 2, 3, 4) + + def test_to_range(self): + pr = ParametrizedRange(4, 'l*k', 'k') + + self.assertEqual(pr.to_range({'l': 5, 'k': 2}), + range(4, 10, 2)) + + def test_parameter_names(self): + self.assertEqual(ParametrizedRange(5).parameter_names, set()) + self.assertEqual(ParametrizedRange('g').parameter_names, {'g'}) + self.assertEqual(ParametrizedRange('g*h', 'h', 'l/m').parameter_names, {'g', 'h', 'l', 'm'}) + + +class ForLoopPulseTemplateTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_init(self): + dt = DummyPulseTemplate(parameter_names={'i', 'k'}) + self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=5).loop_range.to_tuple(), + (0, 5, 1)) + self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range='s').loop_range.to_tuple(), + (0, 's', 1)) + self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=(2, 5)).loop_range.to_tuple(), + (2, 5, 1)) + self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=range(1, 2, 5)).loop_range.to_tuple(), + (1, 2, 5)) + self.assertEqual(ForLoopPulseTemplate(body=dt, loop_index='i', + loop_range=ParametrizedRange('a', 'b', 'c')).loop_range.to_tuple(), + ('a', 'b', 'c')) + + with self.assertRaises(ValueError): + ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=slice(None)) + + with self.assertRaises(LoopIndexNotUsedException): + ForLoopPulseTemplate(body=DummyPulseTemplate(), loop_index='i', loop_range=1) + + def test_body_parameter_generator(self): + dt = DummyPulseTemplate(parameter_names={'i', 'k'}) + flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c')) + + expected_range = range(2, 17, 3) + + outer_params = dict(k=ConstantParameter(5), + a=ConstantParameter(expected_range.start), + b=ConstantParameter(expected_range.stop), + c=ConstantParameter(expected_range.step)) + forward_parameter_dicts = list(flt._body_parameter_generator(outer_params, forward=True)) + backward_parameter_dicts = list(flt._body_parameter_generator(outer_params, forward=False)) + + self.assertEqual(forward_parameter_dicts, list(reversed(backward_parameter_dicts))) + for local_params, i in zip(forward_parameter_dicts, expected_range): + expected_local_params = dict(k=ConstantParameter(5), i=ConstantParameter(i)) + self.assertEqual(expected_local_params, local_params) + + +class WhileLoopPulseTemplateTest(unittest.TestCase): def test_parameter_names_and_declarations(self) -> None: condition = DummyCondition() body = DummyPulseTemplate() - t = LoopPulseTemplate(condition, body) + t = WhileLoopPulseTemplate(condition, body) self.assertEqual(body.parameter_names, t.parameter_names) self.assertEqual(body.parameter_declarations, t.parameter_declarations) @@ -18,10 +98,11 @@ def test_parameter_names_and_declarations(self) -> None: self.assertEqual(body.parameter_names, t.parameter_names) self.assertEqual(body.parameter_declarations, t.parameter_declarations) + @unittest.skip def test_is_interruptable(self) -> None: condition = DummyCondition() body = DummyPulseTemplate(is_interruptable=False) - t = LoopPulseTemplate(condition, body) + t = WhileLoopPulseTemplate(condition, body) self.assertFalse(t.is_interruptable) body.is_interruptable_ = True @@ -30,7 +111,7 @@ def test_is_interruptable(self) -> None: def test_str(self) -> None: condition = DummyCondition() body = DummyPulseTemplate() - t = LoopPulseTemplate(condition, body) + t = WhileLoopPulseTemplate(condition, body) self.assertIsInstance(str(t), str) @@ -40,7 +121,7 @@ def test_requires_stop(self) -> None: condition = DummyCondition(requires_stop=False) conditions = {'foo_cond': condition} body = DummyPulseTemplate(requires_stop=False) - t = LoopPulseTemplate('foo_cond', body) + t = WhileLoopPulseTemplate('foo_cond', body) self.assertFalse(t.requires_stop({}, conditions)) condition.requires_stop_ = True @@ -53,7 +134,7 @@ def test_requires_stop(self) -> None: def test_build_sequence(self) -> None: condition = DummyCondition() body = DummyPulseTemplate() - t = LoopPulseTemplate('foo_cond', body) + t = WhileLoopPulseTemplate('foo_cond', body) sequencer = DummySequencer() block = DummyInstructionBlock() parameters = {} @@ -77,7 +158,7 @@ def test_build_sequence(self) -> None: def test_condition_missing(self) -> None: body = DummyPulseTemplate(requires_stop=False) - t = LoopPulseTemplate('foo_cond', body) + t = WhileLoopPulseTemplate('foo_cond', body) sequencer = DummySequencer() block = DummyInstructionBlock() with self.assertRaises(ConditionMissingException): @@ -91,12 +172,13 @@ def test_get_serialization_data(self) -> None: body = DummyPulseTemplate() condition_name = 'foo_cond' identifier = 'foo_loop' - t = LoopPulseTemplate(condition_name, body, identifier=identifier) + t = WhileLoopPulseTemplate(condition_name, body, identifier=identifier) serializer = DummySerializer() expected_data = dict(type=serializer.get_type_identifier(t), body=str(id(body)), - condition=condition_name) + condition=condition_name, + atomicity=False) data = t.get_serialization_data(serializer) self.assertEqual(expected_data, data) @@ -105,7 +187,8 @@ def test_deserialize(self) -> None: data = dict( identifier='foo_loop', condition='foo_cond', - body='bodyDummyPulse' + body='bodyDummyPulse', + atomicity=False ) # prepare dependencies for deserialization @@ -113,7 +196,7 @@ def test_deserialize(self) -> None: serializer.subelements[data['body']] = DummyPulseTemplate() # deserialize - result = LoopPulseTemplate.deserialize(serializer, **data) + result = WhileLoopPulseTemplate.deserialize(serializer, **data) # compare self.assertIs(serializer.subelements[data['body']], result.body) diff --git a/tests/pulses/repetition_pulse_template_tests.py b/tests/pulses/repetition_pulse_template_tests.py index fc5dde550..6e67438c0 100644 --- a/tests/pulses/repetition_pulse_template_tests.py +++ b/tests/pulses/repetition_pulse_template_tests.py @@ -64,6 +64,7 @@ def test_parameter_names_and_declarations(self) -> None: self.assertEqual(body.parameter_names, t.parameter_names) self.assertEqual(body.parameter_declarations, t.parameter_declarations) + @unittest.skip('is interruptable not implemented for loops') def test_is_interruptable(self) -> None: body = DummyPulseTemplate(is_interruptable=False) t = RepetitionPulseTemplate(body, 6) From 861c0600b1ab37ba7b97cb1b94f37898694a584f Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 2 Mar 2017 17:16:55 +0100 Subject: [PATCH 022/116] ParameterDict to cast constants and expressions automatically --- qctoolkit/pulses/parameters.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index e35e7082f..521266cda 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -16,10 +16,33 @@ from qctoolkit.expressions import Expression from qctoolkit.comparable import Comparable -__all__ = ["Parameter", "ParameterDeclaration", "ConstantParameter", +__all__ = ["make_parameter", "ParameterDict", "Parameter", "ParameterDeclaration", "ConstantParameter", "ParameterNotProvidedException", "ParameterValueIllegalException"] +def make_parameter(value): + """Convenience function """ + if isinstance(value, Parameter): + return value + if isinstance(value, Number): + return ConstantParameter(value) + if isinstance(value, str): + return MappedParameter(Expression(value)) + raise TypeError('Can not convert object of type {} to a parameter'.format(type(value))) + + +class ParameterDict(dict): + """Conve""" + def __init__(self, *args, **kwargs): + super().__init__( + *((k, make_parameter(v)) for k, v in args), + **dict((k, make_parameter(v)) for k, v in kwargs.items()) + ) + + def __setitem__(self, key, value) -> None: + super().__setitem__(key, make_parameter(value)) + + class Parameter(Serializable, Comparable, metaclass=ABCMeta): """A parameter for pulses. From 905bfa538422e536bcbf4939d17e5156012883cb Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 2 Mar 2017 17:17:19 +0100 Subject: [PATCH 023/116] Check that parameter declarations have a valid name --- qctoolkit/pulses/parameters.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index 521266cda..f75d5e918 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -191,7 +191,7 @@ def __init__(self, name: str, """Creates a ParameterDeclaration object. Args: - name (str): A name for the declared parameter. + name (str): A name for the declared parameter. The name must me a valid variable name. min (float or ParameterDeclaration): An optional real number or ParameterDeclaration object specifying the minimum value allowed. (default: -inf) max (float or ParameterDeclaration): An optional real number or @@ -200,6 +200,9 @@ def __init__(self, name: str, pulse template parameter. """ super().__init__(None) + if not name.isidentifier(): + raise InvalidParameterNameException(name) + self.__name = name self.__min_value = float('-inf') self.__max_value = float('+inf') @@ -506,3 +509,11 @@ def __str__(self) -> str: return "The value {0} provided for parameter {1} is illegal (min = {2}, max = {3})".format( self.parameter_value, self.parameter_declaration.name, self.parameter_declaration.min_value, self.parameter_declaration.max_value) + + +class InvalidParameterNameException(Exception): + def __init__(self, parameter_name: str): + self.parameter_name = parameter_name + + def __str__(self): + return '{} is an invalid parameter name'.format(self.parameter_name) From 9f6425f2705409337ab84e2e5dfe222e948aba74 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 2 Mar 2017 17:36:19 +0100 Subject: [PATCH 024/116] Make program tests more consistent --- qctoolkit/hardware/program.py | 2 +- tests/hardware/program_tests.py | 197 ++++++++++++++++---------------- 2 files changed, 100 insertions(+), 99 deletions(-) diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 4bb06ffff..30b33b56d 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -18,7 +18,7 @@ class Loop(Comparable, Node): def __init__(self, parent: Union['Loop', None]=None, children: Iterable['Loop']=list(), - waveform: Union[EXECInstruction, Waveform]=None, + waveform: Union[Waveform]=None, repetition_count=1): super().__init__(parent=parent, children=children) diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py index 199edddd6..211e44191 100644 --- a/tests/hardware/program_tests.py +++ b/tests/hardware/program_tests.py @@ -2,7 +2,9 @@ import itertools from copy import deepcopy -from string import Formatter +import numpy as np + +from string import Formatter, ascii_uppercase from qctoolkit.hardware.program import Loop, MultiChannelProgram from qctoolkit.pulses.instructions import REPJInstruction, InstructionBlock, ImmutableInstructionBlock @@ -10,53 +12,40 @@ from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -class LoopTests(unittest.TestCase): - def __init__(self, *args, waveform_data_generator=itertools.repeat(None), waveform_duration=None, num_channels=2, **kwargs): - super().__init__(*args, **kwargs) +class WaveformGenerator: + def __init__(self, num_channels, + duration_generator=itertools.repeat(None), + waveform_data_generator=itertools.repeat(None), channel_names=ascii_uppercase): + self.num_channels = num_channels + self.duration_generator = duration_generator + self.waveform_data_generator = waveform_data_generator + self.channel_names = channel_names[:num_channels] - names = 'ABCDEFGH'[:num_channels] + def generate_single_channel_waveform(self, channel): + return DummyWaveform(sample_output=next(self.waveform_data_generator), + duration=next(self.duration_generator), + defined_channels={channel}) - def generate_waveform(chan): - return DummyWaveform(sample_output=next(waveform_data_generator), - duration=waveform_duration, - defined_channels={chan}) + def generate_multi_channel_waveform(self): + return MultiChannelWaveform([self.generate_single_channel_waveform(self.channel_names[ch_i]) + for ch_i in range(self.num_channels)]) - def generate_multi_channel_waveform(): - return MultiChannelWaveform([generate_waveform(names[ch_i]) for ch_i in range(num_channels)]) + def __call__(self): + return self.generate_multi_channel_waveform() - self.old_description = \ -"""\ -LOOP 1 times: - ->EXEC 1 times - ->LOOP 10 times: - ->LOOP 5 times: - ->EXEC 1 times - ->LOOP 17 times: - ->LOOP 2 times: - ->EXEC 1 times - ->EXEC 1 times - ->EXEC 1 times - ->LOOP 3 times: - ->EXEC 1 times - ->EXEC 1 times - ->LOOP 4 times: - ->LOOP 6 times: - ->LOOP 7 times: - ->EXEC 1 times - ->LOOP 8 times: - ->EXEC 1 times - ->LOOP 9 times: - ->LOOP 10 times: - ->EXEC 1 times - ->LOOP 11 times: - ->EXEC 1 times""" +class LoopTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) - self.new_description = \ + self.maxDiff = None + + self.test_loop_repr = \ """\ LOOP 1 times: ->EXEC {} 1 times - ->EXEC {} 50 times + ->LOOP 10 times: + ->EXEC {} 50 times ->LOOP 17 times: ->LOOP 2 times: ->EXEC {} 1 times @@ -72,69 +61,71 @@ def generate_multi_channel_waveform(): ->LOOP 9 times: ->EXEC {} 10 times ->EXEC {} 11 times""" - self.root_block = InstructionBlock() - self.loop_block11 = InstructionBlock() - self.loop_block1 = InstructionBlock() - self.loop_block21 = InstructionBlock() - self.loop_block2 = InstructionBlock() - self.loop_block3 = InstructionBlock() - self.loop_block411 = InstructionBlock() - self.loop_block412 = InstructionBlock() - self.loop_block41 = InstructionBlock() - self.loop_block4 = InstructionBlock() - self.loop_block421 = InstructionBlock() - self.loop_block422 = InstructionBlock() - self.loop_block42 = InstructionBlock() - - self.root_block.add_instruction_exec(generate_multi_channel_waveform()) - - self.loop_block11.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(self.loop_block11)) - - self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block21.add_instruction_exec(generate_multi_channel_waveform()) - - self.loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(self.loop_block21)) - self.loop_block2.add_instruction_exec(generate_multi_channel_waveform()) - - self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block3.add_instruction_exec(generate_multi_channel_waveform()) - - self.loop_block411.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block412.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(self.loop_block411)) - self.loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(self.loop_block412)) - - self.loop_block421.add_instruction_exec(generate_multi_channel_waveform()) - self.loop_block422.add_instruction_exec(generate_multi_channel_waveform()) - - self.loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block421)) - self.loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(self.loop_block422)) + @staticmethod + def get_test_loop(waveform_generator=None): + if waveform_generator is None: + waveform_generator = lambda: None + + return Loop(repetition_count=1, children=[Loop(repetition_count=1, waveform=waveform_generator()), + Loop(repetition_count=10, children=[Loop(repetition_count=50, waveform=waveform_generator())]), + Loop(repetition_count=17, children=[Loop(repetition_count=2, children=[Loop(repetition_count=1, waveform=waveform_generator()), + Loop(repetition_count=1, waveform=waveform_generator())]), + Loop(repetition_count=1, waveform=waveform_generator())]), + Loop(repetition_count=3, children=[Loop(repetition_count=1, waveform=waveform_generator()), + Loop(repetition_count=1, waveform=waveform_generator())]), + Loop(repetition_count=4, children=[Loop(repetition_count=6, children=[Loop(repetition_count=7, waveform=waveform_generator()), + Loop(repetition_count=8, waveform=waveform_generator())]), + Loop(repetition_count=9, children=[Loop(repetition_count=10, waveform=waveform_generator()), + Loop(repetition_count=11, waveform=waveform_generator())])])]) + + def test_compare_key(self): + wf_gen = WaveformGenerator(num_channels=1) + + wf_1 = wf_gen() + wf_2 = wf_gen() + + tree1 = Loop(children=[Loop(waveform=wf_1, repetition_count=5)]) + tree2 = Loop(children=[Loop(waveform=wf_1, repetition_count=4)]) + tree3 = Loop(children=[Loop(waveform=wf_2, repetition_count=5)]) + tree4 = Loop(children=[Loop(waveform=wf_1, repetition_count=5)]) + + self.assertNotEqual(tree1, tree2) + self.assertNotEqual(tree1, tree3) + self.assertNotEqual(tree2, tree3) + self.assertEqual(tree1, tree4) + + tree1 = Loop(children=[Loop(waveform=wf_1, repetition_count=5), + Loop(waveform=wf_2, repetition_count=7)], repetition_count=2) + tree2 = Loop(children=[Loop(waveform=wf_1, repetition_count=5), + Loop(waveform=wf_2, repetition_count=5)], repetition_count=2) + tree3 = Loop(children=[Loop(waveform=wf_1, repetition_count=5), + Loop(waveform=wf_1, repetition_count=7)], repetition_count=2) + tree4 = Loop(children=[Loop(waveform=wf_1, repetition_count=5), + Loop(waveform=wf_2, repetition_count=7)], repetition_count=3) + tree5 = Loop(children=[Loop(waveform=wf_1, repetition_count=5), + Loop(waveform=wf_2, repetition_count=7)], repetition_count=2) + self.assertNotEqual(tree1, tree2) + self.assertNotEqual(tree1, tree3) + self.assertNotEqual(tree1, tree4) + self.assertEqual(tree1, tree5) - self.loop_block4.add_instruction_repj(6, ImmutableInstructionBlock(self.loop_block41)) - self.loop_block4.add_instruction_repj(9, ImmutableInstructionBlock(self.loop_block42)) - - self.root_block.add_instruction_repj(10, ImmutableInstructionBlock(self.loop_block1)) - self.root_block.add_instruction_repj(17, ImmutableInstructionBlock(self.loop_block2)) - self.root_block.add_instruction_repj(3, ImmutableInstructionBlock(self.loop_block3)) - self.root_block.add_instruction_repj(4, ImmutableInstructionBlock(self.loop_block4)) + def test_repr(self): + wf_gen = WaveformGenerator(num_channels=1) + wfs = [wf_gen() for _ in range(11)] - self.maxDiff = None + expected = self.test_loop_repr.format(*wfs) - def get_root_loop(self): - program = MultiChannelProgram(self.root_block, {'A', 'B'}) - return program[{'A', 'B'}] + tree = self.get_test_loop() + for loop in tree.get_depth_first_iterator(): + if loop.is_leaf(): + loop.waveform = wfs.pop(0) + self.assertEqual(len(wfs), 0) - def test_repr(self): - root_loop = self.get_root_loop() - repres = root_loop.__repr__() - expected = self.new_description.format(*(loop.waveform - for loop in root_loop.get_depth_first_iterator() if loop.is_leaf())) - self.assertEqual(repres, expected) + self.assertEqual(repr(tree), expected) def test_is_leaf(self): - root_loop = self.get_root_loop() + root_loop = self.get_test_loop(waveform_generator=WaveformGenerator(1)) for loop in root_loop.get_depth_first_iterator(): self.assertTrue(bool(loop.is_leaf()) != bool(loop.waveform is None)) @@ -143,7 +134,7 @@ def test_is_leaf(self): self.assertTrue(bool(loop.is_leaf()) != bool(loop.waveform is None)) def test_depth(self): - root_loop = self.get_root_loop() + root_loop = self.get_test_loop() self.assertEqual(root_loop.depth(), 3) self.assertEqual(root_loop[-1].depth(), 2) self.assertEqual(root_loop[-1][-1].depth(), 1) @@ -152,7 +143,7 @@ def test_depth(self): root_loop[-1][-1][-1][-1].depth() def test_is_balanced(self): - root_loop = self.get_root_loop() + root_loop = self.get_test_loop() self.assertFalse(root_loop.is_balanced()) self.assertFalse(root_loop[2].is_balanced()) @@ -260,10 +251,20 @@ def generate_multi_channel_waveform(): self.maxDiff = None - def get_root_loop(self, channels): + def get_mcp(self, channels): program = MultiChannelProgram(self.root_block, ['A', 'B']) return program[channels] + def test_init(self): + with self.assertRaises(ValueError): + MultiChannelProgram(InstructionBlock()) + + mcp = MultiChannelProgram(self.root_block, ['A', 'B']) + self.assertEqual(mcp.channels, {'A', 'B'}) + + with self.assertRaises(KeyError): + mcp['C'] + def test_via_repr(self): root_loopA = self.get_root_loop('A') root_loopB = self.get_root_loop('B') From b33434d744d71d8e0a8949d053663c3ca54f3541 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 2 Mar 2017 19:04:36 +0100 Subject: [PATCH 025/116] Add a tiny bit missing documentation --- qctoolkit/pulses/sequencing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index 488996f01..988bdff23 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -133,6 +133,8 @@ def push(self, conditions (Dict(str -> Condition)): A mapping of condition identifier defined by the SequencingElement to Condition objects. Optional, if no conditions are defined by the SequencingElement. (default: None) + window_mapping (Dict(str -> str)): Mapping of the measurement window names of the sequence element + channel_mapping (Dict(ChannelID -> ChannelID)): Mapping of the defined channels target_block (InstructionBlock): The instruction block into which instructions resulting from the translation of the SequencingElement will be placed. Optional. If not provided, the main instruction block will be targeted. (default: None) From 6996d10d6a9f67685029ea475b18b5a2c040fb88 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Fri, 3 Mar 2017 16:23:38 +0100 Subject: [PATCH 026/116] Annotation fixes --- qctoolkit/expressions.py | 4 ++-- qctoolkit/pulses/loop_pulse_template.py | 16 ++++++++-------- qctoolkit/pulses/parameters.py | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index 1753f68d1..01cb4de00 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -35,7 +35,7 @@ def __init__(self, ex: str) -> None: def __str__(self) -> str: return self.__string - def get_most_simple_representation(self): + def get_most_simple_representation(self) -> Union[str, int, float, complex]: if self.__expression.free_symbols: return str(self.__expression) elif self.__expression.is_integer: @@ -48,7 +48,7 @@ def get_most_simple_representation(self): return str(self.__expression) @property - def compare_key(self) -> str: + def compare_key(self) -> sympy.Expr: return self.__expression def variables(self) -> Iterable[str]: diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index 1ee6b58d2..bcbbe553c 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -2,7 +2,7 @@ another PulseTemplate based on a condition.""" -from typing import Dict, Set, Optional, Any, Union, Tuple, NamedTuple +from typing import Dict, Set, Optional, Any, Union, Tuple, Generator from qctoolkit.serialization import Serializer @@ -76,13 +76,13 @@ def __init__(self, *args, **kwargs): self.stop = Expression(stop) self.step = Expression(step) - def to_tuple(self): + def to_tuple(self) -> Tuple[Any, Any, Any]: """Return a simple representation of the range which is useful for comparison and serialization""" return (self.start.get_most_simple_representation(), self.stop.get_most_simple_representation(), self.step.get_most_simple_representation()) - def to_range(self, parameters: Dict[str, Any]): + def to_range(self, parameters: Dict[str, Any]) -> range: return range(self.start.evaluate_numeric(**parameters), self.stop.evaluate_numeric(**parameters), self.step.evaluate_numeric(**parameters)) @@ -140,10 +140,10 @@ def parameter_names(self) -> Set[str]: return parameter_names | self._loop_range.parameter_names @property - def parameter_declarations(self): + def parameter_declarations(self) -> Set['ParameterDeclaration']: raise NotImplementedError() - def _body_parameter_generator(self, parameters: Dict[str, Parameter], forward=True): + def _body_parameter_generator(self, parameters: Dict[str, Parameter], forward=True) -> Generator: loop_range_parameters = dict((parameter_name, parameters[parameter_name].get_value()) for parameter_name in self._loop_range.parameter_names) loop_range = self._loop_range.to_range(loop_range_parameters) @@ -185,7 +185,7 @@ def build_waveform(self, parameters: Dict[str, Parameter]) -> ForLoopWaveform: def requires_stop(self, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition']): + conditions: Dict[str, 'Condition']) -> bool: if any(parameters[parameter_name].requires_stop() for parameter_name in self._loop_range.parameter_names): return True if self.atomicity: @@ -208,7 +208,7 @@ def deserialize(serializer: Serializer, body: Dict[str, Any], loop_range: Tuple, loop_index: str, - identifier: Optional[str]=None) -> 'WhileLoopPulseTemplate': + identifier: Optional[str]=None) -> 'ForLoopPulseTemplate': body = serializer.deserialize(body) return ForLoopPulseTemplate(body=body, identifier=identifier, @@ -261,7 +261,7 @@ def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Conditi def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]): + channel_mapping: Dict[ChannelID, ChannelID]) -> None: raise NotImplementedError() def build_sequence(self, diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index f75d5e918..ce7b39e04 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -515,5 +515,5 @@ class InvalidParameterNameException(Exception): def __init__(self, parameter_name: str): self.parameter_name = parameter_name - def __str__(self): + def __str__(self) -> str: return '{} is an invalid parameter name'.format(self.parameter_name) From bc6103e924cd077344e915a9b6b9ecfc7235b0ab Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Fri, 3 Mar 2017 16:24:24 +0100 Subject: [PATCH 027/116] Make program and tabor tests pass. Tests have low coverage of features --- qctoolkit/hardware/awgs/tabor.py | 80 ++++++++++++++++++++------------ tests/hardware/program_tests.py | 4 +- tests/hardware/tabor_tests.py | 49 ++++++++++--------- 3 files changed, 79 insertions(+), 54 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 2fe091137..028626868 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -147,26 +147,59 @@ def setup_advanced_sequence_mode(self) -> None: if entry.depth() == depth_to_unroll: entry.unroll() + min_seq_len = self.__device_properties['min_seq_len'] + max_seq_len = self.__device_properties['max_seq_len'] + + def check_merge_with_next(program, n): + if (program[n].repetition_count == 1 and program[n+1].repetition_count == 1 and + len(program[n]) + len(program[n+1]) < max_seq_len): + program[n][len(program[n]):] = program[n + 1][:] + program[n + 1:n + 2] = [] + return True + return False + + def check_partial_unroll(program, n): + st = program[n] + if sum(entry.repetition_count for entry in st) * st.repetition_count >= min_seq_len: + if sum(entry.repetition_count for entry in st) < min_seq_len: + st.unroll_children() + while len(st) < min_seq_len: + st.split_one_child() + return True + return False + i = 0 while i < len(self.program): - sequence_table = self.program[i] - if len(sequence_table) > self.__device_properties['max_seq_len']: - raise TaborException() - elif len(sequence_table) < self.__device_properties['min_seq_len']: - # try to merge with neighbours - if sequence_table.repetition_count == 1: - if i > 0 and self.program[i-1].repetition_count == 1: - self.program[i-1][len(self.program[i-1]):] = sequence_table[:] - self.program[i:i+1] = [] - elif i+1 < len(self.program) and self.program[i+1].repetition_count == 1: - self.program[i+1][:0] = sequence_table[:] - self.program[i:i+1] = [] - else: - self.increase_sequence_table_length(sequence_table, self.__device_properties) + self.program[i].assert_tree_integrity() + if len(self.program[i]) > max_seq_len: + raise TaborException('The algorithm is not smart enough to make sequence tables shorter') + elif len(self.program[i]) < min_seq_len: + if self.program[i].repetition_count == 0: + raise TaborException('Invalid repetition count') + elif self.program[i].repetition_count == 1: + # check if merging with neighbour is possible + if i > 0 and check_merge_with_next(self.program, i-1): + pass + elif i+1 < len(self.program) and check_merge_with_next(self.program, i): + pass + + # check if (partial) unrolling is possible + elif check_partial_unroll(self.program, i): i += 1 - else: - self.increase_sequence_table_length(sequence_table, self.__device_properties) + + elif i > 0 and len(self.program[i]) + len(self.program[i-1]) < max_seq_len: + self.program[i][:0] = self.program[i-1].copy_tree_structure()[:] + self.program[i - 1].repetition_count -= 1 + elif i+1 < len(self.program) and len(self.program[i]) + len(self.program[i+1]) < max_seq_len: + self.program[i][len(self.program[i]):] = self.program[i+1].copy_tree_structure()[:] + self.program[i+1].repetition_count -= 1 + + else: + raise TaborException('The algorithm is not smart enough to make this sequence table longer') + elif check_partial_unroll(self.program, i): i += 1 + else: + raise TaborException('The algorithm is not smart enough to make this sequence table longer') else: i += 1 @@ -177,7 +210,7 @@ def setup_advanced_sequence_mode(self) -> None: raise TaborException() for sequence_table in self.program: if len(sequence_table) < self.__device_properties['min_seq_len']: - raise TaborException() + raise TaborException('Sequence table is too short') if len(sequence_table) > self.__device_properties['max_seq_len']: raise TaborException() @@ -215,19 +248,6 @@ def program(self) -> Loop: def get_sequencer_tables(self) -> List[Tuple[int, int, int]]: return self.__sequencer_tables - @staticmethod - def increase_sequence_table_length(sequence_table: Loop, device_properties) -> None: - assert(sequence_table.depth() == 1) - if len(sequence_table) < device_properties['min_seq_len']: - - if sum(entry.repetition_count for entry in sequence_table)*sequence_table.repetition_count >= device_properties['min_seq_len']: - if sum(entry.repetition_count for entry in sequence_table) < device_properties['min_seq_len']: - sequence_table.unroll_children() - while len(sequence_table) < device_properties['min_seq_len']: - sequence_table.split_one_child() - else: - TaborException('Sequence table too short: ', sequence_table) - def get_advanced_sequencer_table(self) -> List[Tuple[int, int, int]]: """Advanced sequencer table that can be used via the download_adv_seq_table pytabor command""" return self.__advanced_sequencer_table diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py index 211e44191..0e680b7c2 100644 --- a/tests/hardware/program_tests.py +++ b/tests/hardware/program_tests.py @@ -266,8 +266,8 @@ def test_init(self): mcp['C'] def test_via_repr(self): - root_loopA = self.get_root_loop('A') - root_loopB = self.get_root_loop('B') + root_loopA = self.get_mcp('A') + root_loopB = self.get_mcp('B') waveformsA = tuple(loop.waveform for loop in root_loopA.get_depth_first_iterator() if loop.is_leaf()) reprA = self.descriptionA.format(*waveformsA) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 9306ac072..f97a56e4f 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -1,6 +1,7 @@ import unittest from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair from qctoolkit.hardware.program import MultiChannelProgram +from qctoolkit.pulses.instructions import InstructionBlock import numbers import itertools import numpy as np @@ -8,7 +9,7 @@ from teawg import model_properties_dict from qctoolkit.hardware.util import voltage_to_uint16 -from .program_tests import LoopTests +from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests try: @@ -35,18 +36,16 @@ def waveform_data_generator(self): -0.5*np.cos(np.linspace(0, 2*np.pi, num=4048))]) @property - def root_block(self): - return LoopTests(waveform_data_generator=self.waveform_data_generator, waveform_duration=4048e-9).root_block - - @property - def working_root_block(self): - block = self.root_block + def root_loop(self): + return LoopTests.get_test_loop(WaveformGenerator(num_channels=2, + waveform_data_generator=self.waveform_data_generator, + duration_generator=itertools.repeat(4048e-9))) def test_init(self): - prog = MultiChannelProgram(self.root_block) - TaborProgram(prog, self.instr_props, ('A', 'B'), (None, None)) - with self.assertRaises(Exception): - TaborProgram(prog, self.instr_props, ('A',), (None, None)) + prog = MultiChannelProgram(MultiChannelTests().root_block) + TaborProgram(prog, self.instr_props, ('A', None), (None, None)) + with self.assertRaises(TaborException): + TaborProgram(prog, self.instr_props, ('A', 'B'), (None, None)) @unittest.skip def test_setup_single_waveform_mode(self): @@ -65,17 +64,23 @@ def my_gen(gen): sample_rate = 8096 with self.assertRaises(TaborException): - TaborProgram(MultiChannelProgram( - LoopTests(waveform_data_generator=my_gen(self.waveform_data_generator), - waveform_duration=1e-9, - num_channels=4).root_block - ), self.instr_props, ('A', 'B'), (None, None)).sampled_segments(8000, (1., 1.), (0, 0)) - - mcp = MultiChannelProgram( - LoopTests(waveform_data_generator=my_gen(self.waveform_data_generator), - waveform_duration=0.5, - num_channels=4).root_block - ) + root_loop = LoopTests.get_test_loop(WaveformGenerator( + waveform_data_generator=my_gen(self.waveform_data_generator), + duration_generator=itertools.repeat(1e-9), + num_channels=4)) + + mcp = MultiChannelProgram(InstructionBlock(), tuple()) + mcp.programs[frozenset(('A', 'B', 'C', 'D'))] = root_loop + TaborProgram(mcp, self.instr_props, ('A', 'B'), (None, None)).sampled_segments(8000, (1., 1.), (0, 0)) + + root_loop = LoopTests.get_test_loop(WaveformGenerator( + waveform_data_generator=my_gen(self.waveform_data_generator), + duration_generator=itertools.repeat(0.5), + num_channels=4)) + + mcp = MultiChannelProgram(InstructionBlock(), tuple()) + mcp.programs[frozenset(('A', 'B', 'C', 'D'))] = root_loop + prog = TaborProgram(mcp, self.instr_props, ('A', 'B'), (None, None)) sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0)) From 127e4d8644bcab0021e61268a5e9929c29d20d32 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Fri, 3 Mar 2017 16:24:49 +0100 Subject: [PATCH 028/116] Add test for voltage->binary conversion --- tests/hardware/util_tests.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/hardware/util_tests.py diff --git a/tests/hardware/util_tests.py b/tests/hardware/util_tests.py new file mode 100644 index 000000000..9bc2bdf2d --- /dev/null +++ b/tests/hardware/util_tests.py @@ -0,0 +1,24 @@ +import unittest +import numpy as np + +from qctoolkit.hardware.util import voltage_to_uint16 + + +class VoltageToBinaryTests(unittest.TestCase): + + def test_voltage_to_uint16(self): + + with self.assertRaises(ValueError): + voltage_to_uint16(np.zeros(0), 0, 0, 0) + + linspace_voltage = np.linspace(0, 1, 128) + with self.assertRaises(ValueError): + voltage_to_uint16(linspace_voltage, 0.9, 0, 1) + + with self.assertRaises(ValueError): + voltage_to_uint16(linspace_voltage, 1.1, -1, 1) + + expected_data = np.arange(0, 128, dtype=np.uint16) + received_data = voltage_to_uint16(linspace_voltage, 0.5, 0.5, 7) + + self.assertTrue(np.all(expected_data == received_data)) From ed2f583670fe26de0c54a7acf038bae44f9ac350 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 15:15:07 +0100 Subject: [PATCH 029/116] Fix plotting --- qctoolkit/pulses/plotting.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index cedb87e36..ff39e3975 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -109,11 +109,13 @@ def plot(pulse: PulseTemplate, because a parameter value could not be evaluated all Exceptions possibly raised during sequencing """ + channels = pulse.defined_channels + if parameters is None: parameters = dict() plotter = Plotter(sample_rate=sample_rate) sequencer = Sequencer() - sequencer.push(pulse, parameters) + sequencer.push(pulse, parameters, channel_mapping={ch: ch for ch in channels}) sequence = sequencer.build() if not sequencer.has_finished(): raise PlottingNotPossibleException(pulse) @@ -122,13 +124,13 @@ def plot(pulse: PulseTemplate, # plot to figure figure = plt.figure() ax = figure.add_subplot(111) - for index, channel in enumerate(voltages): - ax.step(times, channel, where='post', label='channel {}'.format(index)) + for ch_name, voltage in voltages.items(): + ax.step(times, voltage, where='post', label='channel {}'.format(ch_name)) ax.legend() - max_voltage = max(max(channel) for channel in voltages) - min_voltage = min(min(channel) for channel in voltages) + max_voltage = max(max(channel) for channel in voltages.values()) + min_voltage = min(min(channel) for channel in voltages.values()) # add some margins in the presentation plt.plot() From 54288c28b7521a95ee47dcf63ffb563b98f845b6 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:04:10 +0100 Subject: [PATCH 030/116] FunctionPulseTemplate/FunctionWaveform update: - Add measurement window functionality - Fix bug in defined_channels - Substitute parameters on waveform creation - Add fix tests --- qctoolkit/pulses/function_pulse_template.py | 123 ++++++++++++------- qctoolkit/pulses/table_pulse_template.py | 4 +- tests/pulses/function_pulse_tests.py | 127 +++++++++++++++++--- tests/pulses/table_pulse_template_tests.py | 20 +++ 4 files changed, 212 insertions(+), 62 deletions(-) diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 38c8b95dc..3cf53169a 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -8,6 +8,8 @@ from typing import Any, Dict, List, Set, Optional, Union +import numbers +import itertools import numpy as np @@ -64,6 +66,7 @@ def __init__(self, self.__parameter_names = set(self.__duration_expression.variables() + self.__expression.variables()) - set(['t']) self.__channel = channel + self.__measurement_windows = dict() @property def parameter_names(self) -> Set[str]: @@ -73,30 +76,24 @@ def parameter_names(self) -> Set[str]: def parameter_declarations(self) -> Set[ParameterDeclaration]: return {ParameterDeclaration(param_name) for param_name in self.parameter_names} - @property - def measurement_names(self) -> Set[str]: - return set() - @property def is_interruptable(self) -> bool: return False @property def defined_channels(self) -> Set['ChannelID']: - return set(self.__channel) + return {self.__channel} def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> 'FunctionWaveform': - return FunctionWaveform( - channel=channel_mapping[self.__channel], - parameters={parameter_name: parameter.get_value() - for (parameter_name, parameter) in parameters.items()}, - expression=self.__expression, - duration_expression=self.__duration_expression, - measurement_windows=[] - ) + substitutions = dict((v, parameters[v].get_value()) for v in self.__expression.variables() if v != 't') + return FunctionWaveform(expression=self.__expression.evaluate_symbolic(substitutions=substitutions), + duration=self.__duration_expression.evaluate_numeric(**parameters), + measurement_windows=self.get_measurement_windows(parameters=parameters, + measurement_mapping=measurement_mapping), + channel=channel_mapping[self.__channel]) def requires_stop(self, parameters: Dict[str, Parameter], @@ -106,11 +103,12 @@ def requires_stop(self, for name in parameters.keys() if (name in self.parameter_names) ) - def get_serialization_data(self, serializer: Serializer) -> None: + def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: return dict( duration_expression=serializer.dictify(self.__duration_expression), expression=serializer.dictify(self.__expression), - channel=self.__channel + channel=self.__channel, + measurement_declarations=self.measurement_declarations ) @staticmethod @@ -118,61 +116,104 @@ def deserialize(serializer: 'Serializer', expression: str, duration_expression: str, channel: 'ChannelID', + measurement_declarations: Dict[str, List], identifier: Optional[bool]=None) -> 'FunctionPulseTemplate': - return FunctionPulseTemplate( + template = FunctionPulseTemplate( serializer.deserialize(expression), serializer.deserialize(duration_expression), channel=channel, identifier=identifier ) + for name, windows in measurement_declarations.items(): + for window in windows: + template.add_measurement_declaration(name, *window) + return template + + def get_measurement_windows(self, + parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: + def get_val(v): + return v if not isinstance(v, Expression) else v.evaluate_numeric( + **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] + for name_ in v.variables()}) + + resulting_windows = [] + for name, windows in self.__measurement_windows.items(): + for begin, end in windows: + resulting_windows.append((measurement_mapping[name], get_val(begin), get_val(end))) + return resulting_windows + + @property + def measurement_declarations(self): + """ + :return: Measurement declarations as added by the add_measurement_declaration method + """ + + return {name: [(begin.get_most_simple_representation(), + end.get_most_simple_representation()) + for begin, end in windows] + for name, windows in self.__measurement_windows.items() } + + @property + def measurement_names(self) -> Set[str]: + """ + :return: + """ + return set(self.__measurement_windows.keys()) + + def add_measurement_declaration(self, name: str, begin: Union[float, str], end: Union[float, str]) -> None: + begin = Expression(begin) + end = Expression(end) + new_parameters = set(itertools.chain(begin.variables(), end.variables())) + + if 't' in new_parameters: + raise ValueError('t is not an allowed measurement window parameter in function templates') + self.__parameter_names |= new_parameters + if name in self.__measurement_windows: + self.__measurement_windows[name].append((begin, end)) + else: + self.__measurement_windows[name] = [(begin, end)] class FunctionWaveform(Waveform): """Waveform obtained from instantiating a FunctionPulseTemplate.""" - def __init__(self, - parameters: Dict[str, float], - expression: Expression, - duration_expression: Expression, + def __init__(self, expression: Expression, + duration: float, measurement_windows: List[MeasurementWindow], - channel: ChannelID - ) -> None: + channel: ChannelID) -> None: """Creates a new FunctionWaveform instance. Args: - parameters (Dict(str -> float)): A mapping of parameter names to parameter values. expression (Expression): The function represented by this FunctionWaveform - as a mathematical expression where 't' denotes the time variable and other variables - are filled with values from the parameters mapping. - duration_expression (Expression): A mathematical expression which reliably + as a mathematical expression where 't' denotes the time variable. It may not have other variables + duration (float): A mathematical expression which reliably computes the duration of this FunctionPulseTemplate. + measurement_windows (List): A list of measurement windows """ super().__init__() - self.__expression = expression - self.__parameters = parameters - self.__duration = duration_expression.evaluate_numeric(**self.__parameters) - self.__channel_id = channel - self.__measurement_windows = measurement_windows + if set(expression.variables()) - set('t'): + raise ValueError('FunctionWaveforms may not depend on anything but "t"') + + self._expression = expression + self._duration = duration + self._channel_id = channel + self._measurement_windows = measurement_windows @property def defined_channels(self) -> Set[ChannelID]: - return {self.__channel_id} + return {self._channel_id} def get_measurement_windows(self) -> List[MeasurementWindow]: - return self.__measurement_windows - - def __evaluate(self, t) -> float: - params = self.__parameters.copy() - params.update({"t": t}) - return self.__expression.evaluate_numeric(**params) + return self._measurement_windows @property def compare_key(self) -> Any: - return self.__channel_id, self.__expression, self.__duration, self.__parameters + return self._channel_id, self._expression, self._duration, self._measurement_windows @property def duration(self) -> float: - return self.__duration + return self._duration def unsafe_sample(self, channel: ChannelID, @@ -180,7 +221,7 @@ def unsafe_sample(self, output_array: Union[np.ndarray, None] = None) -> np.ndarray: if output_array is None: output_array = np.empty(len(sample_times)) - output_array[:] = self.__evaluate(sample_times) + output_array[:] = self._expression.evaluate_numeric(t=sample_times) return output_array def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> Waveform: diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 1deffc2b7..d17c94530 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -423,7 +423,7 @@ def measurement_declarations(self): as_builtin = lambda x: str(x) if isinstance(x, Expression) else x return {name: [(as_builtin(begin), as_builtin(end)) for begin, end in windows] - for name, windows in self.__measurement_windows.items() } + for name, windows in self.__measurement_windows.items()} @property def measurement_names(self) -> Set[str]: @@ -620,6 +620,6 @@ def deserialize(serializer: Serializer, for name, windows in measurement_declarations.items(): for window in windows: - template.add_measurement_declaration(name,*window) + template.add_measurement_declaration(name, *window) return template diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index a6472b2e7..e917dc1ed 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -1,4 +1,5 @@ import unittest +import sympy from qctoolkit.pulses.function_pulse_template import FunctionPulseTemplate,\ FunctionWaveform @@ -17,7 +18,14 @@ def setUp(self) -> None: self.maxDiff = None self.s = 'a + b * t' self.s2 = 'c' + + self.meas_list = [('mw', 1, 1), ('mw', 'x', 'z'), ('drup', 'j', 'u')] + self.meas_dict = {'mw': [(1, 1), ('x', 'z')], 'drup': [('j', 'u')]} + self.fpt = FunctionPulseTemplate(self.s, self.s2,channel='A') + for mw in self.meas_list: + self.fpt.add_measurement_declaration(*mw) + self.pars = dict(a=DummyParameter(1), b=DummyParameter(2), c=DummyParameter(136.78)) def test_is_interruptable(self) -> None: @@ -42,7 +50,8 @@ def test_parameter_names_and_declarations_string_input(self) -> None: def test_serialization_data(self) -> None: expected_data = dict(duration_expression=str(self.s2), expression=str(self.s), - channel='A') + channel='A', + measurement_declarations=self.meas_dict) self.assertEqual(expected_data, self.fpt.get_serialization_data( DummySerializer(serialize_callback=lambda x: str(x)))) @@ -50,15 +59,18 @@ def test_deserialize(self) -> None: basic_data = dict(duration_expression=str(self.s2), expression=str(self.s), channel='A', - identifier='hugo') + identifier='hugo', + measurement_declarations=self.meas_dict) serializer = DummySerializer(serialize_callback=lambda x: str(x)) serializer.subelements[str(self.s2)] = Expression(self.s2) serializer.subelements[str(self.s)] = Expression(self.s) template = FunctionPulseTemplate.deserialize(serializer, **basic_data) self.assertEqual('hugo', template.identifier) - self.assertEqual({'a', 'b', 'c'}, template.parameter_names) - self.assertEqual({ParameterDeclaration(name) for name in {'a', 'b', 'c'}}, + self.assertEqual({'a', 'b', 'c', 'x', 'z', 'j', 'u'}, template.parameter_names) + self.assertEqual({ParameterDeclaration(name) for name in template.parameter_names}, template.parameter_declarations) + self.assertEqual(template.measurement_declarations, + self.meas_dict) serialized_data = template.get_serialization_data(serializer) del basic_data['identifier'] self.assertEqual(basic_data, serialized_data) @@ -78,7 +90,8 @@ def test_build_waveform(self) -> None: wf = self.fpt.build_waveform(self.args, {}, channel_mapping={'default': 'default'}) self.assertIsNotNone(wf) self.assertIsInstance(wf, MultiChannelWaveform) - expected_waveform = MultiChannelWaveform({'default':FunctionWaveform(dict(a=3, y=1), Expression(self.f), Expression(self.duration))}) + expected_waveform = MultiChannelWaveform({'default': FunctionWaveform(Expression(self.f), + Expression(self.duration))}) self.assertEqual(expected_waveform, wf) def test_requires_stop(self) -> None: @@ -91,11 +104,11 @@ def test_requires_stop(self) -> None: class FunctionWaveformTest(unittest.TestCase): def test_equality(self) -> None: - wf1a = FunctionWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b'), channel='A', measurement_windows=[]) - wf1b = FunctionWaveform(dict(a=2, b=1), Expression('a*t'), Expression('b'), channel='A', measurement_windows=[]) - wf2 = FunctionWaveform(dict(a=3, b=1), Expression('a*t'), Expression('b'), channel='A', measurement_windows=[]) - wf3 = FunctionWaveform(dict(a=2, b=1), Expression('a*t+2'), Expression('b'), channel='A', measurement_windows=[]) - wf4 = FunctionWaveform(dict(a=2, c=2), Expression('a*t'), Expression('c'), channel='A', measurement_windows=[]) + wf1a = FunctionWaveform(Expression('2*t'), 3, measurement_windows=[], channel='A') + wf1b = FunctionWaveform(Expression('2*t'), 3, measurement_windows=[], channel='A') + wf2 = FunctionWaveform(Expression('2*t'), 3, measurement_windows=[('K', 1, 2)], channel='A') + wf3 = FunctionWaveform(Expression('2*t+2'), 3, measurement_windows=[], channel='A') + wf4 = FunctionWaveform(Expression('2*t'), 4, measurement_windows=[], channel='A') self.assertEqual(wf1a, wf1a) self.assertEqual(wf1a, wf1b) self.assertNotEqual(wf1a, wf2) @@ -103,25 +116,101 @@ def test_equality(self) -> None: self.assertNotEqual(wf1a, wf4) def test_defined_channels(self) -> None: - wf = FunctionWaveform(dict(), Expression('t'), Expression('4'), channel='A', measurement_windows=[]) + wf = FunctionWaveform(Expression('t'), 4, measurement_windows=[], channel='A') self.assertEqual({'A'}, wf.defined_channels) def test_duration(self) -> None: - wf = FunctionWaveform(parameters=dict(foo=2.5), - expression=Expression('2*t'), - duration_expression=Expression('4*foo/5'), - channel='A', - measurement_windows=[]) - self.assertEqual(2, wf.duration) + wf = FunctionWaveform(expression=Expression('2*t'), duration=4/5, measurement_windows=[], + channel='A') + self.assertEqual(4/5, wf.duration) + + def test_unsafe_sample(self): + fw = FunctionWaveform(Expression('sin(2*pi*t) + 3'), 5, channel='A', measurement_windows=[]) + + t = np.linspace(0, 5, dtype=float) + expected_result = np.sin(2*np.pi*t) + 3 + result = fw.unsafe_sample(channel='A', sample_times=t) + np.testing.assert_equal(result, expected_result) + + out_array = np.empty_like(t) + result = fw.unsafe_sample(channel='A', sample_times=t, output_array=out_array) + np.testing.assert_equal(result, expected_result) + self.assertIs(result, out_array) + @unittest.skip def test_sample(self) -> None: f = Expression("(t+1)**b") length = Expression("c**b") par = {"b":2,"c":10} - fw = FunctionWaveform(par, f, length, channel='A', measurement_windows=[]) + fw = FunctionWaveform(f, length, measurement_windows=[], channel='A') a = np.arange(4) expected_result = [[1, 4, 9, 16]] result = fw.sample(a) self.assertTrue(np.all(result == expected_result)) - \ No newline at end of file + + +class FunctionPulseMeasurementTest(unittest.TestCase): + def assert_window_equal(self, w1, w2): + self.assertEqual(len(w1), len(w2)) + self.assertEqual(type(w1), type(w2)) + for x, y in zip(w1, w2): + self.assertEqual(type(y), type(y)) + if isinstance(x, str): + self.assertEqual(sympy.sympify(x), sympy.sympify(y)) + else: + self.assertEqual(x, y) + + def assert_declaration_dict_equal(self, d1, d2): + self.assertEqual(set(d1.keys()), set(d2.keys())) + + for k in d1.keys(): + self.assertEqual(len(d1[k]), len(d2[k])) + for w1, w2 in zip(d1[k], d2[k]): + self.assert_window_equal(w1, w2) + + def test_measurement_windows(self) -> None: + pulse = FunctionPulseTemplate(5, 5) + + pulse.add_measurement_declaration('mw', 0, 5) + windows = pulse.get_measurement_windows(parameters={}, measurement_mapping={'mw': 'asd'}) + self.assertEqual([('asd', 0, 5)], windows) + self.assertEqual(pulse.measurement_declarations, dict(mw=[(0, 5)])) + + def test_no_measurement_windows(self) -> None: + pulse = FunctionPulseTemplate(5, 5) + + windows = pulse.get_measurement_windows({}, {'mw': 'asd'}) + self.assertEqual([], windows) + self.assertEqual(dict(), pulse.measurement_declarations) + + def test_measurement_windows_with_parameters(self) -> None: + pulse = FunctionPulseTemplate(5, 'length') + + pulse.add_measurement_declaration('mw',1,'(1+length)/2') + parameters = dict(length=100) + windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) + self.assertEqual(windows, [('asd', 1, 101/2)]) + + declared = pulse.measurement_declarations + expected = dict(mw=[(1, '(1+length)/2')]) + + self.assert_declaration_dict_equal(declared, expected) + + def test_multiple_measurement_windows(self) -> None: + pulse = FunctionPulseTemplate(5, 'length') + + pulse.add_measurement_declaration('A', 0, '(1+length)/2') + pulse.add_measurement_declaration('A', 1, 3) + pulse.add_measurement_declaration('B', 'begin', 2) + + parameters = dict(length=5, begin=1) + measurement_mapping = dict(A='A', B='C') + windows = pulse.get_measurement_windows(parameters=parameters, + measurement_mapping=measurement_mapping) + expected = [('A', 0, 3), ('A', 1, 3), ('C', 1, 2)] + self.assertEqual(sorted(windows), sorted(expected)) + + self.assert_declaration_dict_equal(pulse.measurement_declarations, + dict(A=[(0, '(1+length)/2'), (1, 3)], + B=[('begin', 2)])) \ No newline at end of file diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 4cd9168d4..a576501da 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -63,6 +63,26 @@ def test_measurement_windows_with_parameters(self) -> None: parameters = dict(length=100) windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) self.assertEqual(windows, [('asd', 1, 101/2)]) + self.assertEqual(pulse.measurement_declarations, dict(mw=[(1, '(1+length)/2')])) + + def test_multiple_measurement_windows(self) -> None: + pulse = TablePulseTemplate() + pulse.add_entry(1, 1) + pulse.add_entry('length', 0) + + pulse.add_measurement_declaration('A', 0, '(1+length)/2') + pulse.add_measurement_declaration('A', 1, 3) + pulse.add_measurement_declaration('B', 'begin', 2) + + parameters = dict(length=5, begin=1) + measurement_mapping = dict(A='A', B='C') + windows = pulse.get_measurement_windows(parameters=parameters, + measurement_mapping=measurement_mapping) + expected = [('A', 0, 3), ('A', 1, 3), ('C', 1, 2)] + self.assertEqual(sorted(windows), sorted(expected)) + self.assertEqual(pulse.measurement_declarations, + dict(A=[(0, '(1+length)/2'), (1, 3)], + B=[('begin', 2)])) def test_add_entry_empty_time_is_negative(self) -> None: table = TablePulseTemplate() From 86219470b656029aad1a26885bb2ddb69bee08ad Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:07:52 +0100 Subject: [PATCH 031/116] Fix MultiChannelWaveform compare_key --- .../pulses/multi_channel_pulse_template.py | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index aa4624031..830bf0250 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -47,7 +47,7 @@ class MultiChannelWaveform(Waveform): assigned more than one channel of any Waveform object it consists of """ - def __init__(self, subwaveforms: Iterable[Waveform]) -> None: + def __init__(self, sub_waveforms: Iterable[Waveform]) -> None: """Create a new MultiChannelWaveform instance. Requires a list of subwaveforms in the form (Waveform, List(int)) where the list defines @@ -55,7 +55,7 @@ def __init__(self, subwaveforms: Iterable[Waveform]) -> None: subwaveform will be mapped to channel y of this MultiChannelWaveform object. Args: - subwaveforms (Iterable( Waveform )): The list of subwaveforms of this + sub_waveforms (Iterable( Waveform )): The list of sub waveforms of this MultiChannelWaveform Raises: ValueError, if a channel mapping is out of bounds of the channels defined by this @@ -65,18 +65,25 @@ def __init__(self, subwaveforms: Iterable[Waveform]) -> None: ValueError, if subwaveforms have inconsistent durations """ super().__init__() - if not subwaveforms: + if not sub_waveforms: raise ValueError( "MultiChannelWaveform cannot be constructed without channel waveforms." ) - def flattened_sub_waveforms(): - for sub_waveform in subwaveforms: + # avoid unnecessary multi channel nesting + def flatten_sub_waveforms(to_flatten): + for sub_waveform in to_flatten: if isinstance(sub_waveform, MultiChannelWaveform): yield from sub_waveform.__sub_waveforms else: yield sub_waveform - self.__sub_waveforms = tuple(flattened_sub_waveforms()) + + # sort the waveforms with their defined channels to make compare key reproducible + def get_sub_waveform_sort_key(waveform): + return sorted(tuple(waveform.defined_channels)) + + self.__sub_waveforms = sorted(flatten_sub_waveforms(sub_waveforms), + key=get_sub_waveform_sort_key) if not all(waveform.duration == self.__sub_waveforms[0].duration for waveform in self.__sub_waveforms[1:]): raise ValueError( @@ -105,8 +112,8 @@ def defined_channels(self) -> Set[ChannelID]: @property def compare_key(self) -> Any: - # make independent of order - return set(self.__sub_waveforms) + # sort with channels + return tuple(sub_waveform.compare_key for sub_waveform in self.__sub_waveforms) def unsafe_sample(self, channel: ChannelID, @@ -259,8 +266,13 @@ def atomicity(self, val: bool): raise ValueError('Cannot make atomic as not all sub templates are atomic') self.__atomicity = val - def build_waveform(self, parameters: Dict[str, Parameter]) -> Optional['MultiChannelWaveform']: - return MultiChannelWaveform([subtemplate.build_waveform(parameters) for subtemplate in self.__subtemplates]) + def build_waveform(self, parameters: Dict[str, Parameter], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['MultiChannelWaveform']: + return MultiChannelWaveform( + [subtemplate.build_waveform(parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) for subtemplate in self.__subtemplates]) def build_sequence(self, sequencer: 'Sequencer', From eee5d6836736f79eb70cbab79ab21feb24497815 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:10:37 +0100 Subject: [PATCH 032/116] Replace pytabor make_combined_wave with more efficient and adapted variant TESTED! --- qctoolkit/hardware/util.py | 42 ++++++++++++++++++++++++++ tests/hardware/util_tests.py | 58 +++++++++++++++++++++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/qctoolkit/hardware/util.py b/qctoolkit/hardware/util.py index 68a40bccf..323450467 100644 --- a/qctoolkit/hardware/util.py +++ b/qctoolkit/hardware/util.py @@ -1,3 +1,5 @@ +from typing import List, Tuple + import numpy as np __all__ = ['voltage_to_uint16'] @@ -24,3 +26,43 @@ def voltage_to_uint16(voltage: np.ndarray, output_amplitude: float, output_offse non_dc_voltage *= (2**resolution - 1) / (2*output_amplitude) np.rint(non_dc_voltage, out=non_dc_voltage) return non_dc_voltage.astype(np.uint16) + + +def make_combined_wave(segments: List['TaborSegment'], destination_array=None, fill_value=None) -> np.ndarray: + quantum = 16 + if len(segments) == 0: + return np.zeros(0, dtype=np.uint16) + segment_lengths = np.fromiter((segment.num_points for segment in segments), count=len(segments), dtype=int) + if np.any(segment_lengths % quantum != 0): + raise ValueError('Segment is not a multiple of 16') + n_quanta = np.sum(segment_lengths) // quantum + len(segments) - 1 + + if destination_array is not None: + if len(destination_array) != 2*n_quanta*quantum: + raise ValueError('Destination array has an invalid length') + destination_array = destination_array.reshape((2*n_quanta, quantum)) + else: + destination_array = np.empty((2*n_quanta, quantum), dtype=np.uint16) + if fill_value: + destination_array[:] = fill_value + + segment_quanta = 2 * segment_lengths[0] // quantum + if segments[0][1] is not None: + destination_array[0:segment_quanta:2].flat = segments[0][1] + if segments[0][0] is not None: + destination_array[1:segment_quanta:2].flat = segments[0][0] + + current_quantum = segment_quanta + for (chan_a, chan_b), segment_length in zip(segments[1:], segment_lengths[1:]): + segment_quanta = 2 * (segment_length // quantum + 1) + segment_destination = destination_array[current_quantum:current_quantum+segment_quanta, :] + + if chan_b is not None: + segment_destination[0, :] = chan_b[0] + segment_destination[2::2, :].flat = chan_b + if chan_a is not None: + segment_destination[1, :] = chan_a[0] + segment_destination[3::2, :].flat = chan_a + current_quantum += segment_quanta + return destination_array.ravel() + diff --git a/tests/hardware/util_tests.py b/tests/hardware/util_tests.py index 9bc2bdf2d..3df325e9a 100644 --- a/tests/hardware/util_tests.py +++ b/tests/hardware/util_tests.py @@ -1,7 +1,11 @@ import unittest +import itertools + +import pytabor import numpy as np -from qctoolkit.hardware.util import voltage_to_uint16 +from qctoolkit.hardware.awgs.tabor import TaborSegment +from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave class VoltageToBinaryTests(unittest.TestCase): @@ -22,3 +26,55 @@ def test_voltage_to_uint16(self): received_data = voltage_to_uint16(linspace_voltage, 0.5, 0.5, 7) self.assertTrue(np.all(expected_data == received_data)) + + +class TaborMakeCombinedTest(unittest.TestCase): + + def exec_general(self, data_1, data_2): + tabor_segments = [TaborSegment(d1, d2) for d1, d2 in zip(data_1, data_2)] + expected_length = (sum(segment.num_points for segment in tabor_segments) + 16 * (len(tabor_segments) - 1)) * 2 + + offset = 0 + pyte_result = 15000*np.ones(expected_length, dtype=np.uint16) + for i, segment in enumerate(tabor_segments): + offset = pytabor.make_combined_wave(segment[0], segment[1], + dest_array=pyte_result, dest_array_offset=offset, + add_idle_pts=i > 0) + self.assertEqual(expected_length, offset) + + result = make_combined_wave(tabor_segments, fill_value=15000) + np.testing.assert_equal(pyte_result, result) + + dest_array = 15000*np.ones(expected_length, dtype=np.uint16) + result = make_combined_wave(tabor_segments, destination_array=dest_array) + np.testing.assert_equal(pyte_result, result) + # test that the destination array data is not copied + self.assertEqual(dest_array.__array_interface__['data'], + result.__array_interface__['data']) + + with self.assertRaises(ValueError): + make_combined_wave(tabor_segments, destination_array=np.ones(16)) + + + def test_make_comb_both(self): + gen = itertools.count() + data_1 = [np.fromiter(gen, count=32, dtype=np.uint16), + np.fromiter(gen, count=16, dtype=np.uint16), + np.fromiter(gen, count=192, dtype=np.uint16)] + + data_2 = [np.fromiter(gen, count=32, dtype=np.uint16), + np.fromiter(gen, count=16, dtype=np.uint16), + np.fromiter(gen, count=192, dtype=np.uint16)] + + self.exec_general(data_1, data_2) + + def test_make_single_chan(self): + gen = itertools.count() + data_1 = [np.fromiter(gen, count=32, dtype=np.uint16), + np.fromiter(gen, count=16, dtype=np.uint16), + np.fromiter(gen, count=192, dtype=np.uint16)] + + data_2 = [None]*len(data_1) + self.exec_general(data_1, data_2) + self.exec_general(data_2, data_1) + From f1358bbae109b998d5f15e010b59240765f70a70 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:12:22 +0100 Subject: [PATCH 033/116] Simplify DummyAWG --- qctoolkit/hardware/awgs/base.py | 57 +++++++++++++++------------------ tests/hardware/awg_tests.py | 16 ++------- 2 files changed, 28 insertions(+), 45 deletions(-) diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index daf4e6ac2..8303ea3dd 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -8,7 +8,7 @@ """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Set, Tuple, List +from typing import Set, Tuple, List, Callable, Optional from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.hardware.program import Loop @@ -44,8 +44,9 @@ def num_markers(self): @abstractmethod def upload(self, name: str, program: Loop, - channels: List[ChannelID], - markers: List[ChannelID], + channels: Tuple[Optional[ChannelID], ...], + markers: Tuple[Optional[ChannelID], ...], + voltage_transformation: Tuple[Optional[Callable], ...], force: bool=False) -> None: """Upload a program to the AWG. @@ -57,8 +58,10 @@ def upload(self, name: str, Args: name (str): A name for the program on the AWG. program (Loop): The program (a sequence of instructions) to upload. - channels (List): List of channels in the program to use. Index of channel ID corresponds to the AWG channel - markers (List): List of channels in the program to use. Index of channel ID corresponds to the AWG channel + channels (List): Tuple of length num_channels that ChannelIDs of in the program to use. Position in the list corresponds to the AWG channel + markers (List): List of channels in the program to use. Position in the List in the list corresponds to the AWG channel + voltage_transformation (List): transformations applied to the waveforms extracted rom the program. Position + in the list corresponds to the AWG channel force (bool): If a different sequence is already present with the same name, it is overwritten if force is set to True. (default = False) """ @@ -85,7 +88,10 @@ def programs(self) -> Set[str]: def sample_rate(self) -> float: """The sample rate of the AWG.""" + @property def compare_key(self) -> int: + """Comparison and hashing is based on the id of the AWG so different devices with the same properties + are ot equal""" return id(self) def __copy__(self) -> None: @@ -101,7 +107,7 @@ class DummyAWG(AWG): def __init__(self, memory: int=100, sample_rate: float=10, - output_range: Tuple[float, float]=(-5,5), + output_range: Tuple[float, float]=(-5, 5), num_channels: int=1, num_markers: int=1) -> None: """Create a new DummyAWG instance. @@ -120,17 +126,10 @@ def __init__(self, self._output_range = output_range self._num_channels = num_channels self._num_markers = num_markers + self._channels = ('default',) + self._armed = None - def add_waveform(self, waveform) -> int: - try: - index = self._waveform_memory.index(None) - except ValueError: - raise OutOfWaveformMemoryException() - self._waveform_memory[index] = waveform - self._waveform_indices[hash(waveform)] = index - return index - - def upload(self, name, program, force=False) -> None: + def upload(self, name, program, channels, markers, voltage_transformation, force=False) -> None: if name in self.programs: if not force: raise ProgramOverwriteException(name) @@ -138,28 +137,16 @@ def upload(self, name, program, force=False) -> None: self.remove(name) self.upload(name, program) else: - self._programs[name] = program - exec_blocks = filter(lambda x: type(x) == EXECInstruction, program) - indices = frozenset(self.add_waveform(block.waveform) for block in exec_blocks) - self._program_wfs[name] = indices + self._programs[name] = (program, channels, markers, voltage_transformation) - def remove(self,name) -> None: + def remove(self, name) -> None: if name in self.programs: self._programs.pop(name) self.program_wfs.pop(name) self.clean() - def clean(self) -> None: - necessary_wfs = reduce(lambda acc, s: acc.union(s), self._program_wfs.values(), set()) - all_wfs = set(self._waveform_indices.values()) - delete = all_wfs - necessary_wfs - for index in delete: - wf = self._waveform_memory(index) - self._waveform_indices.pop(wf) - self._waveform_memory = None - def arm(self, name: str) -> None: - raise NotImplementedError() + self._armed = name @property def programs(self) -> Set[str]: @@ -201,3 +188,11 @@ class OutOfWaveformMemoryException(Exception): def __str__(self) -> str: return "Out of memory error adding waveform to waveform memory." + + +class ChannelNotFoundException(Exception): + def __init__(self, channel): + self.channel = channel + + def __str__(self) -> str: + return 'Marker or channel not found: {}'.format(self.channel) diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 9ab13ab62..f1ce158ec 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -17,23 +17,11 @@ def setUp(self): self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) self.program = self.sequencer.build() - def test_OutOfMemoryException(self): - dummy = awg.DummyAWG(10) - with self.assertRaises(awg.OutOfWaveformMemoryException): - dummy.upload('program', self.program) - def test_ProgramOverwriteException(self): dummy = awg.DummyAWG(100) - dummy.upload('program', self.program) + dummy.upload('program', self.program, [], [], []) with self.assertRaises(awg.ProgramOverwriteException): - dummy.upload('program', self.program) - - def test_upload(self): - dummy = awg.DummyAWG(100) - dummy.upload('program',self.program) - memory_part = [None for i in range(89)] - self.assertEqual(dummy._waveform_memory[11:], memory_part) - self.assertEqual(dummy.programs, set(['program'])) + dummy.upload('program', self.program, [], [], []) class TektronixAWGTest(unittest.TestCase): From 637b3919a61946e83b57f2463424874a5e52d787 Mon Sep 17 00:00:00 2001 From: "Pascal Cerfontaine (Lab PC)" Date: Thu, 9 Mar 2017 18:15:01 +0100 Subject: [PATCH 034/116] Misc non hardware changes --- qctoolkit/expressions.py | 4 ++-- qctoolkit/pulses/repetition_pulse_template.py | 2 +- tests/pulses/table_pulse_template_tests.py | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index 01cb4de00..5555dbe2e 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -84,7 +84,7 @@ def evaluate_numeric(self, **kwargs) -> Union[Number, numpy.ndarray]: return result raise NonNumericEvaluation(self, result, kwargs) - def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> sympy.Expr: + def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> 'Expression': """Evaluate the expression symbolically. Args: @@ -92,7 +92,7 @@ def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> sympy.Expr: Returns: """ - return self.__expression.subs(substitutions) + return Expression(self.__expression.subs(substitutions)) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: return dict(expression=str(self)) diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index fc7808492..af6fcd1fb 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -111,7 +111,7 @@ def get_repetition_count_value(self, parameters: Dict[str, Parameter]) -> int: value = self._repetition_count.get_value(parameters) if isinstance(value, float) and not value.is_integer(): raise ParameterNotIntegerException(self._repetition_count.name, value) - return value + return int(value) else: return self._repetition_count def __str__(self) -> str: diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index a576501da..47ee4ce56 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -10,7 +10,6 @@ from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyInterpolationStrategy, DummyParameter, DummyCondition from tests.serialization_dummies import DummySerializer - class TablePulseTemplateTest(unittest.TestCase): def test_add_entry_known_interpolation_strategies(self) -> None: @@ -43,9 +42,10 @@ def test_measurement_windows(self) -> None: pulse.add_entry(1, 1) pulse.add_entry(3, 0) pulse.add_entry(5, 0) - pulse.add_measurement_declaration('mw',0,5) + pulse.add_measurement_declaration('mw', 0, 5) windows = pulse.get_measurement_windows(parameters={}, measurement_mapping={'mw': 'asd'}) self.assertEqual([('asd', 0, 5)], windows) + self.assertEqual(pulse.measurement_declarations, dict(mw=[(0, 5)])) def test_no_measurement_windows(self) -> None: pulse = TablePulseTemplate() @@ -54,6 +54,7 @@ def test_no_measurement_windows(self) -> None: pulse.add_entry(5, 0) windows = pulse.get_measurement_windows({}, {'mw': 'asd'}) self.assertEqual([], windows) + self.assertEqual(dict(), pulse.measurement_declarations) def test_measurement_windows_with_parameters(self) -> None: pulse = TablePulseTemplate() From 89b3d26027c5a55f570d265f82c99cf7b15fb12a Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:16:48 +0100 Subject: [PATCH 035/116] Make measurement window collection more efficient. Consider complete preallocation as allocation/deallocation of memory takes most of the time --- qctoolkit/hardware/dacs/__init__.py | 6 +-- qctoolkit/hardware/dacs/alazar.py | 74 ++++++++++++++------------- qctoolkit/hardware/program.py | 79 +++++++++++++++++++++-------- 3 files changed, 102 insertions(+), 57 deletions(-) diff --git a/qctoolkit/hardware/dacs/__init__.py b/qctoolkit/hardware/dacs/__init__.py index 25ba39513..5bc0f5297 100644 --- a/qctoolkit/hardware/dacs/__init__.py +++ b/qctoolkit/hardware/dacs/__init__.py @@ -1,6 +1,5 @@ from abc import ABCMeta, abstractmethod -from typing import Dict -from collections import deque +from typing import Dict, Tuple __all__ = ['DAC'] @@ -9,7 +8,8 @@ class DAC(metaclass=ABCMeta): """Representation of a data acquisition card""" @abstractmethod - def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]) -> None: + def register_measurement_windows(self, program_name: str, windows: Dict[str, Tuple['numpy.ndarray', + 'numpy.ndarray']]) -> None: """""" @abstractmethod diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py index ee0265b84..9a4621ed0 100644 --- a/qctoolkit/hardware/dacs/alazar.py +++ b/qctoolkit/hardware/dacs/alazar.py @@ -1,5 +1,5 @@ -from typing import Union, Dict, NamedTuple, List, Any -from collections import deque +from typing import Union, Dict, NamedTuple, List, Any, Optional, Tuple +from collections import deque, defaultdict import numpy as np @@ -11,7 +11,7 @@ class AlazarProgram: - def __init__(self, masks=None, operations=None, total_length=None): + def __init__(self, masks=list(), operations=list(), total_length=None): self.masks = masks self.operations = operations self.total_length = total_length @@ -22,50 +22,51 @@ def __iter__(self): class AlazarCard(DAC): - def __init__(self, card, config: Union[ScanlineConfiguration, None] = None): + def __init__(self, card, config: Optional[ScanlineConfiguration]=None): self.__card = card self.__definitions = dict() self.config = config - self.__mask_prototypes = dict() # type: Dict[str, Tuple[Any, str]] + self._mask_prototypes = dict() # type: Dict[str, Tuple[Any, str]] - self.__registered_programs = dict() # type: Dict[str, AlazarProgram] + self._registered_programs = defaultdict(AlazarProgram) # type: Dict[str, AlazarProgram] @property def card(self) -> Any: return self.__card - def __make_mask(self, mask_id: str, window_deque: deque) -> Mask: - if mask_id not in self.__mask_prototypes: + def __make_mask(self, mask_id: str, begins, lengths) -> Mask: + if mask_id not in self._mask_prototypes: raise KeyError('Measurement window {} can not be converted as it is not registered.'.format(mask_id)) - hardware_channel, mask_type = self.__mask_prototypes[mask_id] + hardware_channel, mask_type = self._mask_prototypes[mask_id] if mask_type not in ('auto', 'cross_buffer', None): raise ValueError('Currently only can do cross buffer mask') - begins_lengths = np.asarray(window_deque) - begins = begins_lengths[:, 0] - lengths = begins_lengths[:, 1] - - sorting_indices = np.argsort(begins) - begins = begins[sorting_indices] - lengths = lengths[sorting_indices] - - if np.any(begins[:-1]+lengths[:-1] >= begins[1:]): + if np.any(begins[:-1]+lengths[:-1] > begins[1:]): raise ValueError('Found overlapping windows in begins') mask = CrossBufferMask() + mask.identifier = mask_id mask.begin = begins mask.length = lengths mask.channel = hardware_channel return mask - def register_measurement_windows(self, program_name: str, windows: Dict[str, deque]) -> None: - for mask_id, window_deque in windows.items(): - begins_lengths = np.asarray(window_deque) - begins = begins_lengths[:, 0] - lengths = begins_lengths[:, 1] + def register_measurement_windows(self, + program_name: str, + windows: Dict[str, Tuple[np.ndarray, np.ndarray]]) -> None: + if not windows: + return + total_length = 0 + for mask_id, (begins, lengths) in windows.items(): + + sample_factor = self.config.captureClockConfiguration.numeric_sample_rate(self.__card.model) / 10**9 + + begins = np.rint(begins*sample_factor).astype(dtype=np.uint64) + lengths = np.floor(lengths*sample_factor).astype(dtype=np.uint64) + sorting_indices = np.argsort(begins) begins = begins[sorting_indices] lengths = lengths[sorting_indices] @@ -75,30 +76,35 @@ def register_measurement_windows(self, program_name: str, windows: Dict[str, deq total_length = np.ceil(total_length/self.__card.minimum_record_size) * self.__card.minimum_record_size - self.__registered_programs.get(program_name, - default=AlazarProgram()).masks = [ - self.__make_mask(mask_id, window_deque) - for mask_id, window_deque in windows.items()] - self.__registered_programs[program_name].total_length = total_length + self._registered_programs[program_name].masks = [ + self.__make_mask(mask_id, *window_begin_length) + for mask_id, window_begin_length in windows.items()] + self._registered_programs[program_name].total_length = total_length def register_operations(self, program_name: str, operations) -> None: - self.__registered_programs.get(program_name, - default=AlazarProgram() - ).operations = self.__registered_programs.get(program_name, self) + self._registered_programs[program_name].operations = operations def arm_program(self, program_name: str) -> None: config = self.config - config.masks, config.operations, total_record_size = self.__registered_programs[program_name] + config.masks, config.operations, total_record_size = self._registered_programs[program_name] - if config.totalRecordSize is None: + if config.totalRecordSize == 0: config.totalRecordSize = total_record_size elif config.totalRecordSize < total_record_size: raise ValueError('specified total record size is smaller than needed {} < {}'.format(config.totalRecordSize, total_record_size)) - config.apply(self.__card) + config.apply(self.__card, True) self.__card.startAcquisition(1) def delete_program(self, program_name: str) -> None: self.__registered_operations.pop(program_name, None) self.__registered_masks.pop(program_name, None) + + @property + def mask_prototypes(self): + return self._mask_prototypes + + def register_mask_for_channel(self, mask_id, hw_channel, mask_type='auto'): + self._mask_prototypes[mask_id] = (hw_channel, mask_type) + diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 30b33b56d..bfd083dfd 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -1,8 +1,10 @@ import itertools from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable, Tuple -from collections import deque +from collections import deque, defaultdict from copy import deepcopy -from ctypes import c_int64 as MutableInt +from ctypes import c_double as MutableFloat + +import numpy as np from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.pulses.instructions import AbstractInstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction, Waveform @@ -23,14 +25,17 @@ def __init__(self, super().__init__(parent=parent, children=children) self._waveform = waveform - self._repetition_count = repetition_count + self._repetition_count = int(repetition_count) + + if abs(self._repetition_count - repetition_count) > 1e-10: + raise ValueError('Repetition count was not an integer') if not isinstance(waveform, (type(None), Waveform)): raise Exception() @property def compare_key(self) -> Tuple: - return self._waveform, self._repetition_count, tuple(c.compare_key for c in self) + return self._waveform, self.repetition_count, tuple(c.compare_key for c in self) def append_child(self, **kwargs) -> None: self[len(self):len(self)] = (kwargs, ) @@ -43,13 +48,22 @@ def waveform(self) -> Waveform: def waveform(self, val) -> None: self._waveform = val + @property + def duration(self): + if self.is_leaf(): + return self.repetition_count*self.waveform.duration + else: + return self.repetition_count*sum(child.duration for child in self) + @property def repetition_count(self) -> int: return self._repetition_count @repetition_count.setter def repetition_count(self, val) -> None: - self._repetition_count = val + self._repetition_count = int(val) + if abs(self._repetition_count - val) > 1e-10: + raise ValueError('Repetition count was not an integer') def unroll(self) -> None: for i, e in enumerate(self.parent): @@ -66,14 +80,14 @@ def unroll_children(self) -> None: self[:] = (child.copy_tree_structure() for _ in range(self.repetition_count) for child in old_children) - self._repetition_count = 1 + self.repetition_count = 1 self.assert_tree_integrity() def encapsulate(self) -> None: self[:] = [Loop(children=self.children, - repetition_count=self._repetition_count, + repetition_count=self.repetition_count, waveform=self._waveform)] - self._repetition_count = 1 + self.repetition_count = 1 self._waveform = None self.assert_tree_integrity() @@ -83,9 +97,9 @@ def __repr__(self) -> str: return '{}: Circ {}'.format(id(self), is_circular) if self.is_leaf(): - return 'EXEC {} {} times'.format(self._waveform, self._repetition_count) + return 'EXEC {} {} times'.format(self._waveform, self.repetition_count) else: - repr = ['LOOP {} times:'.format(self._repetition_count)] + repr = ['LOOP {} times:'.format(self.repetition_count)] for elem in self: sub_repr = elem.__repr__().splitlines() sub_repr = [' ->' + sub_repr[0]] + [' ' + line for line in sub_repr[1:]] @@ -98,18 +112,43 @@ def copy_tree_structure(self, new_parent: Union['Loop', bool]=False) -> 'Loop': repetition_count=self.repetition_count, children=(child.copy_tree_structure() for child in self)) - def get_measurement_windows(self, offset: MutableInt = MutableInt(0), measurement_windows=dict()) -> Dict[str, - deque]: + def get_measurement_windows(self) -> Dict[str, Tuple[np.ndarray, np.ndarray]]: if self.is_leaf(): - for _ in range(self.repetition_count): - for (mw_name, begin, length) in self.waveform.get_measurement_windows(): - measurement_windows.get(mw_name, default=deque()).append((begin + offset.value, length)) - offset.value += self.waveform.duration + body_duration = self.waveform.duration + temp_meas_windows = defaultdict(deque) + for (mw_name, begin, length) in self.waveform.get_measurement_windows(): + temp_meas_windows[mw_name].append((begin, length)) + + body_meas_windows = dict() + while temp_meas_windows: + mw_name, begin_lengths = temp_meas_windows.popitem() + begin_lengths = np.asarray(begin_lengths) + body_meas_windows[mw_name] = (begin_lengths[:, 0], begin_lengths[:, 1]) else: - for _ in range(self.repetition_count): - for child in self: - child.get_measurement_windows(offset, measurement_windows=measurement_windows) - return measurement_windows + offset = 0 + temp_meas_windows = defaultdict(deque) + for child in self: + for mw_name, (begins, lengths) in child.get_measurement_windows().items(): + temp_meas_windows[mw_name].append((begins+offset, lengths)) + offset += child.duration + + body_meas_windows = dict() + while temp_meas_windows: + mw_name, begin_length_deque = temp_meas_windows.popitem() + begin_length_deque = np.asarray(begin_length_deque) + + body_meas_windows[mw_name] = (np.concatenate(tuple(begin for begin, _ in begin_length_deque)), + np.concatenate(tuple(length for _, length in begin_length_deque))) + body_duration = offset + result = dict() + while body_meas_windows: + mw_name, (body_begins, body_lengths) = body_meas_windows.popitem() + result[mw_name] = ( + np.tile(body_begins, self.repetition_count) + np.repeat(range(self.repetition_count), + len(body_begins)) * body_duration, + np.tile(body_lengths, self.repetition_count) + ) + return result def split_one_child(self, child_index=None) -> None: """Take the last child that has a repetition count larger one, decrease it's repetition count and insert a copy From 40f3c94fc7876431735c73cce6e6365a67f85dde Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:49:33 +0100 Subject: [PATCH 036/116] Tabor AWG works in advanced sequencing mode --- qctoolkit/hardware/awgs/tabor.py | 577 +++++++++++++++++++++++-------- qctoolkit/hardware/setup.py | 165 ++++++--- tests/hardware/program_tests.py | 60 ++++ tests/hardware/tabor_tests.py | 47 +-- 4 files changed, 643 insertions(+), 206 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 028626868..1f5f1e825 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,41 +1,58 @@ -from qctoolkit import ChannelID -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -from qctoolkit.hardware.program import Loop, MultiChannelProgram -from qctoolkit.hardware.util import voltage_to_uint16 -from qctoolkit.hardware.awgs import AWG - +"""""" +import fractions import sys -import numpy as np -from typing import List, Tuple, Iterable, Set, NamedTuple +from typing import List, Tuple, Iterable, Set, NamedTuple, Callable, Optional # Provided by Tabor electronics for python 2.7 # a python 3 version is in a private repository on https://git.rwth-aachen.de/qutech # Beware of the string encoding change! import pytabor import teawg +import numpy as np + +from qctoolkit import ChannelID +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform +from qctoolkit.hardware.program import Loop, MultiChannelProgram +from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave +from qctoolkit.hardware.awgs import AWG + assert(sys.byteorder == 'little') +__all__ = ['TaborAWGRepresentation', 'TaborChannelPair'] + + class TaborSegment(tuple): - def __new__(cls, ch_a, ch_b): + """Represents one segment of two channels on the device. Convenience class.""" + def __new__(cls, ch_a: Optional[np.ndarray], ch_b: Optional[np.ndarray]): return tuple.__new__(cls, (ch_a, ch_b)) def __init__(self, ch_a, ch_b): - pass - - def num_points(self) -> int: - return max(len(self[0]), len(self[1])) + if ch_a is None and ch_b is None: + raise TaborException('Empty TaborSegments are not allowed') def __hash__(self) -> int: - return hash((bytes(self[0]), bytes(self[1]))) + return hash((bytes(self[0]) if self[0] is not None else 0, + bytes(self[1]) if self[1] is not None else 0)) + + def __eq__(self, other): + return + + @property + def num_points(self): + return len(self[0]) if self[1] is None else len(self[1]) + + def get_as_binary(self): + assert not (self[0] is None or self[1] is None) + return make_combined_wave([self]) class TaborProgram: WAVEFORM_MODES = ('single', 'advanced', 'sequence') def __init__(self, - program: MultiChannelProgram, + program: Loop, device_properties, channels: Tuple[ChannelID, ChannelID], markers: Tuple[ChannelID, ChannelID]): @@ -46,51 +63,60 @@ def __init__(self, channel_set = frozenset(channel for channel in channels if channel is not None) | frozenset(marker for marker in markers if marker is not None) - self.__root_loop = None - for known_channels in program.programs.keys(): - if known_channels.issuperset(channel_set): - self.__root_loop = program.programs[known_channels] - break - if self.__root_loop is None: - raise TaborException("{} not found in program.".format(channel_set)) + self._program = program self.__waveform_mode = 'advanced' - self.__channels = channels - self.__markers = markers + self._channels = channels + self._markers = markers self.__used_channels = channel_set self.__device_properties = device_properties - self.__waveforms = [] # type: List[MultiChannelWaveform] - self.__sequencer_tables = [] - self.__advanced_sequencer_table = [] + self._waveforms = [] # type: List[MultiChannelWaveform] + self._sequencer_tables = [] + self._advanced_sequencer_table = [] if self.program.depth() == 0: self.setup_single_waveform_mode() elif self.program.depth() == 1: - self.setup_single_sequence_table_mode() + self.setup_single_sequence_mode() else: self.setup_advanced_sequence_mode() + @property + def markers(self): + return self._markers + + @property + def channels(self): + return self._channels + def setup_single_waveform_mode(self) -> None: raise NotImplementedError() def sampled_segments(self, sample_rate: float, voltage_amplitude: Tuple[float, float], - voltage_offset: Tuple[float, float]) -> List[TaborSegment]: - - segment_lengths = np.fromiter((waveform.duration for waveform in self.__waveforms), - dtype=float, count=len(self.__waveforms)) * sample_rate - if not all(segment_length.is_integer() for segment_length in segment_lengths): - raise TaborException('At least one waveform has a length that is no multiple of the time per sample') - segment_lengths = segment_lengths.astype(dtype=int) + voltage_offset: Tuple[float, float], + voltage_transformation: Tuple[Callable, Callable]) -> List[TaborSegment]: + sample_rate = fractions.Fraction(sample_rate, 10**9) + + segment_lengths = [waveform.duration*sample_rate for waveform in self._waveforms] + if not all(abs(int(segment_length) - segment_length) < 1e-10 and segment_length > 0 + for segment_length in segment_lengths): + raise TaborException('At least one waveform has a length that is no integer or smaller zero') + segment_lengths = np.asarray(segment_lengths, dtype=np.uint64) + + if np.any(segment_lengths % 16 > 0) or np.any(segment_lengths < 192): + raise TaborException('At least one waveform has a length that is smaller 192 or not a multiple of 16') + sample_rate = float(sample_rate) time_array = np.arange(np.max(segment_lengths)) / sample_rate def voltage_to_data(waveform, time, channel): - if self.__channels[channel]: + if self._channels[channel]: return voltage_to_uint16( - waveform[self.__channels[channel]].get_sampled(channel=self.__channels[channel], - sample_times=time), + voltage_transformation[channel]( + waveform.get_sampled(channel=self._channels[channel], + sample_times=time)), voltage_amplitude[channel], voltage_offset[channel], resolution=14) @@ -99,21 +125,21 @@ def voltage_to_data(waveform, time, channel): def get_marker_data(waveform: MultiChannelWaveform, time): marker_data = np.zeros(len(time), dtype=np.uint16) - for marker_index, markerID in enumerate(self.__markers): + for marker_index, markerID in enumerate(self._markers): if markerID is not None: marker_data |= (waveform.get_sampled(channel=markerID, sample_times=time) != 0).\ astype(dtype=np.uint16) << marker_index+14 return marker_data - segments = len(self.__waveforms)*[None] - for i, waveform in enumerate(self.__waveforms): + segments = np.empty_like(self._waveforms, dtype=object) + for i, waveform in enumerate(self._waveforms): t = time_array[:int(waveform.duration*sample_rate)] segment_a = voltage_to_data(waveform, t, 0) segment_b = voltage_to_data(waveform, t, 1) assert (len(segment_a) == len(t)) assert (len(segment_b) == len(t)) seg_data = get_marker_data(waveform, t) - segment_b |= seg_data + segment_a |= seg_data segments[i] = TaborSegment(segment_a, segment_b) return segments, segment_lengths @@ -121,9 +147,28 @@ def setup_single_sequence_mode(self) -> None: self.__waveform_mode = 'sequence' if len(self.program) < self.__device_properties['min_seq_len']: raise TaborException('SEQuence:LENGth has to be >={min_seq_len}'.format(**self.__device_properties)) - raise NotImplementedError() + + sequencer_table = [] + waveforms = [] + + for waveform, repetition_count in ((waveform_loop.waveform.get_subset_for_channels(self.__used_channels), + waveform_loop.repetition_count) + for waveform_loop in self.program): + if waveform in waveforms: + segment_no = waveforms.index(waveform) + 1 + else: + segment_no = len(waveforms) + 1 + waveforms.append(waveform) + sequencer_table.append((repetition_count, segment_no, 0)) + + self.__waveform_mode = 'sequence' + self._waveforms = waveforms + self._sequencer_tables = [sequencer_table] + self._advanced_sequencer_table = [(self.program.repetition_count, 1, 0)] def setup_advanced_sequence_mode(self) -> None: + if self.program.depth() == 1: + self.program.encapsulate() while self.program.depth() > 2 or not self.program.is_balanced(): for i, sequence_table in enumerate(self.program): if sequence_table.depth() == 0: @@ -133,7 +178,7 @@ def setup_advanced_sequence_mode(self) -> None: elif len(sequence_table) == 1 and len(sequence_table[0]) == 1: sequence_table.join_loops() elif sequence_table.is_balanced(): - if len(self.program) < self.__device_properties['min_aseq_len'] or (sequence_table.repetition_count / self.__device_properties['max_aseq_len'] < + if len(self.program) < self.__device_properties['min_aseq_len'] or (sequence_table.repetition_count // self.__device_properties['max_aseq_len'] < max(entry.repetition_count for entry in sequence_table) / self.__device_properties['max_seq_len']): sequence_table.unroll() @@ -223,11 +268,11 @@ def check_partial_unroll(program, n): waveform_loop.repetition_count) for waveform_loop in sequencer_table_loop): if waveform in waveforms: - segment_no = waveforms.index(waveform) + 1 + wf_index = waveforms.index(waveform) else: - segment_no = len(waveforms) + 1 + wf_index = len(waveforms) waveforms.append(waveform) - current_sequencer_table.append((repetition_count, segment_no, 0)) + current_sequencer_table.append((repetition_count, wf_index, 0)) if current_sequencer_table in sequencer_tables: sequence_no = sequencer_tables.index(current_sequencer_table) + 1 @@ -237,42 +282,42 @@ def check_partial_unroll(program, n): advanced_sequencer_table.append((sequencer_table_loop.repetition_count, sequence_no, 0)) - self.__advanced_sequencer_table = advanced_sequencer_table - self.__sequencer_tables = sequencer_tables - self.__waveforms = waveforms + self._advanced_sequencer_table = advanced_sequencer_table + self._sequencer_tables = sequencer_tables + self._waveforms = waveforms @property def program(self) -> Loop: - return self.__root_loop + return self._program def get_sequencer_tables(self) -> List[Tuple[int, int, int]]: - return self.__sequencer_tables + return self._sequencer_tables def get_advanced_sequencer_table(self) -> List[Tuple[int, int, int]]: """Advanced sequencer table that can be used via the download_adv_seq_table pytabor command""" - return self.__advanced_sequencer_table + return self._advanced_sequencer_table def get_waveform_data(self, device_properties, sample_rate: float, voltage_amplitude: Tuple[float, float], voltage_offset: Tuple[float, float]) -> Tuple[np.ndarray, np.ndarray]: - if any(not(waveform.duration*sample_rate).is_integer() for waveform in self.__waveforms): + if any(not(waveform.duration*sample_rate).is_integer() for waveform in self._waveforms): raise TaborException('At least one waveform has a length that is no multiple of the time per sample') - maximal_length = int(max(waveform.duration for waveform in self.__waveforms) * sample_rate) + maximal_length = int(max(waveform.duration for waveform in self._waveforms) * sample_rate) time_array = np.arange(0, maximal_length, 1) - maximal_size = int(2 * (sample_rate*sum(waveform.duration for waveform in self.__waveforms) + 16*len(self.__waveforms))) + maximal_size = int(2 * (sample_rate * sum(waveform.duration for waveform in self._waveforms) + 16 * len(self._waveforms))) data = np.empty(maximal_size, dtype=np.uint16) offset = 0 - segment_lengths = np.zeros(len(self.__waveforms), dtype=np.uint32) + segment_lengths = np.zeros(len(self._waveforms), dtype=np.uint32) def voltage_to_data(waveform, time, channel): - return voltage_to_uint16(waveform[self.__channels[channel]].sample(time), + return voltage_to_uint16(waveform[self._channels[channel]].sample(time), voltage_amplitude[channel], voltage_offset[channel], - resolution=14) if self.__channels[channel] else None + resolution=14) if self._channels[channel] else None - for i, waveform in enumerate(self.__waveforms): + for i, waveform in enumerate(self._waveforms): t = time_array[:int(waveform.duration*sample_rate)] segment1 = voltage_to_data(waveform, t, 0) segment2 = voltage_to_data(waveform, t, 1) @@ -320,11 +365,67 @@ def upload_to_device(self, device: 'TaborAWG', channel_pair) -> None: device.download_adv_seq_table(self.get_advanced_sequencer_table()) device.send_cmd('SEQ:SEL 1') + @property + def waveform_mode(self): + return self.__waveform_mode + class TaborAWGRepresentation(teawg.TEWXAwg): - def __init__(self, *args, **kwargs): + def __init__(self, *args, external_trigger=False, reset=False, **kwargs): super().__init__(*args, **kwargs) - self.__selected_channel = None + if reset: + self.visa_inst.write(':RES') + + if external_trigger: + raise NotImplementedError() + + def switch_group_off(grp): + switch_off_cmd = (":INST:SEL {}; :OUTP OFF; :INST:SEL {}; :OUTP OFF; " + ":SOUR:MARK:SEL 1; :SOUR:MARK:STAT OFF; :SOUR:MARK:SOUR USER; " + ":SOUR:MARK:SEL 2; :SOUR:MARK:STAT OFF; :SOUR:MARK:SOUR USER").format(grp+1, grp+2) + self.send_cmd(switch_off_cmd) + switch_group_off(0) + switch_group_off(1) + + setup_command = ( + ":INIT:GATE OFF; :INIT:CONT ON; " + ":INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS; " + ":SOUR:SEQ:JUMP:EVEN BUS") + # Set Software Trigger Mode + self.select_channel(1) + self.send_cmd(setup_command) + self.select_channel(3) + self.send_cmd(setup_command) + + def send_cmd(self, cmd_str, paranoia_level=None): + if paranoia_level is not None: + super().send_cmd(cmd_str=cmd_str, paranoia_level=paranoia_level) + else: + cmd_str = cmd_str.rstrip() + + if len(cmd_str) > 0: + ask_str = cmd_str + '; *OPC?; :SYST:ERR?' + else: + ask_str = '*OPC?; :SYST:ERR?' + + answer = self._visa_inst.ask(ask_str).split(';') + + error_code, error_msg = answer[-1].split(',') + error_code = int(error_code) + if error_code != 0: + _ = self._visa_inst.ask('*CLS; *OPC?') + + if error_code == -450: + # query queue overflow + self.send_cmd(cmd_str) + else: + raise RuntimeError('Cannot execute command: {}\n{}: {}'.format(cmd_str, error_code, error_msg)) + if len(answer) == 2: + return None + elif len(answer) == 3: + return answer[0] + else: + return tuple(answer[:-2]) @property def is_open(self) -> bool: @@ -333,73 +434,136 @@ def is_open(self) -> bool: def select_channel(self, channel) -> None: if channel not in (1, 2, 3, 4): raise TaborException('Invalid channel: {}'.format(channel)) - if self.__selected_channel != channel: - self.send_cmd(':INST:SEL {channel}'.format(channel=channel)) - self.__selected_channel = channel + + self.send_cmd(':INST:SEL {channel}'.format(channel=channel)) + + def select_marker(self, marker): + if marker not in (1, 2, 3, 4): + raise TaborException('Invalid marker: {}'.format(marker)) + self.send_cmd(':SOUR:MARK:SEL {marker}'.format(marker=marker)) def sample_rate(self, channel) -> int: - self.select_channel(channel) - return int(float(self.send_query(':FREQ:RAST?'.format(channel=channel)))) + return int(float(self.send_query(':INST:SEL {channel}; :FREQ:RAST?'.format(channel=channel)))) def amplitude(self, channel) -> float: - self.select_channel(channel) - coupling = self.send_query(':OUTP:COUP?'.format(channel=channel)) + coupling = self.send_query(':INST:SEL {channel}; :OUTP:COUP?'.format(channel=channel)) if coupling == 'DC': return float(self.send_query(':VOLT?')) elif coupling == 'HV': return float(self.send_query(':VOLD:HV?')) def offset(self, channel) -> float: - self.select_channel(channel) - return float(self.send_query(':VOLT:OFFS?'.format(channel=channel))) + return float(self.send_query(':INST:SEL {channel}; :VOLT:OFFS?'.format(channel=channel))) + + def enable(self): + self.send_cmd(':ENAB') + + def abort(self): + self.send_cmd(':ABOR') + + def reset(self): + self.send_cmd(':RES') + self.send_cmd(':INST:SEL 1; :INIT:GATE OFF; :INIT:CONT ON; :INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS') + self.send_cmd(':INST:SEL 3; :INIT:GATE OFF; :INIT:CONT ON; :INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS') TaborProgramMemory = NamedTuple('TaborProgramMemory', [('segment_indices', np.ndarray), - ]) + ('program', TaborProgram)]) + + +def with_configuration_guard(function_object): + def guarding_method(channel_pair: 'TaborChannelPair', *args, **kwargs): + if channel_pair._configuration_guard_count == 0: + channel_pair._enter_config_mode() + channel_pair._configuration_guard_count += 1 + + function_object(channel_pair, *args, **kwargs) + + channel_pair._configuration_guard_count -= 1 + if channel_pair._configuration_guard_count == 0: + channel_pair._exit_config_mode() + + return guarding_method + class TaborChannelPair(AWG): def __init__(self, tabor_device: TaborAWGRepresentation, channels: Tuple[int, int], identifier: str): super().__init__(identifier) - self.__device = tabor_device + self._device = tabor_device + + self._configuration_guard_count = 0 + self._is_in_config_mode = False if channels not in ((1, 2), (3, 4)): raise ValueError('Invalid channel pair: {}'.format(channels)) - self.__channels = channels + self._channels = channels + + self._enter_config_mode() + + # Select channel 1 + self._device.select_channel(self._channels[0]) + self._device.send_cmd(':TRAC:DEL:ALL') + self._device.send_cmd(':SOUR:SEQ:DEL:ALL') + self._device.send_cmd(':ASEQ:DEL') + + self._idle_segment = TaborSegment(voltage_to_uint16(voltage=np.zeros(192), + output_amplitude=0.5, + output_offset=0., resolution=14), + voltage_to_uint16(voltage=np.zeros(192), + output_amplitude=0.5, + output_offset=0., resolution=14)) + self._idle_sequence_table = [(1, 1, 0), (1, 1, 0), (1, 1, 0)] - self.__segment_lengths = np.zeros(0, dtype=int) - self.__segment_capacity = np.zeros(0, dtype=int) - self.__segment_hashes = np.zeros(0, dtype=int) - self.__segment_references = np.zeros(0, dtype=int) + self._device.send_cmd(':TRAC:DEF 1, 192') + self._device.send_cmd(':TRAC:SEL 1') + self._device.send_cmd(':TRAC:MODE COMB') + self._device.send_binary_data(pref=':TRAC:DATA', bin_dat=self._idle_segment.get_as_binary()) + + self._segment_lengths = 192*np.ones(1, dtype=np.uint32) + self._segment_capacity = 192*np.ones(1, dtype=np.uint32) + self._segment_hashes = np.ones(1, dtype=np.int64) * hash(self._idle_segment) + self._segment_references = np.ones(1, dtype=np.uint32) + + self._device.send_cmd('SEQ:SEL 1') + self._device.download_sequencer_table(self._idle_sequence_table) + self._sequencer_tables = [self._idle_sequence_table] + + self._advanced_sequence_table = [] self.total_capacity = int(16e6) - self.__known_programs = dict() # type: Dict[str, TaborProgramMemory] + self._known_programs = dict() # type: Dict[str, TaborProgramMemory] + self._current_program = None + + self._exit_config_mode() def free_program(self, name: str) -> TaborProgramMemory: - program = self.__known_programs.pop(name) - self.__segment_references[program.segment_indices] -= 1 + program = self._known_programs.pop(name) + self._segment_references[program.segment_indices] -= 1 return program @property - def __segment_reserved(self) -> np.ndarray: - return self.__segment_references > 0 + def _segment_reserved(self) -> np.ndarray: + return self._segment_references > 0 @property - def __free_points_in_total(self) -> int: - return self.total_capacity - np.sum(self.__segment_capacity[self.__segment_reserved]) + def _free_points_in_total(self) -> int: + return self.total_capacity - np.sum(self._segment_capacity[self._segment_reserved]) @property - def __free_points_at_end(self) -> int: - reserved_index = np.where(self.__segment_reserved) + def _free_points_at_end(self) -> int: + reserved_index = np.flatnonzero(self._segment_reserved) if reserved_index: - return self.total_capacity - np.sum(self.__segment_capacity[:reserved_index[-1]]) + return self.total_capacity - np.sum(self._segment_capacity[:reserved_index[-1]]) else: return self.total_capacity + @with_configuration_guard def upload(self, name: str, program: Loop, channels: List[ChannelID], markers: List[ChannelID], + voltage_transformation: List[Callable], force: bool=False) -> None: """Upload a program to the AWG. @@ -409,10 +573,12 @@ def upload(self, name: str, raise ValueError('Channel ID not specified') if len(markers) != self.num_markers: raise ValueError('Markers not specified') + if len(voltage_transformation) != self.num_channels: + raise ValueError('Wrong number of voltage transformations') # helper to restore previous state if upload is impossible to_restore = None - if name in self.__known_programs: + if name in self._known_programs: if force: # save old program to restore in on error to_restore = self.free_program(name) @@ -421,90 +587,141 @@ def upload(self, name: str, try: # parse to tabor program - tabor_program = TaborProgram(program, channels=tuple(channels)) - sample_rate = self.__device.sample_rate(self.__channels[0]) - voltage_amplitudes = (self.__device.amplitude(self.__channels[0]), - self.__device.amplitude(self.__channels[1])) - voltage_offsets = (self.__device.offset(self.__channels[0]), - self.__device.offset(self.__channels[1])) + tabor_program = TaborProgram(program, + channels=tuple(channels), + markers=markers, + device_properties=self._device.dev_properties) + sample_rate = self._device.sample_rate(self._channels[0]) + voltage_amplitudes = (self._device.amplitude(self._channels[0]), + self._device.amplitude(self._channels[1])) + voltage_offsets = (self._device.offset(self._channels[0]), + self._device.offset(self._channels[1])) segments, segment_lengths = tabor_program.sampled_segments(sample_rate=sample_rate, - voltage_amplitude=voltage_amplitudes, - voltage_offset=voltage_offsets) - segment_hashes = np.fromiter((hash(bytes(segment)) for segment in segments), count=len(segments), dtype=int) + voltage_amplitude=voltage_amplitudes, + voltage_offset=voltage_offsets, + voltage_transformation=voltage_transformation) + segment_hashes = np.fromiter((hash(segment) for segment in segments), count=len(segments), dtype=np.uint64) - known_waveforms = np.in1d(segment_hashes, self.__segment_hashes, assume_unique=True) + known_waveforms = np.in1d(segment_hashes, self._segment_hashes, assume_unique=True) to_upload_size = np.sum(segment_lengths[~known_waveforms] + 16) waveform_to_segment = np.full(len(segments), -1, dtype=int) - waveform_to_segment[known_waveforms] = np.where( - np.in1d(self.__segment_hashes, segment_hashes[known_waveforms])) + waveform_to_segment[known_waveforms] = np.flatnonzero( + np.in1d(self._segment_hashes, segment_hashes[known_waveforms])) + + to_amend = ~known_waveforms + to_insert = [] - if name not in self.__known_programs: - if self.__free_points_in_total < to_upload_size: + if name not in self._known_programs: + if self._free_points_in_total < to_upload_size: raise MemoryError('Not enough free memory') - if self.__free_points_at_end < to_upload_size: - reserved_indices = np.where(self.__segment_reserved) + if self._free_points_at_end < to_upload_size: + reserved_indices = np.flatnonzero(self._segment_reserved) if len(reserved_indices) == 0: raise MemoryError('Fragmentation does not allow upload.') last_reserved = reserved_indices[-1] if reserved_indices else 0 - free_segments = np.where(self.__segment_references[:last_reserved] == 0)[ - np.argsort(self.__segment_capacity[:last_reserved])[::-1]] + free_segments = np.flatnonzero(self._segment_references[:last_reserved] == 0)[ + np.argsort(self._segment_capacity[:last_reserved])[::-1]] - to_amend = ~known_waveforms - to_insert = [] for wf_index in np.argsort(segment_lengths[~known_waveforms])[::-1]: - if segment_lengths[wf_index] <= self.__segment_capacity[free_segments[0]]: + if segment_lengths[wf_index] <= self._segment_capacity[free_segments[0]]: to_insert.append((wf_index, free_segments[0])) free_segments = free_segments[1:] to_amend[wf_index] = False - if np.sum(segment_lengths[to_amend] + 16) > self.__free_points_at_end: + if np.sum(segment_lengths[to_amend] + 16) > self._free_points_at_end: raise MemoryError('Fragmentation does not allow upload.') except: if to_restore: - self.__known_programs[name] = to_restore - self.__segment_reserved[to_restore.segment_indices] += 1 + self._known_programs[name] = to_restore + self._segment_reserved[to_restore.segment_indices] += 1 raise - self.__segment_references[waveform_to_segment[waveform_to_segment >= 0]] += 1 + self._segment_references[waveform_to_segment[waveform_to_segment >= 0]] += 1 if to_insert: # as we have to insert waveforms the waveforms behind the last referenced are discarded self.cleanup() for wf_index, segment_index in to_insert: - self.__upload_segment(segment_index, segments[wf_index]) + self._upload_segment(segment_index, segments[wf_index]) waveform_to_segment[wf_index] = segment_index if np.any(to_amend): segments_to_amend = segments[to_amend] - self.__amend_segments(segments_to_amend) - waveform_to_segment[to_amend] = np.arange(len(self.__segment_capacity)-np.sum(to_amend), - len(self.__segment_capacity), dtype=int) + self._amend_segments(segments_to_amend) + waveform_to_segment[to_amend] = np.arange(len(self._segment_capacity) - np.sum(to_amend), + len(self._segment_capacity), dtype=int) + 1 + + self._known_programs[name] = TaborProgramMemory(segment_indices=waveform_to_segment, + program=tabor_program) + + @with_configuration_guard + def _upload_segment(self, segment_index: int, segment: TaborSegment) -> None: + if self._segment_references[segment_index] > 0: + raise ValueError('Reference count not zero') + if segment.num_points > self._segment_capacity[segment_index]: + raise ValueError('Cannot upload segment here.') + + + self._device.send_cmd(':TRAC:DEF {}, {}'.format(segment_index, segment.num_points)) + self._segment_lengths[segment_index] = segment.num_points + + self._device.send_cmd(':TRAC:SEL {}'.format(segment_index)) + + self._device.send_cmd('TRAC:MODE COMB') + wf_data = segment.get_as_binary() + + self._device.send_binary_data(pref=':TRAC:DATA', bin_dat=wf_data) + self._segment_references[segment_index] = 1 + self._segment_hashes[segment_index] = hash(segment) + + @with_configuration_guard + def _amend_segments(self, segments: List[TaborSegment]) -> None: + new_lengths = np.asarray([s.num_points for s in segments], dtype=np.uint32) + + wf_data = make_combined_wave(segments) + trac_len = len(wf_data) // 2 + + segment_index = len(self._segment_capacity) + 1 + self._device.send_cmd(':TRAC:DEF {}, {}'.format(segment_index, trac_len)) + self._device.send_cmd(':TRAC:SEL {}'.format(segment_index)) + self._device.send_cmd('TRAC:MODE COMB') + self._device.send_binary_data(pref=':TRAC:DATA', bin_dat=wf_data) + + old_to_update = np.count_nonzero(self._segment_capacity != self._segment_lengths and self._segment_reserved) + segment_capacity = np.concatenate((self._segment_capacity, new_lengths)) + segment_lengths = np.concatenate((self._segment_lengths, new_lengths)) + segment_references = np.concatenate((self._segment_references, np.ones(len(segments), dtype=int))) + segment_hashes = np.concatenate((self._segment_hashes, [hash(s) for s in segments])) + if len(segments) < old_to_update: + for i, segment in enumerate(segments): + current_segment = segment_index + i + self._device.send_cmd(':TRAC:DEF {}, {}'.format(current_segment, segment.num_points)) + else: + # flush the capacity + self._device.download_segment_lengths(segment_capacity) - self.__known_programs[name] = TaborProgramMemory(segment_index=waveform_to_segment, - ) - raise NotImplementedError() + # update non fitting lengths + for i in np.flatnonzero(np.logical_and(segment_capacity != segment_lengths, segment_references > 0)): + self._device.send_cmd(':TRAC:DEF {},{}'.format(i, segment_lengths[i])) - def __upload_segment(self, segment_index: int, segment: TaborSegment) -> None: - self.__segment_references[segment_index] = 1 - self.__segment_hashes[segment_index] - raise NotImplementedError() - - def __amend_segments(self, segments: List[TaborSegment]) -> None: - raise NotImplementedError() + self._segment_capacity = segment_capacity + self._segment_lengths = segment_lengths + self._segment_hashes = segment_hashes + self._segment_references = segment_references def cleanup(self) -> None: """Discard all segments after the last which is still referenced""" - reserved_indices = np.where(self.__segment_references > 0) + reserved_indices = np.flatnonzero(self._segment_references > 0) new_end = reserved_indices[-1]+1 if reserved_indices else 0 - self.__segment_lengths = self.__segment_lengths[:new_end] - self.__segment_capacity = self.__segment_capacity[:new_end] - self.__segment_hashes = self.__segment_capacity[:new_end] - self.__segment_references = self.__segment_capacity[:new_end] + self._segment_lengths = self._segment_lengths[:new_end] + self._segment_capacity = self._segment_capacity[:new_end] + self._segment_hashes = self._segment_capacity[:new_end] + self._segment_references = self._segment_capacity[:new_end] def remove(self, name: str) -> None: """Remove a program from the AWG. @@ -516,17 +733,75 @@ def remove(self, name: str) -> None: """ self.free_program(name) + def set_marker_state(self, marker, active) -> None: + command_string = ':INST:SEL {}; :SOUR:MARK:SEL {}; :SOUR:MARK:SOUR USER; :SOUR:MARK:STAT {}'.format( + self._channels[0], + marker+1, + 'ON' if active else 'OFF') + self._device.send_cmd(command_string) + + def set_channel_state(self, channel, active) -> None: + command_string = ':INST:SEL {}; :OUTP {}'.format(self._channels[channel], 'ON' if active else 'OFF') + self._device.send_cmd(command_string) + + @with_configuration_guard def arm(self, name: str) -> None: - raise NotImplementedError() + if self._current_program == name: + self._device.send_cmd('SEQ:SEL 1') + return + + waveform_to_segment, program = self._known_programs[name] + + sequencer_tables = program.get_sequencer_tables() + sequencer_tables = [[(rep_count, waveform_to_segment[wf_index], jump_flag) + for (rep_count, wf_index, jump_flag) in sequencer_table] + for sequencer_table in sequencer_tables] + sequencer_tables = [self._idle_sequence_table] + sequencer_tables + + advanced_sequencer_table = program.get_advanced_sequencer_table() + advanced_sequencer_table = [(rep_count, seq_no+1, jump_flag) + for rep_count, seq_no, jump_flag in advanced_sequencer_table] + advanced_sequencer_table = [(1, 1, 1)] + advanced_sequencer_table + while len(advanced_sequencer_table) < self._device.dev_properties['min_aseq_len']: + advanced_sequencer_table.append((1, 1, 0)) + + self.set_marker_state(0, False) + self.set_marker_state(1, False) + + self.set_channel_state(0, False) + self.set_channel_state(1, False) + + self._device.abort() + + #download all sequence tables + for i, sequencer_table in enumerate(sequencer_tables): + if i >= len(self._sequencer_tables) or self._sequencer_tables[i] != sequencer_table: + self._device.send_cmd('SEQ:SEL {}'.format(i+1)) + self._device.download_sequencer_table(sequencer_table) + self._sequencer_tables = sequencer_tables + self._device.send_cmd('SEQ:SEL 1') + + self._device.download_adv_seq_table(advanced_sequencer_table) + self._advanced_sequence_table = advanced_sequencer_table + + # this will set the DC voltage to the first value of the idle waveform + self._device.enable() + self._current_program = name + + def run_current_program(self): + if self._current_program: + self._device.send_cmd(':TRIG') + else: + raise RuntimeError('No program active') @property def programs(self) -> Set[str]: """The set of program names that can currently be executed on the hardware AWG.""" - raise set(program.name for program in self.__known_programs.keys()) + raise set(program.name for program in self._known_programs.keys()) @property def sample_rate(self) -> float: - return self.__device.sample_rate(self.__channels[0]) + return self._device.sample_rate(self._channels[0]) @property def num_channels(self) -> int: @@ -536,6 +811,36 @@ def num_channels(self) -> int: def num_markers(self) -> int: return 2 + def configuration_guard(self): + return self.ConfigurationGuard(self) + + def _enter_config_mode(self): + if self._is_in_config_mode is False: + self.set_marker_state(0, False) + self.set_marker_state(1, False) + + self.set_channel_state(0, False) + self.set_channel_state(1, False) + + self._device.send_cmd(':SOUR:FUNC:MODE FIX') + self._is_in_config_mode = True + + def _exit_config_mode(self): + if self._current_program: + _, program = self._known_programs[self._current_program] + + self._device.send_cmd(':SOUR:FUNC:MODE ASEQ') + + self.set_marker_state(0, program.markers[0] is not None) + self.set_marker_state(1, program.markers[1] is not None) + + self.set_channel_state(0, program.channels[0] is not None) + self.set_channel_state(1, program.channels[1] is not None) + + self._is_in_config_mode = False + + + class TaborException(Exception): pass diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index a1ddd05b1..f5bb2ea8d 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -1,4 +1,6 @@ -from typing import NamedTuple, Any, Set, Callable, Dict, Tuple +from typing import NamedTuple, Any, Set, Callable, Dict, Tuple, Union +import itertools +from collections import defaultdict, deque from ctypes import c_int64 as MutableInt @@ -11,20 +13,46 @@ import numpy as np -__all__ = ['PlaybackChannel', 'HardwareSetup'] +__all__ = ['PlaybackChannel', 'MarkerChannel', 'HardwareSetup'] -PlaybackChannel = NamedTuple('PlaybackChannel', [('awg', AWG), - ('channel_on_awg', Any), - ('voltage_transformation', Callable[[np.ndarray], np.ndarray])]) -PlaybackChannel.__new__.__defaults__ = (lambda v: v,) -PlaybackChannel.__doc__ += ': Properties of an actual hardware channel' -PlaybackChannel.awg.__doc__ = 'The AWG the channel is defined on' -PlaybackChannel.channel_on_awg.__doc__ = 'The channel\'s index(starting with 0) on the AWG.' -PlaybackChannel.voltage_transformation.__doc__ = \ - 'A transformation that is applied to the pulses on the channel.\ - One use case is to scale up the voltage if an amplifier is inserted.' -PlaybackChannel.__hash__ = lambda pbc: hash((id(pbc.awg), pbc.channel_on_awg)) +class _SingleChannel: + """An actual hardware channel""" + def __init__(self, awg: AWG, channel_on_awg: int): + self.awg = awg + """The AWG the channel is defined on""" + + self.channel_on_awg = channel_on_awg + """The channel's index(starting with 0) on the AWG.""" + + def __hash__(self): + return hash((id(self.awg), self.channel_on_awg, type(self))) + + def __eq__(self, other): + return hash(self) == hash(other) + + +class PlaybackChannel(_SingleChannel): + """A hardware channel that is not a marker""" + def __init__(self, awg: AWG, channel_on_awg: int, + voltage_transformation: Callable[[np.ndarray], np.ndarray]=lambda x: x): + if channel_on_awg >= awg.num_channels: + raise ValueError('Can not create PlayBack channel {}. AWG only has {} channels'.format(channel_on_awg, + awg.num_channels)) + super().__init__(awg=awg, channel_on_awg=channel_on_awg) + + self.voltage_transformation = voltage_transformation + """A transformation that is applied to the pulses on the channel. One use case is to scale up the voltage if an + amplifier is inserted.""" + + +class MarkerChannel(_SingleChannel): + """A hardware channel that can only take two values""" + def __init__(self, awg: AWG, channel_on_awg: int): + if channel_on_awg >= awg.num_markers: + raise ValueError('Can not create MarkerBack channel {}. AWG only has {} channels'.format(channel_on_awg, + awg.num_markers)) + super().__init__(awg=awg, channel_on_awg=channel_on_awg) RegisteredProgram = NamedTuple('RegisteredProgram', [('program', MultiChannelProgram), @@ -41,73 +69,110 @@ class HardwareSetup: extracts the measurement windows(with absolute times) and hands them over to the DACs which will do further processing.""" def __init__(self): - self.__dacs = [] + self._dacs = [] - self.__channel_map = dict() # type: Dict[ChannelID, Set[PlaybackChannel]] + self._channel_map = dict() # type: Dict[ChannelID, Set[SingleChannel]] - self.__registered_programs = dict() # type: Dict[str, RegisteredProgram] + self._registered_programs = dict() # type: Dict[str, RegisteredProgram] - def register_program(self, name: str, instruction_block, run_callback=lambda: None, update=False): + def register_program(self, name: str, instruction_block, run_callback=lambda: None, update=False) -> None: if not callable(run_callback): raise TypeError('The provided run_callback is not callable') mcp = MultiChannelProgram(instruction_block) - measurement_windows = dict() - + temp_measurement_windows = defaultdict(deque) for program in mcp.programs.values(): - program.get_measurement_windows(measurement_windows=measurement_windows) + for mw_name, begins_lengths in program.get_measurement_windows().items(): + temp_measurement_windows[mw_name].append(begins_lengths) - for mw_name, begin_length_list in measurement_windows.items(): - measurement_windows[mw_name] = sorted(set(begin_length_list)) + measurement_windows = dict() + while temp_measurement_windows: + mw_name, begins_lengths_deque = temp_measurement_windows.popitem() + measurement_windows[mw_name] = ( + np.concatenate(tuple(begins for begins, _ in begins_lengths_deque)), + np.concatenate(tuple(lengths for _, lengths in begins_lengths_deque)) + ) handled_awgs = set() - for channels, program in mcp.programs: - awgs_to_upload_to = dict() + for channels, program in mcp.programs.items(): + awgs_to_channel_info = dict() + + def get_default_info(awg): + return ([None] * awg.num_channels, + [None] * awg.num_channels, + [None] * awg.num_markers) + for channel_id in channels: - if channel_id in channels: - pbc = self.__channel_map[channel_id] - awgs_to_upload_to.get(pbc.awg, [None]*pbc.awg.num_channels)[pbc.channel_on_awg] = channel_id + for single_channel in self._channel_map[channel_id]: + playback_ids, voltage_trafos, marker_ids = \ + awgs_to_channel_info.setdefault(single_channel.awg, get_default_info(single_channel.awg)) + + if isinstance(single_channel, PlaybackChannel): + playback_ids[single_channel.channel_on_awg] = channel_id + voltage_trafos[single_channel.channel_on_awg] = single_channel.voltage_transformation + elif isinstance(single_channel, MarkerChannel): + marker_ids[single_channel.channel_on_awg] = channel_id - for awg, channel_ids in awgs_to_upload_to.items(): + for awg, (playback_ids, voltage_trafos, marker_ids) in awgs_to_channel_info.items(): if awg in handled_awgs: raise ValueError('AWG has two programs') else: handled_awgs.add(awg) - awg.upload(name, program=program, channels=channel_ids, force=update) - - for dac in self.__dacs: + awg.upload(name, + program=program, + channels=tuple(playback_ids), + markers=tuple(marker_ids), + force=update, + voltage_transformation=tuple(voltage_trafos)) + + for dac in self._dacs: dac.register_measurement_windows(name, measurement_windows) - self.__registered_programs[name] = RegisteredProgram(program=mcp, - measurement_windows=measurement_windows, - run_callback=run_callback, - awgs_to_upload_to= - set(awg for awg, _ in awgs_to_upload_to.items())) + self._registered_programs[name] = RegisteredProgram(program=mcp, + measurement_windows=measurement_windows, + run_callback=run_callback, + awgs_to_upload_to=handled_awgs) - def arm_program(self, name): + def arm_program(self, name) -> None: """Assert program is in memory. Hardware will wait for trigger event""" - for awg in self.__registered_programs[name].awgs_to_upload_to: + if name not in self._registered_programs: + raise KeyError('{} is not a registered program'.format(name)) + for awg in self._registered_programs[name].awgs_to_upload_to: awg.arm(name) - for dac in self.__dacs: + for dac in self._dacs: dac.arm_program(name) - def run_program(self, name): + def run_program(self, name) -> None: """Calls arm program and starts it using the run callback""" self.arm_program(name) - self.__registered_programs[name].run_callback() + self._registered_programs[name].run_callback() + + def set_channel(self, identifier: ChannelID, single_channel: Union[PlaybackChannel, MarkerChannel]) -> None: + for ch_id, channel_set in self._channel_map.items(): + if single_channel in channel_set: + raise ValueError('Channel already registered as {} for channel {}'.format( + type(self._channel_map[ch_id]).__name__, ch_id)) + + if isinstance(single_channel, (PlaybackChannel, MarkerChannel)): + self._channel_map.setdefault(identifier, set()).add(single_channel) + else: + raise ValueError('Channel must be either a playback or a marker channel') + + def rm_channel(self, identifier: ChannelID) -> None: + self._playback_channel_map.pop(identifier) - def set_channel(self, identifier: ChannelID, playback_channel: PlaybackChannel): - for ch_id, pbc_set in self.__channel_map.items(): - if playback_channel in pbc_set: - raise ValueError('Channel already registered as playback channel for channel {}'.format(ch_id)) - self.__channel_map[identifier] = playback_channel + def registered_channels(self) -> Set[PlaybackChannel]: + return self._channel_map.copy() - def rm_channel(self, identifier: ChannelID): - self.__channel_map.pop(identifier) + def register_dac(self, dac): + if dac in self._dacs: + raise ValueError('DAC already known {}'.format(str(dac))) + self._dacs.append(dac) - def registered_playback_channels(self) -> Set[PlaybackChannel]: - return set(pbc for pbc_set in self.__channel_map.values() for pbc in pbc_set) + @property + def registered_programs(self) -> Dict: + return self._registered_programs diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py index 0e680b7c2..b004274a7 100644 --- a/tests/hardware/program_tests.py +++ b/tests/hardware/program_tests.py @@ -34,6 +34,66 @@ def __call__(self): return self.generate_multi_channel_waveform() +def get_two_chan_test_block(wfg=WaveformGenerator(2)): + generate_waveform = wfg.generate_single_channel_waveform + generate_multi_channel_waveform = wfg.generate_multi_channel_waveform + + loop_block11 = InstructionBlock() + loop_block11.add_instruction_exec(generate_multi_channel_waveform()) + + loop_block1 = InstructionBlock() + loop_block1.add_instruction_repj(5, ImmutableInstructionBlock(loop_block11)) + + loop_block21 = InstructionBlock() + loop_block21.add_instruction_exec(generate_multi_channel_waveform()) + loop_block21.add_instruction_exec(generate_multi_channel_waveform()) + + loop_block2 = InstructionBlock() + loop_block2.add_instruction_repj(2, ImmutableInstructionBlock(loop_block21)) + loop_block2.add_instruction_exec(generate_multi_channel_waveform()) + + loop_block3 = InstructionBlock() + loop_block3.add_instruction_exec(generate_multi_channel_waveform()) + loop_block3.add_instruction_exec(generate_multi_channel_waveform()) + + loop_block411 = InstructionBlock() + loop_block411.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) + loop_block412 = InstructionBlock() + loop_block412.add_instruction_exec(MultiChannelWaveform([generate_waveform('A')])) + + loop_block41 = InstructionBlock() + loop_block41.add_instruction_repj(7, ImmutableInstructionBlock(loop_block411)) + loop_block41.add_instruction_repj(8, ImmutableInstructionBlock(loop_block412)) + + loop_block421 = InstructionBlock() + loop_block421.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) + loop_block422 = InstructionBlock() + loop_block422.add_instruction_exec(MultiChannelWaveform([generate_waveform('B')])) + + loop_block42 = InstructionBlock() + loop_block42.add_instruction_repj(10, ImmutableInstructionBlock(loop_block421)) + loop_block42.add_instruction_repj(11, ImmutableInstructionBlock(loop_block422)) + + chan_block4A = InstructionBlock() + chan_block4A.add_instruction_repj(6, ImmutableInstructionBlock(loop_block41)) + + chan_block4B = InstructionBlock() + chan_block4B.add_instruction_repj(9, ImmutableInstructionBlock(loop_block42)) + + loop_block4 = InstructionBlock() + loop_block4.add_instruction_chan({frozenset('A'): ImmutableInstructionBlock(chan_block4A), + frozenset('B'): ImmutableInstructionBlock(chan_block4B)}) + + root_block = InstructionBlock() + root_block.add_instruction_exec(generate_multi_channel_waveform()) + root_block.add_instruction_repj(10, ImmutableInstructionBlock(loop_block1)) + root_block.add_instruction_repj(17, ImmutableInstructionBlock(loop_block2)) + root_block.add_instruction_repj(3, ImmutableInstructionBlock(loop_block3)) + root_block.add_instruction_repj(4, ImmutableInstructionBlock(loop_block4)) + + return root_block + + class LoopTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index f97a56e4f..936014146 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -30,10 +30,10 @@ def __init__(self, *args, **kwargs): @property def waveform_data_generator(self): - return itertools.cycle([np.linspace(-0.5, 0.5, num=4048), - np.concatenate((np.linspace(-0.5, 0.5, num=2024), - np.linspace(0.5, -0.5, num=2024))), - -0.5*np.cos(np.linspace(0, 2*np.pi, num=4048))]) + return itertools.cycle([np.linspace(-0.5, 0.5, num=192), + np.concatenate((np.linspace(-0.5, 0.5, num=96), + np.linspace(0.5, -0.5, num=96))), + -0.5*np.cos(np.linspace(0, 2*np.pi, num=192))]) @property def root_loop(self): @@ -43,9 +43,9 @@ def root_loop(self): def test_init(self): prog = MultiChannelProgram(MultiChannelTests().root_block) - TaborProgram(prog, self.instr_props, ('A', None), (None, None)) - with self.assertRaises(TaborException): - TaborProgram(prog, self.instr_props, ('A', 'B'), (None, None)) + TaborProgram(prog['A'], self.instr_props, ('A', None), (None, None)) + with self.assertRaises(KeyError): + TaborProgram(prog['A'], self.instr_props, ('A', 'B'), (None, None)) @unittest.skip def test_setup_single_waveform_mode(self): @@ -54,41 +54,46 @@ def test_setup_single_waveform_mode(self): def test_sampled_segments(self): def my_gen(gen): - alternating_on_off = itertools.cycle((np.ones(4048), np.zeros(4048))) + alternating_on_off = itertools.cycle((np.ones(192), np.zeros(192))) chan_gen = gen while True: for _ in range(2): yield next(chan_gen) yield next(alternating_on_off) - yield np.zeros(4048) + yield np.zeros(192) - sample_rate = 8096 + sample_rate = 10**9 with self.assertRaises(TaborException): root_loop = LoopTests.get_test_loop(WaveformGenerator( waveform_data_generator=my_gen(self.waveform_data_generator), - duration_generator=itertools.repeat(1e-9), + duration_generator=itertools.repeat(12), num_channels=4)) mcp = MultiChannelProgram(InstructionBlock(), tuple()) mcp.programs[frozenset(('A', 'B', 'C', 'D'))] = root_loop - TaborProgram(mcp, self.instr_props, ('A', 'B'), (None, None)).sampled_segments(8000, (1., 1.), (0, 0)) + TaborProgram(root_loop, self.instr_props, ('A', 'B'), (None, None)).sampled_segments(8000, + (1., 1.), + (0, 0), + (lambda x: x, lambda x: x)) root_loop = LoopTests.get_test_loop(WaveformGenerator( waveform_data_generator=my_gen(self.waveform_data_generator), - duration_generator=itertools.repeat(0.5), + duration_generator=itertools.repeat(192), num_channels=4)) mcp = MultiChannelProgram(InstructionBlock(), tuple()) mcp.programs[frozenset(('A', 'B', 'C', 'D'))] = root_loop - prog = TaborProgram(mcp, self.instr_props, ('A', 'B'), (None, None)) + prog = TaborProgram(root_loop, self.instr_props, ('A', 'B'), (None, None)) - sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0)) + sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0), + (lambda x: x, lambda x: x)) self.assertEqual(len(sampled), 3) - prog = TaborProgram(mcp, self.instr_props, ('A', 'B'), ('C', None)) - sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0)) + prog = TaborProgram(root_loop, self.instr_props, ('A', 'B'), ('C', None)) + sampled, sampled_length = prog.sampled_segments(sample_rate, (1., 1.), (0, 0), + (lambda x: x, lambda x: x)) self.assertEqual(len(sampled), 6) iteroe = my_gen(self.waveform_data_generator) @@ -96,10 +101,11 @@ def my_gen(gen): data = [next(iteroe) for _ in range(4)] data = (voltage_to_uint16(data[0], 1., 0., 14), voltage_to_uint16(data[1], 1., 0., 14), data[2], data[3]) if i % 2 == 0: - self.assertTrue(np.all(sampled_seg[1] >> 14 == np.ones(4048, dtype=np.uint16))) + self.assertTrue(np.all(sampled_seg[0] >> 14 == np.ones(192, dtype=np.uint16))) else: - self.assertTrue(np.all(sampled_seg[1] >> 14 == np.zeros(4048, dtype=np.uint16))) - self.assertTrue(np.all(sampled_seg[0] >> 15 == np.zeros(4048, dtype=np.uint16))) + self.assertTrue(np.all(sampled_seg[0] >> 14 == np.zeros(192, dtype=np.uint16))) + self.assertTrue(np.all(sampled_seg[0] >> 15 == np.zeros(192, dtype=np.uint16))) + self.assertTrue(np.all(sampled_seg[1] >> 15 == np.zeros(192, dtype=np.uint16))) self.assertTrue(np.all(sampled_seg[0] << 2 == data[0] << 2)) self.assertTrue(np.all(sampled_seg[1] << 2 == data[1] << 2)) @@ -132,6 +138,7 @@ def test_amplitude(self): class TaborChannelPairTests(unittest.TestCase): + @unittest.skipIf(instrument is None, "Instrument not present") def test_copy(self): channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) with self.assertRaises(NotImplementedError): From 5b62448739b2e0c44cbb5b78a398554e4a44b764 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:51:27 +0100 Subject: [PATCH 037/116] Add files I used while testing the setup to use in documentation later on. Tests are convenient for debugging --- temp_examples/meas_clock_diffrence.ipynb | 626 +++++++++++++++++++++++ tests/hardware/tabor_clock_tests.py | 165 ++++++ tests/hardware/tabor_exex_test.py | 136 +++++ 3 files changed, 927 insertions(+) create mode 100644 temp_examples/meas_clock_diffrence.ipynb create mode 100644 tests/hardware/tabor_clock_tests.py create mode 100644 tests/hardware/tabor_exex_test.py diff --git a/temp_examples/meas_clock_diffrence.ipynb b/temp_examples/meas_clock_diffrence.ipynb new file mode 100644 index 000000000..5bf9bb1d1 --- /dev/null +++ b/temp_examples/meas_clock_diffrence.ipynb @@ -0,0 +1,626 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "with_alazar = True\n", + "\n", + "\n", + "def get_pulse():\n", + " from qctoolkit.pulses import TablePulseTemplate as TPT, SequencePulseTemplate as SPT, \\\n", + " RepetitionPulseTemplate as RPT, FunctionPulseTemplate as FPT, MultiChannelPulseTemplate as MPT\n", + "\n", + " sine = FPT('U*sin(2*pi*t/tau)', 'tau', channel='out')\n", + " marker_on = FPT('1', 'tau', channel='trigger')\n", + "\n", + " multi = MPT([sine, marker_on], {'tau', 'U'})\n", + " multi.atomicity = True\n", + "\n", + " assert sine.defined_channels == {'out'}\n", + " assert multi.defined_channels == {'out', 'trigger'}\n", + "\n", + " sine.add_measurement_declaration('meas', 0, 'tau')\n", + "\n", + " base = SPT([(multi, dict(tau='tau', U='U'), dict(meas='A')),\n", + " (multi, dict(tau='tau', U='U'), dict(meas='A')),\n", + " (multi, dict(tau='tau', U='U'), dict(meas='A'))], {'tau', 'U'})\n", + "\n", + " repeated = RPT(base, 'n')\n", + "\n", + " root = SPT([repeated, repeated, repeated], {'tau', 'n', 'U'})\n", + "\n", + " assert root.defined_channels == {'out', 'trigger'}\n", + "\n", + " return root\n", + "\n", + "\n", + "def get_alazar_config():\n", + " from atsaverage import alazar\n", + " from atsaverage.config import ScanlineConfiguration, CaptureClockConfiguration, EngineTriggerConfiguration,\\\n", + " TRIGInputConfiguration, InputConfiguration\n", + " \n", + " r = 2.5\n", + " rid = alazar.TriggerRangeID.etr_2V5\n", + "\n", + " trig_level = int((r + 0.05) / (2*r) * 255)\n", + " assert 0 <= trig_level < 256\n", + "\n", + " config = ScanlineConfiguration()\n", + " config.triggerInputConfiguration = TRIGInputConfiguration(triggerRange=rid)\n", + " config.triggerConfiguration = EngineTriggerConfiguration(triggerOperation=alazar.TriggerOperation.J,\n", + " triggerEngine1=alazar.TriggerEngine.J,\n", + " triggerSource1=alazar.TriggerSource.external,\n", + " triggerSlope1=alazar.TriggerSlope.positive,\n", + " triggerLevel1=trig_level,\n", + " triggerEngine2=alazar.TriggerEngine.K,\n", + " triggerSource2=alazar.TriggerSource.disable,\n", + " triggerSlope2=alazar.TriggerSlope.positive,\n", + " triggerLevel2=trig_level)\n", + " config.captureClockConfiguration = CaptureClockConfiguration(source=alazar.CaptureClockType.internal_clock,\n", + " samplerate=alazar.SampleRateID.rate_100MSPS)\n", + " config.inputConfiguration = 4*[InputConfiguration(input_range=alazar.InputRangeID.range_1_V)]\n", + " config.totalRecordSize = 0\n", + " config.aimedBufferSize = 10*config.aimedBufferSize\n", + "\n", + " assert config.totalRecordSize == 0\n", + "\n", + " return config\n", + "\n", + "\n", + "def get_operations():\n", + " from atsaverage.operations import Downsample, RepAverage\n", + "\n", + " return [RepAverage(identifier='REP_A', maskID='A'),\n", + " Downsample(identifier='raw', maskID='D')]\n", + "\n", + "\n", + "def get_window(card):\n", + " from atsaverage.gui import ThreadedStatusWindow\n", + " window = ThreadedStatusWindow(card)\n", + " window.start()\n", + " return window\n", + "\n", + "\n", + "def exec_test():\n", + " import time\n", + " import numpy as np\n", + "\n", + " t = []\n", + " names = []\n", + "\n", + " def tic(name):\n", + " t.append(time.time())\n", + " names.append(name)\n", + "\n", + " from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation\n", + " tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR', reset=True)\n", + "\n", + " tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB')\n", + " tawg.paranoia_level = 2\n", + "\n", + " # warnings.simplefilter('error', Warning)\n", + "\n", + " from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel\n", + " hardware_setup = HardwareSetup()\n", + "\n", + " hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0))\n", + " hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1))\n", + " hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair, 0))\n", + " hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair, 1))\n", + "\n", + " if with_alazar:\n", + " from qctoolkit.hardware.dacs.alazar import AlazarCard\n", + " import atsaverage.server\n", + "\n", + " if not atsaverage.server.Server.default_instance.running:\n", + " atsaverage.server.Server.default_instance.start(key=b'guest')\n", + "\n", + " import atsaverage.core\n", + "\n", + " alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1))\n", + " alazar.register_mask_for_channel('A', 0)\n", + " alazar.config = get_alazar_config()\n", + "\n", + " alazar.register_operations('test', get_operations())\n", + "\n", + " window = get_window(atsaverage.core.getLocalCard(1, 1))\n", + "\n", + " hardware_setup.register_dac(alazar)\n", + "\n", + " repeated = get_pulse()\n", + "\n", + " from qctoolkit.pulses.sequencing import Sequencer\n", + "\n", + " tic('init')\n", + " sequencer = Sequencer()\n", + " sequencer.push(repeated,\n", + " parameters=dict(n=12800, tau=1920, U=0.5),\n", + " channel_mapping={'out': 'TABOR_A', 'trigger': 'TABOR_A_MARKER'},\n", + " window_mapping=dict(A='A'))\n", + " instruction_block = sequencer.build()\n", + "\n", + " tic('sequence')\n", + "\n", + " hardware_setup.register_program('test', instruction_block)\n", + "\n", + " tic('register')\n", + "\n", + " if with_alazar:\n", + " from atsaverage.masks import PeriodicMask\n", + " m = PeriodicMask()\n", + " m.identifier = 'D'\n", + " m.begin = 0\n", + " m.end = 1\n", + " m.period = 1\n", + " m.channel = 0\n", + " alazar._registered_programs['test'].masks.append(m)\n", + " \n", + " m2 = PeriodicMask()\n", + " m2.identifier = 'K'\n", + " m2.begin = 0\n", + " m2.end = 192\n", + " m2.period = 192\n", + " m2.channel = 0\n", + " alazar._registered_programs['test'].masks.append(m2)\n", + "\n", + " tic('per_mask')\n", + "\n", + " hardware_setup.arm_program('test')\n", + "\n", + " tic('arm')\n", + "\n", + " for d, name in zip(np.diff(np.asarray(t)), names[1:]):\n", + " print(name, d)\n", + " \n", + " return tawg, tchannelpair, hardware_setup" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "initializing services... servertype=thread\n", + "daemon location string=localhost:49567\n", + "daemon sockets=[]\n", + "Registering 1 card(s)\n", + "Registered ATS9440 as PYRO:1_1_ATS9440@localhost:49567\n", + "\n", + "Starting pyro daemon\n", + "done\n", + "\n", + " Scanline configuration:\n", + " Total record size: 22118400\n", + " Sample rate: 100000000\n", + " Total time: 221184 µs\n", + "\n", + " Buffer size: 22118400\n", + " Time per buffer: 221184 µs\n", + " Number of buffers: 1\n", + "\n", + " Recording Channels: A\n", + " Store raw data: False\n", + " \n", + "sequence 0.00200009346008\n", + "register 0.330168485641\n", + "per_mask 0.0\n", + "arm 0.79404759407\n" + ] + } + ], + "source": [ + "tawg, tchannelpair, hardware_setup = exec_test()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#hardware_setup.arm_program('test')\n", + "tawg.send_cmd(':TRIG')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import atsaverage.core\n", + "card = atsaverage.core.getLocalCard(1, 1)\n", + "scanline = card.extractNextScanline()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "raw = scanline.operationResults['raw']\n", + "def volt(x):\n", + " return x.getAsVoltage(atsaverage.alazar.InputRangeID.range_1_V)\n", + "rep = scanline.operationResults['REP_A']" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "raw_volt = volt(raw)\n", + "rep_volt = volt(rep)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztvXl4ZFd55/852tfWvvUqqTf36q3xEsxqQ8wSzGLAQBIn\nIUPCJM+EyUwC/DKBBzJZyDyTZDIhQwiQYZKAzRLAEIPxAmZ129122+69tXVrb0mlvVtrnd8f771S\nSZZaKtVdzq06n+fRU9KtW/e+XX3v/Z7zbkdprbFYLBaLxSUrbAMsFovFYhZWGCwWi8WyBCsMFovF\nYlmCFQaLxWKxLMEKg8VisViWYIXBYrFYLEuwwmCxWCyWJVhhsFgsFssSrDBYLBaLZQk5YRuwEaqr\nq3VjY2PYZlgsFkukOH78+KDWumat/SIpDI2NjRw7dixsMywWiyVSKKUurmc/60qyWCwWyxKsMFgs\nFotlCVYYLBaLxbIEKwwWi8ViWYIVBovFY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ptmr\ntZ7XWt+AjMZvAa4L2aQVUUq9GbistT4eti1JcIfW+ibgDcDvKKVemfimQddCDuKu/T9a6xuBScQV\ns4BBti7gxJPeAnx1+XtB2JspwtANJK7WvdXZZiL9SqkGAOf1srM99H+DUioXEYV/1Vr/m7PZWHtd\ntNYjwA8Qd0y5UipnBZsW7HXeLwOGAjLx5cBblFIdwAOIO+l/GWorAFrrbuf1MvANRHhNvBa6gC6t\n9VHn768hQmGirYm8AXhWa93v/B2ovZkiDM8Au50sjzxkivZQyDatxkPA/c7v9yO+fHf7rzpZCLcB\nowlTS99RSing88AZrfVfRcDeGqVUufN7IRIPOYMIxL2r2Ov+O+4FnnBGZr6jtf6o1nqr1roRuTaf\n0Fq/z0RbAZRSxUqpUvd3xBd+EgOvBa11H9CplNrrbLoTOG2irct4D4tuJNeu4OwNI6gSUiDnjUgm\nTSvwR2Hb49j0ZaAXmEVGNu9HfMWPAxeAx4BKZ18FfNqx/0XgSMC23oFMX18ATjg/bzTY3sPAc469\nJ4GPOdubgaeBFmSanu9sL3D+bnHebw7pmng1i1lJRtrq2PW883PKvZ8MvhZuAI4518I3gQpTbXVs\nKEZmgGUJ2wK111Y+WywWi2UJmeJKslgsFss6scJgsVgsliVYYbBYLBbLEqwwWCwWi2UJVhgsFovF\nsgQrDBaLxWJZghUGi8VisSzBCoPFYrFYlvD/A6hRf/xIsxHyAAAAAElFTkSuQmCC\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "plt.plot(raw_volt[:100])\n", + "plt.plot(raw_volt[-700:])\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5529600.0" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(raw_volt) / 4" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "del scanline\n", + "del raw\n", + "del tawg\n", + "del hardware_setup\n", + "card.reset()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "raw_mean = np.mean(raw_volt)\n", + "raw_volt -= raw_mean " + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "fourier = np.fft.rfft(raw_volt[:last:10])" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "ampl = np.abs(fourier)\n", + "#freq = np.fft.fftfreq(len(ampl), 1e-8)\n", + "\n", + "#ixd = np.argsort(freq)\n", + "#ampl = ampl[ixd]\n", + "#freq = freq[ixd]" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "freq = np.linspace(0, 1e7/2, num=len(ampl))" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1.0000009722242476" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max_freq = freq[np.argmax(ampl)]\n", + "max_freq * 1920e-9" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY8AAAEJCAYAAABsc6siAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XucnFWd7/vPry/V93RVkk4M6UBCCGIAuUVAcUaEDQTG\nbTgz6uDMkQhs44zoOOPsPeLMPocZPb50zp4ZlJfKGRQkOCqgo5LtBmM24HgNEK7hnqYD6Q65dNKX\npO+33/njWZVUm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kPSTIq60N13he3dwMKw/TXgbcAbRDf2fdrdx83scmAF0dQvZwPnpcxz\nN2uTp1ZMn0VERKZiZo8STUdSD8y1wwu2fZZoMtYvEP3j/gLRZKzXT/I6k02KCkRT3JtZ8s7uK4ju\nGL+EaLLXTWb2K+Dy8Hgq5KsnCia/JJo8daeZLQj5X3L3X87kZ1bwEBE5Su5+AUR9HsBH3f2j6fKZ\n2TeBn05yLO2kqMAeM1vk7rvMbBHR1PYA1xHNY+ZAi5ltJ5pd2YAvufu/pqnnoclTzSw5eeqMgocu\nW4mI5FD4h5/0fxDN8TUxjwF3AC+6+79MOLwBSC4bvBa4P2zvAC4N5y8E3ko0CeNG4PrQisHMFpvZ\nAjOrM7OGkFZH1Dp5U10ypZaHiEhu/b8WrSvvREtFfxzAzE4gmpjwKqLp1D8CbE255PW3YWTWl4H7\nzOwG4HWi1UQhugR2l5ltJWptfNbd9xH1abwN+F0Uk+gF/k+iy1c/DmkVwPfcfbKFoaaliRFFRCRr\numwlIiJZU/AQEZGsKXiIiEjWFDxERCRrCh4iIpI1BQ8REcmagoeIiGRNwUNERLL2/wPKp+UCLfH/\n9AAAAABJRU5ErkJggg==\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "520833.610052\n" + ] + } + ], + "source": [ + "max_index = np.argmax(ampl)\n", + "\n", + "sel = slice(max_index-15, max_index+15, 1)\n", + "plt.plot(freq[sel], ampl[sel])\n", + "plt.show()\n", + "\n", + "max_freq_mean = np.sum(ampl[sel]*freq[sel]) / np.sum(ampl[sel])\n", + "\n", + "print(max_freq_mean, max_freq)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "172799530 172800000\n" + ] + }, + { + "data": { + "text/plain": [ + "0.99999791087962964" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "volt_l = np.flatnonzero(raw_volt > 0.05)\n", + "print(volt_l[-1], len(raw_volt))\n", + "#raw_volt = raw_volt[:volt_l[-1]]\n", + "\n", + "plt.plot(raw_volt[volt_l[-1]+108:volt_l[-1]+120])\n", + "last = volt_l[-1] + 109\n", + "\n", + "(volt_l[-1] + 109) / len(raw_volt)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztvXmUZFd95/n9xb5HbpGRe61ZkqpEaaEQYhMYsYi2jZhp\nRIvBtqbHtNqn7YYeu8ctTx+bM3h8ju1mTLuPaWYYlsE+bYQM7kZeGknIgAAjodK+VlXWmntEbrHv\nceeP925kZiiXWN57976M+zmnTmVGvsi4N+9793d/v/v7/i4xxqBQKBQKBcchugEKhUKhkAtlGBQK\nhUKxDWUYFAqFQrENZRgUCoVCsQ1lGBQKhUKxDWUYFAqFQrENZRgUCoVCsQ1lGBQKhUKxDWUYFAqF\nQrENl+gGdMLQ0BA7fPiw6GYoFAqFrXjmmWdWGGOx/a6zpWE4fPgwzp49K7oZCoVCYSuI6Gor16lQ\nkkKhUCi2oQyDQqFQKLahDINCoVAotqEMg0KhUCi2oQyDQqFQKLahDINCoVAotqEMg0KhUCi2oQyD\nQqGwnFShgr9+dg7VWl10U0zn++cSuJjMim5GW9hS4NbLbOTLWM9XcGQoKLopChM4v5yBy0E4GguJ\nboppMMbwWw89j++9lkCtznDPmUnRTTKN2bU8Pvn1s+gPePA3//odGI36RTepJZRhsBn/5pvP4wfn\nknjfDXF8+s5pvGkiKrpJpjC3nsd9X/0Z8uUaAMDtdODP/qdbcHqiT3DLzKFWZ/jiD2bw+e9dwFif\nDz/8tz8Hh4NEN8sU/uLJq/jeawkEPE588YcX8T/eO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ankA/5wxdrbjz+4Vw6BQ\nKBSK1uiVUJJCoVAoWkQZBoVCoVBsQxkGhUKhUGxDGQaFQqFQbEMZBoVCoVBsQxkGhUKhUGxDGQaF\nQqFQbEMZBoVCoVBs4/8HFVWd/I5zIwgAAAAASUVORK5CYII=\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "890998" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = raw_volt[:volt_l[-1] + 100:10]\n", + "\n", + "plt.plot(a[:100])\n", + "plt.show()\n", + "\n", + "np.sum(np.logical_and(a[:-2] < a[1:-1], a[1:-1] > a[2:]))" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'center': 520833.652549753, 'amplitude': 1226284.1411607806, 'sigma': 0.4398059725482273, 'c': 31488.238344030437}\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAaAAAAEWCAYAAAAgpUMxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsnXd8VUX2wL8nIRB6aGooElBAemgqYMGGKGJZu2vvu7Zd\nK24zq6ui8rN3RQV0VRYVC659URSRGjooTQk1lECABFLO74+ZBzeP95L3klfykvl+Pu/z3p07d+bc\ne+fdc2fmzDmiqjgcDofDEWuS4i2Aw+FwOGonTgE5HA6HIy44BeRwOByOuOAUkMPhcDjiglNADofD\n4YgLTgE5HA6HIy44BVSLEJGHReRPUa4jS0TeDDHvFBG51rPdSkSWikj96EkYP0RkhIi8G285QkVE\nhohIToh5D7jvIvKDiPQJtr86IiKLRGRIBMo54F6LyHsiclpVy65JOAVUSxCRVsDlwEt2e4iIqIh8\n4Jevt02fEgcxRwJvqGpBHOrGnvfhESorw5ZXx5emqh8D3UWkVyTq8KtPRWSTtz4RSbFpM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kEYzMHgQWAc0At+1WIsmDCilzgZj\nDAi4WSl1c6B4IvIU8FqQawGdEQMVIpKllCoTkSyMbS0AbsHw/aeA3SKyD8MzugC/VEr9IYDONqfF\nItLqtLhPBkh3wWk0A4ORGG+hSUCcxVo0EcY0Gq1cjeETr3McAf4EbFdKPdrp8gqgdRv1pcC/zfOD\nwMVm+gzgdAznp28Ct5qtKUQkW0TSzS7gJDMsAaOV1OfWuPYFp9EMAMwuuBcwvCNnKaXutFiSJkwE\nagGJyHMYYzEK2A/cZrZmRmM4BL1MRM7H8BS+hfYN5X6klHpdREZibAeeBxwArlVKVZvplwFZGK2e\nR5RSrVswFGJ4wgaoBW407/8vM8wBPK+UerjPv1UbII0muhGRJcCVSqkvi4gdwzX+/UqplRZL02g+\nF9oAaTQajcYS9BiQRqPRaCxBGyCNRqPRWII2QBqNRqOxBG2ANBqNRmMJ2gBpNBqNxhK0AdJoNBqN\nJWgDpNFoNBpL+P8QPxTUW1HDFgAAAABJRU5ErkJggg==\n", + "text/plain": [ + "" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from lmfit import minimize, Parameters\n", + "from lmfit.models import LinearModel, LorentzianModel, ConstantModel\n", + "\n", + "x = freq[sel]\n", + "y = ampl[sel]\n", + "\n", + "lor = LorentzianModel()\n", + "model = ConstantModel() + LorentzianModel()\n", + "\n", + "pars = lor.guess(y, x=x)\n", + "pars.add('c', 0)\n", + "out = model.fit(y, pars, x=x)\n", + "out.best_values\n", + "print(out.best_values)\n", + "out.plot()" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.53608852786 -0.443911152704\n" + ] + } + ], + "source": [ + "print(((out.best_values['center']+freq[1]) * 1920e-9 - 1) * 890998,\n", + " ((out.best_values['center']-freq[1]) * 1920e-9 - 1) * 890998)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import atsaverage.alazar\n", + "atsaverage.alazar.TriggerRangeID.et" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "lab_master", + "language": "python", + "name": "lab_master" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/tests/hardware/tabor_clock_tests.py b/tests/hardware/tabor_clock_tests.py new file mode 100644 index 000000000..fcbb64bec --- /dev/null +++ b/tests/hardware/tabor_clock_tests.py @@ -0,0 +1,165 @@ +import unittest + + +class MyTest(unittest.TestCase): + @unittest.skip + def test_the_thing(self): + exec_test() + +with_alazar = True + + +def get_pulse(): + from qctoolkit.pulses import TablePulseTemplate as TPT, SequencePulseTemplate as SPT, \ + RepetitionPulseTemplate as RPT, FunctionPulseTemplate as FPT, MultiChannelPulseTemplate as MPT + + sine = FPT('U*sin(2*pi*t/tau)', 'tau', channel='out') + marker_on = FPT('1', 'tau', channel='trigger') + + multi = MPT([sine, marker_on], {'tau', 'U'}) + multi.atomicity = True + + assert sine.defined_channels == {'out'} + assert multi.defined_channels == {'out', 'trigger'} + + sine.add_measurement_declaration('meas', 0, 'tau') + + base = SPT([(multi, dict(tau='tau', U='U'), dict(meas='A')), + (multi, dict(tau='tau', U='U'), dict(meas='A')), + (multi, dict(tau='tau', U='U'), dict(meas='A'))], {'tau', 'U'}) + + repeated = RPT(base, 'n') + + root = SPT([repeated, repeated, repeated], {'tau', 'n', 'U'}) + + assert root.defined_channels == {'out', 'trigger'} + + return root + + +def get_alazar_config(): + from atsaverage import alazar + from atsaverage.config import ScanlineConfiguration, CaptureClockConfiguration, EngineTriggerConfiguration,\ + TRIGInputConfiguration, InputConfiguration + + trig_level = int((5 + 0.4) / 10. * 255) + assert 0 <= trig_level < 256 + + config = ScanlineConfiguration() + config.triggerInputConfiguration = TRIGInputConfiguration(triggerRange=alazar.TriggerRangeID.etr_5V) + config.triggerConfiguration = EngineTriggerConfiguration(triggerOperation=alazar.TriggerOperation.J, + triggerEngine1=alazar.TriggerEngine.J, + triggerSource1=alazar.TriggerSource.external, + triggerSlope1=alazar.TriggerSlope.positive, + triggerLevel1=trig_level, + triggerEngine2=alazar.TriggerEngine.K, + triggerSource2=alazar.TriggerSource.disable, + triggerSlope2=alazar.TriggerSlope.positive, + triggerLevel2=trig_level) + config.captureClockConfiguration = CaptureClockConfiguration(source=alazar.CaptureClockType.internal_clock, + samplerate=alazar.SampleRateID.rate_100MSPS) + config.inputConfiguration = 4*[InputConfiguration(input_range=alazar.InputRangeID.range_1_V)] + config.totalRecordSize = 0 + + assert config.totalRecordSize == 0 + + return config + + +def get_operations(): + from atsaverage.operations import Downsample, RepAverage + + return [RepAverage(identifier='REP_A', maskID='A')] + + +def get_window(card): + from atsaverage.gui import ThreadedStatusWindow + window = ThreadedStatusWindow(card) + window.start() + return window + + +def exec_test(): + import time + import numpy as np + + t = [] + names = [] + + def tic(name): + t.append(time.time()) + names.append(name) + + from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation + tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR', reset=True) + + tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB') + tawg.paranoia_level = 2 + + # warnings.simplefilter('error', Warning) + + from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel + hardware_setup = HardwareSetup() + + hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0)) + hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1)) + hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair, 0)) + hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair, 1)) + + if with_alazar: + from qctoolkit.hardware.dacs.alazar import AlazarCard + import atsaverage.server + + if not atsaverage.server.Server.default_instance.running: + atsaverage.server.Server.default_instance.start(key=b'guest') + + import atsaverage.core + + alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1)) + alazar.register_mask_for_channel('A', 0) + alazar.config = get_alazar_config() + + alazar.register_operations('test', get_operations()) + + window = get_window(atsaverage.core.getLocalCard(1, 1)) + + hardware_setup.register_dac(alazar) + + repeated = get_pulse() + + from qctoolkit.pulses.sequencing import Sequencer + + tic('init') + sequencer = Sequencer() + sequencer.push(repeated, + parameters=dict(n=10000, tau=1920, U=0.5), + channel_mapping={'out': 'TABOR_A', 'trigger': 'TABOR_A_MARKER'}, + window_mapping=dict(A='A')) + instruction_block = sequencer.build() + + tic('sequence') + + hardware_setup.register_program('test', instruction_block) + + tic('register') + + if with_alazar: + from atsaverage.masks import PeriodicMask + m = PeriodicMask() + m.identifier = 'D' + m.begin = 0 + m.end = 1 + m.period = 1 + m.channel = 0 + alazar._registered_programs['test'].masks.append(m) + + tic('per_mask') + + hardware_setup.arm_program('test') + + tic('arm') + + for d, name in zip(np.diff(np.asarray(t)), names[1:]): + print(name, d) + + d = 1 diff --git a/tests/hardware/tabor_exex_test.py b/tests/hardware/tabor_exex_test.py new file mode 100644 index 000000000..045243bd2 --- /dev/null +++ b/tests/hardware/tabor_exex_test.py @@ -0,0 +1,136 @@ +import unittest + + +with_alazar = True + +def get_pulse(): + from qctoolkit.pulses import TablePulseTemplate as TPT, SequencePulseTemplate as SPT, RepetitionPulseTemplate as RPT + + ramp = TPT(identifier='ramp', channels={'out', 'trigger'}) + ramp.add_entry(0, 'start', channel='out') + ramp.add_entry('duration', 'stop', 'linear', channel='out') + + ramp.add_entry(0, 1, channel='trigger') + ramp.add_entry('duration', 1, 'hold', channel='trigger') + + ramp.add_measurement_declaration('meas', 0, 'duration') + + base = SPT([(ramp, dict(start='min', stop='max', duration='tau/3'), dict(meas='A')), + (ramp, dict(start='max', stop='max', duration='tau/3'), dict(meas='B')), + (ramp, dict(start='max', stop='min', duration='tau/3'), dict(meas='C'))], {'min', 'max', 'tau'}) + + repeated = RPT(base, 'n') + + root = SPT([repeated, repeated, repeated], {'min', 'max', 'tau', 'n'}) + + return root + + +def get_alazar_config(): + from atsaverage import alazar + from atsaverage.config import ScanlineConfiguration, CaptureClockConfiguration, EngineTriggerConfiguration,\ + TRIGInputConfiguration, InputConfiguration + + trig_level = int((5 + 0.4) / 10. * 255) + assert 0 <= trig_level < 256 + + config = ScanlineConfiguration() + config.triggerInputConfiguration = TRIGInputConfiguration(triggerRange=alazar.TriggerRangeID.etr_5V) + config.triggerConfiguration = EngineTriggerConfiguration(triggerOperation=alazar.TriggerOperation.J, + triggerEngine1=alazar.TriggerEngine.J, + triggerSource1=alazar.TriggerSource.external, + triggerSlope1=alazar.TriggerSlope.positive, + triggerLevel1=trig_level, + triggerEngine2=alazar.TriggerEngine.K, + triggerSource2=alazar.TriggerSource.disable, + triggerSlope2=alazar.TriggerSlope.positive, + triggerLevel2=trig_level) + config.captureClockConfiguration = CaptureClockConfiguration(source=alazar.CaptureClockType.internal_clock, + samplerate=alazar.SampleRateID.rate_100MSPS) + config.inputConfiguration = 4*[InputConfiguration(input_range=alazar.InputRangeID.range_1_V)] + config.totalRecordSize = 0 + + assert config.totalRecordSize == 0 + + return config + +def get_operations(): + from atsaverage.operations import Downsample + + return [Downsample(identifier='DS_A', maskID='A'), + Downsample(identifier='DS_B', maskID='B'), + Downsample(identifier='DS_C', maskID='C'), + Downsample(identifier='DS_D', maskID='D')] + +def get_window(card): + from atsaverage.gui import ThreadedStatusWindow + window = ThreadedStatusWindow(card) + window.start() + return window + + +class TaborTests(unittest.TestCase): + @unittest.skip + def test_all(self): + from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation + #import warnings + tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR') + tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB') + tawg.paranoia_level = 2 + + #warnings.simplefilter('error', Warning) + + from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel + hardware_setup = HardwareSetup() + + hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0)) + hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1)) + hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair, 0)) + hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair, 1)) + + if with_alazar: + from qctoolkit.hardware.dacs.alazar import AlazarCard + import atsaverage.server + + if not atsaverage.server.Server.default_instance.running: + atsaverage.server.Server.default_instance.start(key=b'guest') + + import atsaverage.core + + alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1)) + alazar.register_mask_for_channel('A', 0) + alazar.register_mask_for_channel('B', 0) + alazar.register_mask_for_channel('C', 0) + alazar.config = get_alazar_config() + + alazar.register_operations('test', get_operations()) + window = get_window(atsaverage.core.getLocalCard(1, 1)) + hardware_setup.register_dac(alazar) + + repeated = get_pulse() + + from qctoolkit.pulses.sequencing import Sequencer + + sequencer = Sequencer() + sequencer.push(repeated, + parameters=dict(n=1000, min=-0.5, max=0.5, tau=192*3), + channel_mapping={'out': 'TABOR_A', 'trigger': 'TABOR_A_MARKER'}, + window_mapping=dict(A='A', B='B', C='C')) + instruction_block = sequencer.build() + + hardware_setup.register_program('test', instruction_block) + + if with_alazar: + from atsaverage.masks import PeriodicMask + m = PeriodicMask() + m.identifier = 'D' + m.begin = 0 + m.end = 1 + m.period = 1 + m.channel = 0 + alazar._registered_programs['test'].masks.append(m) + + hardware_setup.arm_program('test') + + d = 1 + From d6313b5e5213f94f2e55fd9e8403cc2ff5c38a15 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 18:52:00 +0100 Subject: [PATCH 038/116] Fic duration evaluation in function pulse template --- qctoolkit/pulses/function_pulse_template.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 3cf53169a..b45c9c57f 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -89,8 +89,9 @@ def build_waveform(self, measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> 'FunctionWaveform': substitutions = dict((v, parameters[v].get_value()) for v in self.__expression.variables() if v != 't') + duration_parameters = dict((v, parameters[v].get_value()) for v in self.__duration_expression.variables()) return FunctionWaveform(expression=self.__expression.evaluate_symbolic(substitutions=substitutions), - duration=self.__duration_expression.evaluate_numeric(**parameters), + duration=self.__duration_expression.evaluate_numeric(**duration_parameters), measurement_windows=self.get_measurement_windows(parameters=parameters, measurement_mapping=measurement_mapping), channel=channel_mapping[self.__channel]) From 9b6252d20fcaed1cff27f4f4932ab8f8fdf2e7ff Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 19:05:51 +0100 Subject: [PATCH 039/116] Skip tests if pytabor or pyvisa are not found --- tests/hardware/tabor_tests.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index f97a56e4f..93e65b466 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -1,5 +1,15 @@ import unittest -from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair + + +try: + from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair + imports_failed = (False, '') +except ImportError as err: + if err.name in ('pytabor', 'pyvisa'): + imports_failed = (True, 'Could not import {}').format(err.name) + else: + raise + from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit.pulses.instructions import InstructionBlock import numbers @@ -22,6 +32,7 @@ instrument = None +@unittest.skipIf(*imports_failed) class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -112,6 +123,7 @@ def test_upload(self): program.upload_to_device(instrument, (1, 2)) +@unittest.skipIf(*imports_failed) class TaborAWGRepresentationTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -131,6 +143,7 @@ def test_amplitude(self): self.assertIsInstance(instrument.amplitude(ch), float) +@unittest.skipIf(*imports_failed) class TaborChannelPairTests(unittest.TestCase): def test_copy(self): channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) From 7e355bb1a08885dee1228bce6148ddd1f3c5123a Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 19:07:17 +0100 Subject: [PATCH 040/116] Update travis dependencies --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index ca12b4afa..de7156a5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_install: - export PATH=$HOME/miniconda3/bin:$PATH - conda update --yes conda - conda install --yes python=$TRAVIS_PYTHON_VERSION pip atlas numpy scipy matplotlib - - pip install pyflakes coverage coveralls + - pip install pyflakes coverage coveralls sympy install: - python setup.py install script: From b87e68b33b550320e44f80892ccaf12df182a6b5 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 19:54:30 +0100 Subject: [PATCH 041/116] Fix a few tests. Tabor test now imports dummy modules --- qctoolkit/hardware/awgs/tabor.py | 124 ++++++++----------------------- tests/expression_tests.py | 2 +- tests/hardware/tabor_tests.py | 74 ++++++++++-------- tests/serialization_tests.py | 5 +- 4 files changed, 80 insertions(+), 125 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 1f5f1e825..60aba7f8b 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,18 +1,17 @@ """""" import fractions import sys -from typing import List, Tuple, Iterable, Set, NamedTuple, Callable, Optional +from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any # Provided by Tabor electronics for python 2.7 # a python 3 version is in a private repository on https://git.rwth-aachen.de/qutech # Beware of the string encoding change! -import pytabor import teawg import numpy as np from qctoolkit import ChannelID from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -from qctoolkit.hardware.program import Loop, MultiChannelProgram +from qctoolkit.hardware.program import Loop from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave from qctoolkit.hardware.awgs import AWG @@ -36,14 +35,14 @@ def __hash__(self) -> int: return hash((bytes(self[0]) if self[0] is not None else 0, bytes(self[1]) if self[1] is not None else 0)) - def __eq__(self, other): + def __eq__(self, other) -> bool: return @property - def num_points(self): + def num_points(self) -> int: return len(self[0]) if self[1] is None else len(self[1]) - def get_as_binary(self): + def get_as_binary(self) -> np.ndarray: assert not (self[0] is None or self[1] is None) return make_combined_wave([self]) @@ -54,8 +53,8 @@ class TaborProgram: def __init__(self, program: Loop, device_properties, - channels: Tuple[ChannelID, ChannelID], - markers: Tuple[ChannelID, ChannelID]): + channels: Tuple[Optional[ChannelID], Optional[ChannelID]], + markers: Tuple[Optional[ChannelID], Optional[ChannelID]]): if len(channels) != device_properties['chan_per_part']: raise TaborException('TaborProgram only supports {} channels'.format(device_properties['chan_per_part'])) if len(markers) != device_properties['chan_per_part']: @@ -66,8 +65,8 @@ def __init__(self, self._program = program self.__waveform_mode = 'advanced' - self._channels = channels - self._markers = markers + self._channels = tuple(channels) + self._markers = tuple(markers) self.__used_channels = channel_set self.__device_properties = device_properties @@ -83,11 +82,11 @@ def __init__(self, self.setup_advanced_sequence_mode() @property - def markers(self): + def markers(self) -> Tuple[Optional[ChannelID], Optional[ChannelID]]: return self._markers @property - def channels(self): + def channels(self) -> Tuple[Optional[ChannelID], Optional[ChannelID]]: return self._channels def setup_single_waveform_mode(self) -> None: @@ -297,76 +296,8 @@ def get_advanced_sequencer_table(self) -> List[Tuple[int, int, int]]: """Advanced sequencer table that can be used via the download_adv_seq_table pytabor command""" return self._advanced_sequencer_table - def get_waveform_data(self, - device_properties, - sample_rate: float, - voltage_amplitude: Tuple[float, float], - voltage_offset: Tuple[float, float]) -> Tuple[np.ndarray, np.ndarray]: - if any(not(waveform.duration*sample_rate).is_integer() for waveform in self._waveforms): - raise TaborException('At least one waveform has a length that is no multiple of the time per sample') - maximal_length = int(max(waveform.duration for waveform in self._waveforms) * sample_rate) - time_array = np.arange(0, maximal_length, 1) - maximal_size = int(2 * (sample_rate * sum(waveform.duration for waveform in self._waveforms) + 16 * len(self._waveforms))) - data = np.empty(maximal_size, dtype=np.uint16) - offset = 0 - segment_lengths = np.zeros(len(self._waveforms), dtype=np.uint32) - - def voltage_to_data(waveform, time, channel): - return voltage_to_uint16(waveform[self._channels[channel]].sample(time), - voltage_amplitude[channel], - voltage_offset[channel], - resolution=14) if self._channels[channel] else None - - for i, waveform in enumerate(self._waveforms): - t = time_array[:int(waveform.duration*sample_rate)] - segment1 = voltage_to_data(waveform, t, 0) - segment2 = voltage_to_data(waveform, t, 1) - segment_lengths[i] = len(segment1) if segment1 is not None else len(segment2) - assert(segment2 is None or segment_lengths[i] == len(segment2)) - offset = pytabor.make_combined_wave( - segment1, - segment2, - data, offset, add_idle_pts=True) - - if np.any(segment_lengths < device_properties['min_seq_len']): - raise Exception() - if np.any(segment_lengths % device_properties['seg_quantum']>0): - raise Exception() - - return data[:offset], segment_lengths - - def upload_to_device(self, device: 'TaborAWG', channel_pair) -> None: - if channel_pair not in ((1, 2), (3, 4)): - raise Exception('Invalid channel pair', channel_pair) - - if self.__waveform_mode == 'advanced': - sample_rate = device.sample_rate(channel_pair[0]) - amplitude = (device.amplitude(channel_pair[0]), device.amplitude(channel_pair[1])) - offset = (device.offset(channel_pair[0]), device.offset(channel_pair[1])) - - wf_data, segment_lengths = self.get_waveform_data(device_properties=device.dev_properties, - sample_rate=sample_rate, - voltage_amplitude=amplitude, - voltage_offset=offset) - # download the waveform data as one big waveform - device.select_channel(channel_pair[0]) - device.send_cmd(':FUNC:MODE ASEQ') - device.send_cmd(':TRAC:DEF 1,{}'.format(len(wf_data))) - device.send_cmd(':TRAC:SEL 1') - device.send_binary_data(pref=':TRAC:DATA', bin_dat=wf_data) - # partition the memory - device.download_segment_lengths(segment_lengths) - - #download all sequence tables - for i, sequencer_table in enumerate(self.get_sequencer_tables()): - device.send_cmd('SEQ:SEL {}'.format(i+1)) - device.download_sequencer_table(sequencer_table) - - device.download_adv_seq_table(self.get_advanced_sequencer_table()) - device.send_cmd('SEQ:SEL 1') - @property - def waveform_mode(self): + def waveform_mode(self) -> str: return self.__waveform_mode @@ -397,7 +328,7 @@ def switch_group_off(grp): self.select_channel(3) self.send_cmd(setup_command) - def send_cmd(self, cmd_str, paranoia_level=None): + def send_cmd(self, cmd_str, paranoia_level=None) -> Any: if paranoia_level is not None: super().send_cmd(cmd_str=cmd_str, paranoia_level=paranoia_level) else: @@ -437,7 +368,7 @@ def select_channel(self, channel) -> None: self.send_cmd(':INST:SEL {channel}'.format(channel=channel)) - def select_marker(self, marker): + def select_marker(self, marker) -> None: if marker not in (1, 2, 3, 4): raise TaborException('Invalid marker: {}'.format(marker)) self.send_cmd(':SOUR:MARK:SEL {marker}'.format(marker=marker)) @@ -455,24 +386,29 @@ def amplitude(self, channel) -> float: def offset(self, channel) -> float: return float(self.send_query(':INST:SEL {channel}; :VOLT:OFFS?'.format(channel=channel))) - def enable(self): + def enable(self) -> None: self.send_cmd(':ENAB') - def abort(self): + def abort(self) -> None: self.send_cmd(':ABOR') - def reset(self): + def reset(self) -> None: self.send_cmd(':RES') self.send_cmd(':INST:SEL 1; :INIT:GATE OFF; :INIT:CONT ON; :INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS') self.send_cmd(':INST:SEL 3; :INIT:GATE OFF; :INIT:CONT ON; :INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS') + def trigger(self) -> None: + self.send_cmd(':TRIG') + TaborProgramMemory = NamedTuple('TaborProgramMemory', [('segment_indices', np.ndarray), ('program', TaborProgram)]) -def with_configuration_guard(function_object): - def guarding_method(channel_pair: 'TaborChannelPair', *args, **kwargs): +def with_configuration_guard(function_object: Callable[['TaborChannelPair'], Any]) -> Callable[['TaborChannelPair'], + Any]: + """This decorator assures that the AWG is in configuration mode while the decorated method runs.""" + def guarding_method(channel_pair: 'TaborChannelPair', *args, **kwargs) -> Any: if channel_pair._configuration_guard_count == 0: channel_pair._enter_config_mode() channel_pair._configuration_guard_count += 1 @@ -788,7 +724,7 @@ def arm(self, name: str) -> None: self._device.enable() self._current_program = name - def run_current_program(self): + def run_current_program(self) -> None: if self._current_program: self._device.send_cmd(':TRIG') else: @@ -811,10 +747,9 @@ def num_channels(self) -> int: def num_markers(self) -> int: return 2 - def configuration_guard(self): - return self.ConfigurationGuard(self) - - def _enter_config_mode(self): + def _enter_config_mode(self) -> None: + """Enter the configuration mode if not already in. All outputs are turned of and the sequencing is disabled + as the manual states this speeds up sequence validation when uploading multiple sequences""" if self._is_in_config_mode is False: self.set_marker_state(0, False) self.set_marker_state(1, False) @@ -825,7 +760,8 @@ def _enter_config_mode(self): self._device.send_cmd(':SOUR:FUNC:MODE FIX') self._is_in_config_mode = True - def _exit_config_mode(self): + def _exit_config_mode(self) -> None: + """Leave the configuration mode. Enter advanced sequence mode and turn on all outputs""" if self._current_program: _, program = self._known_programs[self._current_program] diff --git a/tests/expression_tests.py b/tests/expression_tests.py index c11e1738f..e6f29bcd4 100644 --- a/tests/expression_tests.py +++ b/tests/expression_tests.py @@ -34,7 +34,7 @@ def test_evaluate_symbolic(self): 'c': -7 } result = e.evaluate_symbolic(params) - expected = sympify('d*b-7') + expected = Expression('d*b-7') self.assertEqual(result, expected) def test_variables(self) -> None: diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 30e52ca38..7445a047f 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -1,38 +1,63 @@ import unittest +import sys +import numbers +import itertools +from copy import copy, deepcopy +import numpy as np +failed_imports = {} try: - from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair - imports_failed = (False, '') -except ImportError as err: - if err.name in ('pytabor', 'pyvisa'): - imports_failed = (True, 'Could not import {}').format(err.name) - else: - raise + import pytabor +except ImportError: + class dummy_pytabor: + pass + sys.modules['pytabor'] = dummy_pytabor + failed_imports.add('pytabor') +try: + import pyvisa +except ImportError: + class dummy_pyvisa: + pass + sys.modules['pyvisa'] = dummy_pyvisa + failed_imports.add('pyvisa') + +try: + import teawg +except ImportError: + class dummy_teawg: + model_properties_dict = dict() + sys.modules['teawg'] = dummy_teawg + failed_imports.add('teawg') + + +from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit.pulses.instructions import InstructionBlock -import numbers -import itertools -import numpy as np -from copy import copy, deepcopy -from teawg import model_properties_dict from qctoolkit.hardware.util import voltage_to_uint16 + +from teawg import model_properties_dict + from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests -try: - instrument_address = '127.0.0.1' - #instrument_address = '192.168.1.223' - #instrument_address = 'ASRL10::INSTR' - instrument = TaborAWGRepresentation(instrument_address) - instrument._visa_inst.timeout = 25000 -except: +if 'pytabor' not in failed_imports: + # fix on your machine + possible_addresses = ('127.0.0.1', ) + instrument = None + try: + for instrument_address in possible_addresses: + instrument = TaborAWGRepresentation(instrument_address, + reset=True, + paranoia_level=2) + instrument._visa_inst.timeout = 25000 + except: + pass -@unittest.skipIf(*imports_failed) class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -121,15 +146,7 @@ def my_gen(gen): self.assertTrue(np.all(sampled_seg[0] << 2 == data[0] << 2)) self.assertTrue(np.all(sampled_seg[1] << 2 == data[1] << 2)) - @unittest.skipIf(instrument is None, "Instrument not present") - def test_upload(self): - prog = MultiChannelProgram(self.root_block) - program = TaborProgram(prog, instrument.dev_properties, ('A', 'B')) - - program.upload_to_device(instrument, (1, 2)) - -@unittest.skipIf(*imports_failed) class TaborAWGRepresentationTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -149,7 +166,6 @@ def test_amplitude(self): self.assertIsInstance(instrument.amplitude(ch), float) -@unittest.skipIf(*imports_failed) class TaborChannelPairTests(unittest.TestCase): @unittest.skipIf(instrument is None, "Instrument not present") def test_copy(self): diff --git a/tests/serialization_tests.py b/tests/serialization_tests.py index c6f89aa2e..ab5ac1c33 100644 --- a/tests/serialization_tests.py +++ b/tests/serialization_tests.py @@ -329,7 +329,10 @@ def test_serialization_and_deserialization_combined(self) -> None: table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') table = TablePulseTemplate() foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') - sequence = SequencePulseTemplate([(table_foo, foo_mappings, {}), (table, {}, {})], ['ilse', 'albert', 'voltage'], identifier=None) + sequence = SequencePulseTemplate([(table_foo, foo_mappings, {}), + (table, {}, {})], + ['ilse', 'albert', 'voltage'], + identifier=None) storage = DummyStorageBackend() serializer = Serializer(storage) From cc2242620a2c7927301eebf8fcf8da59649262ed Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 20:14:39 +0100 Subject: [PATCH 042/116] Add dummy TaborAWG --- tests/hardware/tabor_tests.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 7445a047f..04b204797 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -6,7 +6,7 @@ import numpy as np -failed_imports = {} +failed_imports = set() try: import pytabor except ImportError: @@ -28,6 +28,7 @@ class dummy_pyvisa: except ImportError: class dummy_teawg: model_properties_dict = dict() + sys.modules['teawg'] = dummy_teawg failed_imports.add('teawg') @@ -43,20 +44,31 @@ class dummy_teawg: from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests +class DummyTaborAWGRepresentation: + def __init__(self, *args, **kwargs): + pass + send_cmd = __init__ + send_query = send_cmd + select_channel = send_cmd + send_binary_data = send_cmd + download_sequencer_table = send_cmd + +instrument = None if 'pytabor' not in failed_imports: # fix on your machine possible_addresses = ('127.0.0.1', ) - - instrument = None - try: - for instrument_address in possible_addresses: + for instrument_address in possible_addresses: + try: instrument = TaborAWGRepresentation(instrument_address, reset=True, paranoia_level=2) instrument._visa_inst.timeout = 25000 - except: - pass + break + except: + pass + if instrument is None: + instrument = DummyTaborAWGRepresentation() class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): @@ -147,27 +159,23 @@ def my_gen(gen): self.assertTrue(np.all(sampled_seg[1] << 2 == data[1] << 2)) +@unittest.skipIf(isinstance(instrument, DummyTaborAWGRepresentation), "No instrument present") class TaborAWGRepresentationTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - @unittest.skipIf(instrument is None, "Instrument not present") def test_sample_rate(self): - if not instrument.is_open: - self.skipTest("No instrument found.") for ch in (1, 2, 3, 4): self.assertIsInstance(instrument.sample_rate(ch), numbers.Number) with self.assertRaises(TaborException): instrument.sample_rate(0) - @unittest.skipIf(instrument is None, "Instrument not present") def test_amplitude(self): for ch in range(1, 5): self.assertIsInstance(instrument.amplitude(ch), float) class TaborChannelPairTests(unittest.TestCase): - @unittest.skipIf(instrument is None, "Instrument not present") def test_copy(self): channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) with self.assertRaises(NotImplementedError): From 3e72ef9adb5deb66e1d3629d534177de1beaeb07 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 20:36:09 +0100 Subject: [PATCH 043/116] Update travis to use conda environment Impove dummy modules --- .travis.yml | 7 +++++-- tests/hardware/dummy_modules.py | 15 +++++++++++++++ tests/hardware/tabor_tests.py | 17 ++++++----------- 3 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 tests/hardware/dummy_modules.py diff --git a/.travis.yml b/.travis.yml index de7156a5d..52155477d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,8 +19,11 @@ before_install: - ./install_miniconda3.sh # - export PATH=$HOME/miniconda3/bin:$PATH - - conda update --yes conda - - conda install --yes python=$TRAVIS_PYTHON_VERSION pip atlas numpy scipy matplotlib + - hash -r + - conda config --set always_yes yes --set changeps1 no + - conda update -q conda + - conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION pip atlas numpy matplotlib + - source activate test-environment - pip install pyflakes coverage coveralls sympy install: - python setup.py install diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py new file mode 100644 index 000000000..483b63bd5 --- /dev/null +++ b/tests/hardware/dummy_modules.py @@ -0,0 +1,15 @@ +class dummy_pytabor: + class TEWXAwg: + def __init__(self, *args, **kwargs): + pass + send_cmd = __init__ + send_query = send_cmd + select_channel = send_cmd + send_binary_data = send_cmd + download_sequencer_table = send_cmd + +class dummy_pyvisa: + pass + +class dummy_teawg: + model_properties_dict = dict() diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 04b204797..0533ad898 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -6,30 +6,25 @@ import numpy as np +from . import dummy_modules + failed_imports = set() try: import pytabor except ImportError: - class dummy_pytabor: - pass - sys.modules['pytabor'] = dummy_pytabor + sys.modules['pytabor'] = dummy_modules.dummy_pytabor failed_imports.add('pytabor') try: import pyvisa except ImportError: - class dummy_pyvisa: - pass - sys.modules['pyvisa'] = dummy_pyvisa + sys.modules['pyvisa'] = dummy_modules.dummy_pyvisa failed_imports.add('pyvisa') try: import teawg except ImportError: - class dummy_teawg: - model_properties_dict = dict() - - sys.modules['teawg'] = dummy_teawg + sys.modules['teawg'] = dummy_modules.dummy_teawg failed_imports.add('teawg') @@ -44,7 +39,7 @@ class dummy_teawg: from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests -class DummyTaborAWGRepresentation: +class DummyTaborAWGRepresentation(dummy_modules.dummy_pytabor.TEWXAwg): def __init__(self, *args, **kwargs): pass send_cmd = __init__ From 38c1d72143cfc3a661745221d8aa12712d582f94 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 20:45:22 +0100 Subject: [PATCH 044/116] Move to correct module --- tests/hardware/dummy_modules.py | 13 +++++++------ tests/hardware/tabor_tests.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 483b63bd5..04f4915e4 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -1,4 +1,11 @@ class dummy_pytabor: + pass + +class dummy_pyvisa: + pass + +class dummy_teawg: + model_properties_dict = dict() class TEWXAwg: def __init__(self, *args, **kwargs): pass @@ -7,9 +14,3 @@ def __init__(self, *args, **kwargs): select_channel = send_cmd send_binary_data = send_cmd download_sequencer_table = send_cmd - -class dummy_pyvisa: - pass - -class dummy_teawg: - model_properties_dict = dict() diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 0533ad898..a270d57ae 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -39,7 +39,7 @@ from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests -class DummyTaborAWGRepresentation(dummy_modules.dummy_pytabor.TEWXAwg): +class DummyTaborAWGRepresentation(dummy_modules.dummy_teawg.TEWXAwg): def __init__(self, *args, **kwargs): pass send_cmd = __init__ From 016fd37dd90fad15800a4b7fe782cfbbdb684f53 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 20:51:03 +0100 Subject: [PATCH 045/116] Make use of dummy if instrument not found --- tests/hardware/tabor_tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index a270d57ae..619712856 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -62,8 +62,8 @@ def __init__(self, *args, **kwargs): except: pass - if instrument is None: - instrument = DummyTaborAWGRepresentation() +if instrument is None: + instrument = DummyTaborAWGRepresentation() class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): From 373745282c54fb609d3cd152dce4b3a1afc58fe1 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 21:02:35 +0100 Subject: [PATCH 046/116] Fix more dummy dependencies --- tests/hardware/dummy_modules.py | 24 ++++++++++++++++++++++++ tests/hardware/tabor_tests.py | 26 ++------------------------ tests/hardware/util_tests.py | 6 ++++-- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 04f4915e4..076267b4a 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -1,3 +1,8 @@ +"""Import dummy modules if actual modules not installed. Sets dummy modules in sys so subsequent imports +use the dummies""" + +import sys + class dummy_pytabor: pass @@ -14,3 +19,22 @@ def __init__(self, *args, **kwargs): select_channel = send_cmd send_binary_data = send_cmd download_sequencer_table = send_cmd + +failed_imports = set() +try: + import pytabor +except ImportError: + sys.modules['pytabor'] = dummy_pytabor + failed_imports.add(dummy_pytabor) + +try: + import pyvisa +except ImportError: + sys.modules['pyvisa'] = dummy_pyvisa + failed_imports.add(dummy_pyvisa) + +try: + import teawg +except ImportError: + sys.modules['teawg'] = dummy_teawg + failed_imports.add(dummy_teawg) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 619712856..2af9c2957 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -1,40 +1,18 @@ import unittest -import sys import numbers import itertools from copy import copy, deepcopy - import numpy as np from . import dummy_modules -failed_imports = set() -try: - import pytabor -except ImportError: - sys.modules['pytabor'] = dummy_modules.dummy_pytabor - failed_imports.add('pytabor') - -try: - import pyvisa -except ImportError: - sys.modules['pyvisa'] = dummy_modules.dummy_pyvisa - failed_imports.add('pyvisa') - -try: - import teawg -except ImportError: - sys.modules['teawg'] = dummy_modules.dummy_teawg - failed_imports.add('teawg') - - from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.hardware.util import voltage_to_uint16 - from teawg import model_properties_dict +import pytabor from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests @@ -49,7 +27,7 @@ def __init__(self, *args, **kwargs): download_sequencer_table = send_cmd instrument = None -if 'pytabor' not in failed_imports: +if pytabor not in dummy_modules.failed_imports: # fix on your machine possible_addresses = ('127.0.0.1', ) for instrument_address in possible_addresses: diff --git a/tests/hardware/util_tests.py b/tests/hardware/util_tests.py index 3df325e9a..67de70897 100644 --- a/tests/hardware/util_tests.py +++ b/tests/hardware/util_tests.py @@ -1,12 +1,14 @@ import unittest import itertools -import pytabor import numpy as np from qctoolkit.hardware.awgs.tabor import TaborSegment from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave +from . import dummy_modules +import pytabor + class VoltageToBinaryTests(unittest.TestCase): @@ -28,8 +30,8 @@ def test_voltage_to_uint16(self): self.assertTrue(np.all(expected_data == received_data)) +@unittest.skipIf(pytabor in dummy_modules.failed_imports, "Cannot compare to pytabor results") class TaborMakeCombinedTest(unittest.TestCase): - def exec_general(self, data_1, data_2): tabor_segments = [TaborSegment(d1, d2) for d1, d2 in zip(data_1, data_2)] expected_length = (sum(segment.num_points for segment in tabor_segments) + 16 * (len(tabor_segments) - 1)) * 2 From 9994a913efbd7b6a4f667b5800d5eb84537d1a3d Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 22:27:02 +0100 Subject: [PATCH 047/116] Make dummy packages even smarter Add alazar testing --- qctoolkit/hardware/dacs/alazar.py | 20 ++++++--- tests/hardware/alazar_tests.py | 73 +++++++++++++++++++++++++++++++ tests/hardware/dummy_modules.py | 62 ++++++++++++++++++++++---- 3 files changed, 140 insertions(+), 15 deletions(-) create mode 100644 tests/hardware/alazar_tests.py diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py index 9a4621ed0..bb81dcacc 100644 --- a/qctoolkit/hardware/dacs/alazar.py +++ b/qctoolkit/hardware/dacs/alazar.py @@ -36,13 +36,11 @@ def __init__(self, card, config: Optional[ScanlineConfiguration]=None): def card(self) -> Any: return self.__card - def __make_mask(self, mask_id: str, begins, lengths) -> Mask: + def _make_mask(self, mask_id: str, begins, lengths) -> Mask: if mask_id not in self._mask_prototypes: raise KeyError('Measurement window {} can not be converted as it is not registered.'.format(mask_id)) hardware_channel, mask_type = self._mask_prototypes[mask_id] - if mask_type not in ('auto', 'cross_buffer', None): - raise ValueError('Currently only can do cross buffer mask') if np.any(begins[:-1]+lengths[:-1] > begins[1:]): raise ValueError('Found overlapping windows in begins') @@ -58,7 +56,7 @@ def register_measurement_windows(self, program_name: str, windows: Dict[str, Tuple[np.ndarray, np.ndarray]]) -> None: if not windows: - return + self._registered_programs[program_name].masks = [] total_length = 0 for mask_id, (begins, lengths) in windows.items(): @@ -77,7 +75,7 @@ def register_measurement_windows(self, total_length = np.ceil(total_length/self.__card.minimum_record_size) * self.__card.minimum_record_size self._registered_programs[program_name].masks = [ - self.__make_mask(mask_id, *window_begin_length) + self._make_mask(mask_id, *window_begin_length) for mask_id, window_begin_length in windows.items()] self._registered_programs[program_name].total_length = total_length @@ -88,6 +86,12 @@ def arm_program(self, program_name: str) -> None: config = self.config config.masks, config.operations, total_record_size = self._registered_programs[program_name] + if not config.masks: + if config.operations: + raise RuntimeError('Invalid configuration. Operations have no masks to work with') + else: + return + if config.totalRecordSize == 0: config.totalRecordSize = total_record_size elif config.totalRecordSize < total_record_size: @@ -105,6 +109,10 @@ def delete_program(self, program_name: str) -> None: def mask_prototypes(self): return self._mask_prototypes - def register_mask_for_channel(self, mask_id, hw_channel, mask_type='auto'): + def register_mask_for_channel(self, mask_id, hw_channel, mask_type='auto') -> None: + if hw_channel not in range(16): + raise ValueError('{} is not a valid hw channel'.format(hw_channel)) + if mask_type not in ('auto', 'cross_buffer', None): + raise NotImplementedError('Currently only can do cross buffer mask') self._mask_prototypes[mask_id] = (hw_channel, mask_type) diff --git a/tests/hardware/alazar_tests.py b/tests/hardware/alazar_tests.py new file mode 100644 index 000000000..3e5188574 --- /dev/null +++ b/tests/hardware/alazar_tests.py @@ -0,0 +1,73 @@ +import unittest + +from . import dummy_modules + +import atsaverage +import atsaverage.config +from atsaverage.config import ScanlineConfiguration + +import numpy as np + +from qctoolkit.hardware.dacs.alazar import AlazarCard + + +class AlazarTest(unittest.TestCase): + + def test_add_mask_prototype(self): + card = AlazarCard(None) + + card.register_mask_for_channel('M', 3, 'auto') + self.assertEqual(card._mask_prototypes, dict(M=(3, 'auto'))) + + with self.assertRaises(ValueError): + card.register_mask_for_channel('M', 'A', 'auto') + + with self.assertRaises(NotImplementedError): + card.register_mask_for_channel('M', 1, 'periodic') + + def test_make_mask(self): + card = AlazarCard(None) + card.register_mask_for_channel('M', 3, 'auto') + + begins = np.arange(15, dtype=np.uint64)*16 + lengths = 1+np.arange(15, dtype=np.uint64) + + with self.assertRaises(KeyError): + card._make_mask('N', begins, lengths) + + with self.assertRaises(ValueError): + card._make_mask('M', begins, lengths*3) + + mask = card._make_mask('M', begins, lengths) + self.assertEqual(mask.identifier, 'M') + self.assertIs(mask.begin, begins) + self.assertIs(mask.length, lengths) + self.assertEqual(mask.channel, 3) + + def test_register_measurement_windows(self): + + card = AlazarCard(dummy_modules.dummy_atsaverage.core.AlazarCard()) + card.register_mask_for_channel('A', 3, 'auto') + card.register_mask_for_channel('B', 1, 'auto') + + card.config = dummy_modules.dummy_atsaverage.config.ScanlineConfiguration() + + card.register_measurement_windows('empty', dict()) + + begins = np.arange(100)*176.5 + lengths = np.ones(100)*10*np.pi + card.register_measurement_windows('otto', dict(A=(begins, lengths))) + + self.assertEqual(set(card._registered_programs.keys()), {'empty', 'otto'}) + self.assertEqual(card._registered_programs['empty'].masks, []) + + expected_begins = np.rint(begins / 10).astype(dtype=np.uint64) + self.assertEqual(expected_begins.dtype, card._registered_programs['otto'].masks[0].begin.dtype) + np.testing.assert_equal(card._registered_programs['otto'].masks[0].begin, expected_begins) + + # pi ist genau 3 + self.assertTrue(np.all(card._registered_programs['otto'].masks[0].length == 3)) + + self.assertEqual(card._registered_programs['otto'].masks[0].channel, 3) + self.assertEqual(card._registered_programs['otto'].masks[0].identifier, 'A') + diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 076267b4a..eaae9050b 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -2,14 +2,18 @@ use the dummies""" import sys +from typing import Set -class dummy_pytabor: +class dummy_package: pass -class dummy_pyvisa: +class dummy_pytabor(dummy_package): pass -class dummy_teawg: +class dummy_pyvisa(dummy_package): + pass + +class dummy_teawg(dummy_package): model_properties_dict = dict() class TEWXAwg: def __init__(self, *args, **kwargs): @@ -20,21 +24,61 @@ def __init__(self, *args, **kwargs): send_binary_data = send_cmd download_sequencer_table = send_cmd +class dummy_atsaverage(dummy_package): + class atsaverage(dummy_package): + pass + class alazar(dummy_package): + pass + class core(dummy_package): + class AlazarCard: + model = 'DUMMY' + minimum_record_size = 256 + class config(dummy_package): + class CaptureClockConfig: + def numeric_sample_rate(self, card): + return 10**8 + class ScanlineConfiguration: + pass + ScanlineConfiguration.captureClockConfiguration = CaptureClockConfig() + class operations(dummy_package): + class OperationDefinition: + pass + class masks(dummy_package): + class Mask: + pass + class CrossBufferMask: + pass + + +def import_package(name, package) -> Set[dummy_package]: + imported = set() + sys.modules[name] = package + imported.add(package) + for attr in dir(package): + if isinstance(getattr(package, attr), type) and issubclass(getattr(package, attr), dummy_package): + imported |= import_package(name + '.' + attr, getattr(package, attr)) + return imported + + failed_imports = set() try: import pytabor except ImportError: - sys.modules['pytabor'] = dummy_pytabor - failed_imports.add(dummy_pytabor) + failed_imports |= import_package('pytabor', dummy_pytabor) try: import pyvisa except ImportError: - sys.modules['pyvisa'] = dummy_pyvisa - failed_imports.add(dummy_pyvisa) + failed_imports |= import_package('pyvisa', dummy_pyvisa) try: import teawg except ImportError: - sys.modules['teawg'] = dummy_teawg - failed_imports.add(dummy_teawg) + failed_imports |= import_package('teawg', dummy_teawg) + +try: + import atsaverage + import atsaverage.config +except ImportError: + failed_imports |= import_package('atsaverage', dummy_atsaverage) + From 6e9d26facd0e39eee8aa95dd54550c90c8fe25dc Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 9 Mar 2017 22:57:49 +0100 Subject: [PATCH 048/116] more and better pyvisa dummy --- tests/hardware/dummy_modules.py | 29 ++++++++++++++++++++++++----- tests/hardware/tabor_tests.py | 9 +++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index eaae9050b..1428dd135 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -11,16 +11,35 @@ class dummy_pytabor(dummy_package): pass class dummy_pyvisa(dummy_package): - pass + class resources(dummy_package): + class messagebased(dummy_package): + class MessageBasedResource: + def __init__(self, *args, **kwargs): + self.logged_writes = [] + self.logged_asks = [] + def write(self, *args, **kwargs): + self.logged_writes.append((args, kwargs)) + def ask(self, *args, **kwargs): + self.logged_asks.append((args, kwargs)) + return ';'.join( '0, bla'*args[0].count('?') ) +dummy_pyvisa.resources.MessageBasedResource = dummy_pyvisa.resources.messagebased.MessageBasedResource + class dummy_teawg(dummy_package): model_properties_dict = dict() class TEWXAwg: def __init__(self, *args, **kwargs): - pass - send_cmd = __init__ - send_query = send_cmd - select_channel = send_cmd + self.logged_commands = [] + self.logged_queries = [] + self._visa_inst = dummy_pyvisa.resources.MessageBasedResource() + @property + def visa_inst(self): + return self._visa_inst + def send_cmd(self, *args, **kwargs): + self.logged_commands.append((args, kwargs)) + def send_query(self, *args, **kwargs): + self.logged_queries.append((args, kwargs)) + return 0 send_binary_data = send_cmd download_sequencer_table = send_cmd diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 2af9c2957..eadf4694d 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -19,12 +19,9 @@ class DummyTaborAWGRepresentation(dummy_modules.dummy_teawg.TEWXAwg): def __init__(self, *args, **kwargs): - pass - send_cmd = __init__ - send_query = send_cmd - select_channel = send_cmd - send_binary_data = send_cmd - download_sequencer_table = send_cmd + super().__init__(*args, **kwargs) + select_channel = dummy_modules.dummy_teawg.TEWXAwg.send_cmd + instrument = None if pytabor not in dummy_modules.failed_imports: From c744b118470c88b43ec6fac7ac0940ad74ab4d4a Mon Sep 17 00:00:00 2001 From: "Pascal Cerfontaine (Lab PC)" Date: Tue, 11 Apr 2017 15:11:34 +0200 Subject: [PATCH 049/116] Fix error message --- qctoolkit/pulses/instructions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index 88ea683a4..a40705436 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -78,9 +78,9 @@ def get_sampled(self, A numpy.ndarray of the sampled values of this Waveform at the provided sample times. """ if numpy.any(sample_times[:-1] >= sample_times[1:]): - raise ValueError('The sample times are not in the range [0, duration]') - if sample_times[0] < 0 or sample_times[-1] > self.duration: raise ValueError('The sample times are not monotonously increasing') + if sample_times[0] < 0 or sample_times[-1] > self.duration: + raise ValueError('The sample times are not in the range [0, duration]') if channel not in self.defined_channels: raise KeyError('Channel not defined in this waveform: {}'.format(channel)) From 3cfc749805c5fabf16eb371bec72384abd6f5bba Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 11 Apr 2017 15:30:08 +0200 Subject: [PATCH 050/116] Multi channel plotting fix Corner case pulse template fix --- qctoolkit/pulses/plotting.py | 10 +++++++--- qctoolkit/pulses/table_pulse_template.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index ff39e3975..519ea993b 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -74,16 +74,17 @@ def get_waveform_generator(instruction_block): times[-1] = np.nextafter(times[-1], times[-2]) voltages = dict((ch, np.empty(len(times))) for ch in channels) - offset = 0 + offsets = {ch: 0 for ch in channels} for waveform in waveforms: for channel in channels: + offset = offsets[channel] indices = slice(*np.searchsorted(times, (offset, offset+waveform.duration))) sample_times = times[indices] - offset output_array = voltages[channel][indices] waveform.get_sampled(channel=channel, sample_times=sample_times, output_array=output_array) - offset += waveform.duration + offsets[channel] += waveform.duration return times, voltages @@ -115,7 +116,10 @@ def plot(pulse: PulseTemplate, parameters = dict() plotter = Plotter(sample_rate=sample_rate) sequencer = Sequencer() - sequencer.push(pulse, parameters, channel_mapping={ch: ch for ch in channels}) + sequencer.push(pulse, + parameters, + channel_mapping={ch: ch for ch in channels}, + window_mapping={w: w for w in pulse.measurement_names}) sequence = sequencer.build() if not sequencer.has_finished(): raise PlottingNotPossibleException(pulse) diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index d17c94530..d39062591 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -368,7 +368,7 @@ def __add_entry_check_and_modify_time(self, last_entry.t.name, last_entry.t.absolute_max_value, time.max_value ) ) - else: + if isinstance(time.min_value, numbers.Real) and isinstance(last_entry.t, numbers.Real): if time.min_value < last_entry.t: raise ValueError( "Argument time's minimum value {0} must be no smaller than the previous" From 93c5807ef5341c93596f4f5d22e813bb481d7a67 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 11 Apr 2017 15:35:15 +0200 Subject: [PATCH 051/116] Implement single waveform mode --- qctoolkit/hardware/awgs/tabor.py | 56 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 60aba7f8b..3a676f6b2 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -89,9 +89,6 @@ def markers(self) -> Tuple[Optional[ChannelID], Optional[ChannelID]]: def channels(self) -> Tuple[Optional[ChannelID], Optional[ChannelID]]: return self._channels - def setup_single_waveform_mode(self) -> None: - raise NotImplementedError() - def sampled_segments(self, sample_rate: float, voltage_amplitude: Tuple[float, float], @@ -142,6 +139,12 @@ def get_marker_data(waveform: MultiChannelWaveform, time): segments[i] = TaborSegment(segment_a, segment_b) return segments, segment_lengths + def setup_single_waveform_mode(self) -> None: + self.__waveform_mode = 'single' + self._waveforms = [self.program.waveform.get_subset_for_channels(self.__used_channels)] + self._sequencer_tables = [[(self.program.repetition_count, 1, 0)]] + self._advanced_sequencer_table = [] + def setup_single_sequence_mode(self) -> None: self.__waveform_mode = 'sequence' if len(self.program) < self.__device_properties['min_seq_len']: @@ -304,6 +307,8 @@ def waveform_mode(self) -> str: class TaborAWGRepresentation(teawg.TEWXAwg): def __init__(self, *args, external_trigger=False, reset=False, **kwargs): super().__init__(*args, **kwargs) + self._clock_marker = [0, 0, 0, 0] + if reset: self.visa_inst.write(':RES') @@ -680,34 +685,43 @@ def set_channel_state(self, channel, active) -> None: command_string = ':INST:SEL {}; :OUTP {}'.format(self._channels[channel], 'ON' if active else 'OFF') self._device.send_cmd(command_string) - @with_configuration_guard - def arm(self, name: str) -> None: + def arm(self, name: str): if self._current_program == name: self._device.send_cmd('SEQ:SEL 1') - return + else: + self.change_armed_program(name) + @with_configuration_guard + def change_armed_program(self, name: str) -> None: waveform_to_segment, program = self._known_programs[name] - sequencer_tables = program.get_sequencer_tables() - sequencer_tables = [[(rep_count, waveform_to_segment[wf_index], jump_flag) + # translate waveform number to actual segment + sequencer_tables = [[(rep_count, waveform_to_segment[wf_index-1], jump_flag) for (rep_count, wf_index, jump_flag) in sequencer_table] - for sequencer_table in sequencer_tables] + for sequencer_table in program.get_sequencer_tables()] + + # insert idle sequence sequencer_tables = [self._idle_sequence_table] + sequencer_tables - advanced_sequencer_table = program.get_advanced_sequencer_table() - advanced_sequencer_table = [(rep_count, seq_no+1, jump_flag) - for rep_count, seq_no, jump_flag in advanced_sequencer_table] - advanced_sequencer_table = [(1, 1, 1)] + advanced_sequencer_table - while len(advanced_sequencer_table) < self._device.dev_properties['min_aseq_len']: - advanced_sequencer_table.append((1, 1, 0)) + # adjust advanced sequence table entries by idle sequence table offset + advanced_sequencer_table = [(rep_count, seq_no + 1, jump_flag) + for rep_count, seq_no, jump_flag in program.get_advanced_sequencer_table()] - self.set_marker_state(0, False) - self.set_marker_state(1, False) + if program.waveform_mode == 'single': + assert len(advanced_sequencer_table) == 0 + assert len(sequencer_tables) == 2 + assert len(sequencer_tables[1]) == 1 - self.set_channel_state(0, False) - self.set_channel_state(1, False) + while len(sequencer_tables[1]) < self._device.dev_properties['min_seq_len']: + sequencer_tables[1].append((1, 1, 0)) - self._device.abort() + advanced_sequencer_table = [(1, 2, 0)] + + # insert idle waveform + advanced_sequencer_table = [(1, 1, 1)] + advanced_sequencer_table + + while len(advanced_sequencer_table) < self._device.dev_properties['min_aseq_len']: + advanced_sequencer_table.append((1, 1, 0)) #download all sequence tables for i, sequencer_table in enumerate(sequencer_tables): @@ -757,6 +771,8 @@ def _enter_config_mode(self) -> None: self.set_channel_state(0, False) self.set_channel_state(1, False) + self._device.abort() + self._device.send_cmd(':SOUR:FUNC:MODE FIX') self._is_in_config_mode = True From 57294ec8d912c7bdf940b4775e297ebeead3152c Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 12 Apr 2017 20:17:33 +0200 Subject: [PATCH 052/116] Parameter constraints and comparable expressions --- qctoolkit/expressions.py | 21 ++++++++- qctoolkit/pulses/parameters.py | 47 +++++++++++++++++++- tests/expression_tests.py | 73 ++++++++++++++++++++++++++++++++ tests/pulses/parameters_tests.py | 33 ++++++++++++++- 4 files changed, 170 insertions(+), 4 deletions(-) diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index 5555dbe2e..0d7131715 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -16,7 +16,7 @@ class Expression(Serializable, Comparable): """A mathematical expression instantiated from a string representation.""" - def __init__(self, ex: str) -> None: + def __init__(self, ex: Union[str, Number]) -> None: """Create an Expression object. Receives the mathematical expression which shall be represented by the object as a string @@ -47,6 +47,25 @@ def get_most_simple_representation(self) -> Union[str, int, float, complex]: else: return str(self.__expression) + def __lt__(self, other) -> Union[bool, None]: + result = self.__expression < (other.__expression if isinstance(other, Expression) else other) + return None if isinstance(result, sympy.Rel) else bool(result) + + def __gt__(self, other) -> Union[bool, None]: + result = self.__expression > (other.__expression if isinstance(other, Expression) else other) + return None if isinstance(result, sympy.Rel) else bool(result) + + def __ge__(self, other) -> Union[bool, None]: + result = self.__expression >= (other.__expression if isinstance(other, Expression) else other) + return None if isinstance(result, sympy.Rel) else bool(result) + + def __le__(self, other) -> Union[bool, None]: + result = self.__expression <= (other.__expression if isinstance(other, Expression) else other) + return None if isinstance(result, sympy.Rel) else bool(result) + + def __eq__(self, other) -> bool: + return self.__expression == (other.__expression if isinstance(other, Expression) else other) + @property def compare_key(self) -> sympy.Expr: return self.__expression diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index ce7b39e04..7c51eb815 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -10,7 +10,10 @@ """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Optional, Union, Dict, Any, Iterable +from typing import Optional, Union, Dict, Any, Iterable, Set +from numbers import Real + +import sympy from qctoolkit.serialization import Serializable, Serializer from qctoolkit.expressions import Expression @@ -24,7 +27,7 @@ def make_parameter(value): """Convenience function """ if isinstance(value, Parameter): return value - if isinstance(value, Number): + if isinstance(value, Real): return ConstantParameter(value) if isinstance(value, str): return MappedParameter(Expression(value)) @@ -173,6 +176,46 @@ def deserialize(serializer: Serializer, expression: str) -> 'MappedParameter': return MappedParameter(serializer.deserialize(expression)) +class ParameterConstraint(Comparable, Serializable): + def __init__(self, relation: str): + if '==' in relation: + # The '==' operator is interpreted by sympy as exactly, however we need a symbolical evaluation + self._relation = sympy.Eq(*sympy.sympify(relation.split('=='))) + else: + self._relation = sympy.sympify(relation) + if not isinstance(self._relation, (sympy.Rel, sympy.boolalg.BooleanAtom)): + raise ValueError('Constraint is no relation') + + @property + def affected_parameters(self) -> Set[str]: + return set(str(v) for v in self._relation.free_symbols) + + def is_fulfilled(self, parameter: Dict[str, Any]) -> bool: + return bool(self._relation.subs(parameter)) + + @property + def compare_key(self) -> sympy.Expr: + return self._relation + + def __str__(self): + if isinstance(self._relation, sympy.Eq): + return '{}=={}'.format(self._relation.lhs, self._relation.rhs) + else: + return str(self._relation) + + def get_serialization_data(self, serializer: 'Serializer'): + return dict(relation=str(self)) + + @staticmethod + def deserialize(serializer: 'Serializer', relation: str): + return ParameterConstraint(relation) + + +class ParameterConstraintViolation(Exception): + def __init__(self, constraint: ParameterConstraint, context: str): + super().__init__("The constraint '{}' is not fulfilled. ".format(constraint) + context) + + class ParameterDeclaration(Serializable, Comparable): """A declaration of a parameter required by a pulse template. diff --git a/tests/expression_tests.py b/tests/expression_tests.py index e6f29bcd4..737e14f26 100644 --- a/tests/expression_tests.py +++ b/tests/expression_tests.py @@ -50,3 +50,76 @@ def test_evaluate_variable_missing(self) -> None: } with self.assertRaises(ExpressionVariableMissingException): e.evaluate_numeric(**params) + + def test_undefined_comparison(self): + valued = Expression(2) + unknown = Expression('a') + + self.assertIsNone(unknown < 0) + self.assertIsNone(unknown > 0) + self.assertIsNone(unknown >= 0) + self.assertIsNone(unknown <= 0) + self.assertFalse(unknown == 0) + + self.assertIsNone(0 < unknown) + self.assertIsNone(0 > unknown) + self.assertIsNone(0 <= unknown) + self.assertIsNone(0 >= unknown) + self.assertFalse(0 == unknown) + + self.assertIsNone(unknown < valued) + self.assertIsNone(unknown > valued) + self.assertIsNone(unknown >= valued) + self.assertIsNone(unknown <= valued) + self.assertFalse(unknown == valued) + + valued, unknown = unknown, valued + self.assertIsNone(unknown < valued) + self.assertIsNone(unknown > valued) + self.assertIsNone(unknown >= valued) + self.assertIsNone(unknown <= valued) + self.assertFalse(unknown == valued) + valued, unknown = unknown, valued + + self.assertFalse(unknown == valued) + + def test_defined_comparison(self): + small = Expression(2) + large = Expression(3) + + self.assertIs(small < small, False) + self.assertIs(small > small, False) + self.assertIs(small <= small, True) + self.assertIs(small >= small, True) + self.assertIs(small == small, True) + + self.assertIs(small < large, True) + self.assertIs(small > large, False) + self.assertIs(small <= large, True) + self.assertIs(small >= large, False) + self.assertIs(small == large, False) + + self.assertIs(large < small, False) + self.assertIs(large > small, True) + self.assertIs(large <= small, False) + self.assertIs(large >= small, True) + self.assertIs(large == small, False) + + def test_number_comparison(self): + valued = Expression(2) + + self.assertIs(valued < 3, True) + self.assertIs(valued > 3, False) + self.assertIs(valued <= 3, True) + self.assertIs(valued >= 3, False) + + self.assertIs(valued == 3, False) + self.assertIs(valued == 2, True) + self.assertIs(3 == valued, False) + self.assertIs(2 == valued, True) + + self.assertIs(3 < valued, False) + self.assertIs(3 > valued, True) + self.assertIs(3 <= valued, False) + self.assertIs(3 >= valued, True) + diff --git a/tests/pulses/parameters_tests.py b/tests/pulses/parameters_tests.py index b4e9b35ee..7e08688aa 100644 --- a/tests/pulses/parameters_tests.py +++ b/tests/pulses/parameters_tests.py @@ -2,7 +2,7 @@ from typing import Union from qctoolkit.expressions import Expression -from qctoolkit.pulses.parameters import ConstantParameter, MappedParameter, ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException +from qctoolkit.pulses.parameters import ConstantParameter, MappedParameter, ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException, ParameterConstraint, ParameterConstraintViolation from tests.serialization_dummies import DummySerializer from tests.pulses.sequencing_dummies import DummyParameter @@ -96,6 +96,37 @@ def test_deserialize(self) -> None: self.assertIsNone(p.identifier) +class ParameterConstraintTest(unittest.TestCase): + def test_ordering(self): + constraint = ParameterConstraint('a <= b') + self.assertEqual(constraint.affected_parameters, {'a', 'b'}) + + self.assertTrue(constraint.is_fulfilled(dict(a=1, b=2))) + self.assertTrue(constraint.is_fulfilled(dict(a=2, b=2))) + self.assertFalse(constraint.is_fulfilled(dict(a=2, b=1))) + + def test_equal(self): + constraint = ParameterConstraint('a==b') + self.assertEqual(constraint.affected_parameters, {'a', 'b'}) + + self.assertTrue(constraint.is_fulfilled(dict(a=2, b=2))) + self.assertFalse(constraint.is_fulfilled(dict(a=3, b=2))) + + def test_expressions(self): + constraint = ParameterConstraint('Max(a, b) < a*c') + self.assertEqual(constraint.affected_parameters, {'a', 'b', 'c'}) + + self.assertTrue(constraint.is_fulfilled(dict(a=2, b=2, c=3))) + self.assertFalse(constraint.is_fulfilled(dict(a=3, b=5, c=1))) + + def test_no_relation(self): + with self.assertRaises(ValueError): + ParameterConstraint('a*b') + ParameterConstraint('1 < 2') + + + + class ParameterDeclarationTest(unittest.TestCase): def __test_valid_values(self, **kwargs): From c308c2e1e995683829ad7a22d004c00b3cf53310 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 12 Apr 2017 20:18:10 +0200 Subject: [PATCH 053/116] Begin of table pulse overhaul --- qctoolkit/pulses/pulse_template.py | 54 +- qctoolkit/pulses/table_pulse_template.py | 639 ++++++--------------- tests/pulses/table_pulse_template_tests.py | 446 +++++--------- 3 files changed, 381 insertions(+), 758 deletions(-) diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index 845c9840a..edd246851 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -9,10 +9,11 @@ from abc import ABCMeta, abstractmethod, abstractproperty from typing import Dict, List, Tuple, Set, Optional, Union, NamedTuple import itertools +from numbers import Real - -from qctoolkit import ChannelID +from qctoolkit import ChannelID, MeasurementWindow from qctoolkit.serialization import Serializable +from qctoolkit.expressions import Expression from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter from qctoolkit.pulses.sequencing import SequencingElement, InstructionBlock @@ -21,6 +22,11 @@ __all__ = ["PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException"] +MeasurementDeclaration = Tuple[str, + Union[Real, str, Expression], + Union[Real, str, Expression]] + + class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): """A PulseTemplate represents the parametrized general structure of a pulse. @@ -39,7 +45,6 @@ def __init__(self, identifier: Optional[str]=None) -> None: def parameter_names(self) -> Set[str]: """The set of names of parameters required to instantiate this PulseTemplate.""" - @abstractproperty def parameter_declarations(self) -> Set[ParameterDeclaration]: """The set of ParameterDeclaration objects detailing all parameters required to instantiate this PulseTemplate. @@ -113,8 +118,49 @@ class AtomicPulseTemplate(PossiblyAtomicPulseTemplate, metaclass=ABCMeta): Implies that no AtomicPulseTemplate object is interruptable. """ - def __init__(self, identifier: Optional[str]=None): + def __init__(self, + identifier: Optional[str]=None, + measurements: Optional[List[MeasurementDeclaration]]=None): super().__init__(identifier=identifier) + measurements = [] if measurements is None else measurements + self._measurement_windows = [(name, + begin if isinstance(begin, Expression) else Expression(begin), + length if isinstance(length, Expression) else Expression(length)) + for name, begin, length in measurements] + for _, _, length in self._measurement_windows: + if length.compare_key < 0 == True: + raise ValueError('Measurement window length may not be negative') + + def get_measurement_windows(self, + parameters: Dict[str, Real], + measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: + def get_val(v): + return v.evaluate_numeric(**parameters) + + resulting_windows = [(measurement_mapping[name], get_val(begin), get_val(length)) + for name, begin, length in self._measurement_windows] + + duration = get_val(self.duration) + for _, begin, length in resulting_windows: + if begin < 0 or length < 0 or duration < begin + length: + raise ValueError('Measurement window not in pulse or with negative length: {}, {}, {}'.format(begin, + length, + duration)) + return resulting_windows + + @property + def measurement_declarations(self): + """ + :return: Measurement declarations as added by the add_measurement_declaration method + """ + return [(name, + begin.get_most_simple_representation(), + end.get_most_simple_representation()) + for name, begin, end in self._measurement_windows] + + @property + def measurement_names(self) -> Set[str]: + return set(name for name, _, _ in self._measurement_windows) def is_interruptable(self) -> bool: return False diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index d39062591..7464d1ffd 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -9,15 +9,17 @@ from typing import Union, Dict, List, Set, Optional, NamedTuple, Any, Iterable, Tuple import numbers -import copy +import itertools +import warnings import numpy as np +import sympy from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.serialization import Serializer from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ - ParameterNotProvidedException -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate + ParameterNotProvidedException, ParameterConstraint, ParameterConstraintViolation +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.interpolation import InterpolationStrategy, LinearInterpolationStrategy, \ HoldInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.instructions import Waveform @@ -27,7 +29,8 @@ __all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] -WaveformTableEntry = NamedTuple( # pylint: disable=invalid-name + +WaveformTableEntry = NamedTuple( "WaveformTableEntry", [('t', float), ('v', float), ('interp', InterpolationStrategy)] ) @@ -46,13 +49,14 @@ def __init__(self, waveform_table (ImmutableList(WaveformTableEntry)): A list of instantiated table entries of the form (time as float, voltage as float, interpolation strategy). """ - if len(waveform_table) < 2: - raise ValueError("A given waveform table has less than two entries.") super().__init__() self.__table = tuple(waveform_table) self.__channel_id = channel self.__measurement_windows = tuple(measurement_windows) + if len(waveform_table) < 2: + raise ValueError("A given waveform table has less than two entries.") + @property def compare_key(self) -> Any: return self.__channel_id, self.__table, self.__measurement_windows @@ -85,389 +89,99 @@ def get_measurement_windows(self) -> Iterable[MeasurementWindow]: def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': return self +ValueInInit = Union[Expression, str, numbers.Real] +EntryInInit = Union['TableEntry', + Tuple[ValueInInit, ValueInInit], + Tuple[ValueInInit, ValueInInit, Union[str, InterpolationStrategy]]] -TableValue = Union[float, ParameterDeclaration] # pylint: disable=invalid-name -TableEntry = NamedTuple( # pylint: disable=invalid-name - "TableEntry", - [('t', TableValue), ('v', TableValue), ('interp', InterpolationStrategy)] -) -MeasurementDeclaration = Tuple[Union[float,Expression], Union[float,Expression]] +class TableEntry(tuple): + def __new__(cls, t: ValueInInit, v: ValueInInit, interp: Union[str, InterpolationStrategy]='hold'): + return tuple.__new__(cls, (t if isinstance(t, Expression) else Expression(t), + v if isinstance(v, Expression) else Expression(v), + interp if isinstance(interp, InterpolationStrategy) + else TablePulseTemplate.interpolation_strategies[interp])) -class TablePulseTemplate(AtomicPulseTemplate): - """Defines a pulse via interpolation of a sequence of (time,voltage)-pairs. + @property + def t(self) -> Expression: + return self[0] - TablePulseTemplate stores a list of (time,voltage)-pairs (the table) which is sorted - by time and uniquely define a pulse structure via interpolation of voltages of subsequent - table entries. - TablePulseTemplate provides methods to declare parameters which may be referred to instead of - using concrete values for both, time and voltage. - A TablePulseTemplate may be flagged as representing a measurement pulse, meaning that it defines - a measurement window. + @property + def v(self) -> Expression: + return self[1] - Each TablePulseTemplate contains at least an entry at time 0. - """ + @property + def interp(self) -> InterpolationStrategy: + return self[2] - def __init__(self, channels: List[ChannelID] = ['default'], identifier: Optional[str]=None) -> None: - """Create a new TablePulseTemplate. - Args: - channels (int): The list of channel identifiers defined in this TablePulseTemplate (default = 1). - measurement (bool): True, if this TablePulseTemplate shall define a measurement window. - (optional, default = False). - identifier (str): A unique identifier for use in serialization. (optional) - """ - if len(set(channels)) != len(channels): - raise ValueError('ChannelIDs must be unique') - - super().__init__(identifier) - self.__identifier = identifier - self.__interpolation_strategies = {'linear': LinearInterpolationStrategy(), - 'hold': HoldInterpolationStrategy(), - 'jump': JumpInterpolationStrategy() - } - self.__entries = dict((channel, [TableEntry(0, 0, self.__interpolation_strategies['hold'])]) - for channel in channels) - self.__time_parameter_declarations = {} # type: Dict[str, ParameterDeclaration] - self.__voltage_parameter_declarations = {} # type: Dict[str, ParameterDeclaration] - self.__measurement_windows = {} # type: Dict[str,List[MeasurementDeclaration]] +class TablePulseTemplate(AtomicPulseTemplate): + interpolation_strategies = {'linear': LinearInterpolationStrategy, + 'hold': HoldInterpolationStrategy, + 'jump': JumpInterpolationStrategy} + + def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], + identifier: Optional[str]=None, + parameter_constraints: Optional[List[str]]=None, + measurements: Optional[List[MeasurementDeclaration]]=None, + consistency_check=True): + super().__init__(identifier=identifier, measurements=measurements) + if parameter_constraints is None: + parameter_constraints = list() + + self._entries = dict((ch, list()) for ch in entries.keys()) + for channel, channel_entries in entries.items(): + if len(channel_entries) == 0: + raise ValueError('Channel {} is empty'.format(channel)) - @staticmethod - def from_array(times: np.ndarray, voltages: np.ndarray, channels: Optional[List[ChannelID]] = None) \ - -> 'TablePulseTemplate': - """Static constructor to build a TablePulse from numpy arrays. + for entry in channel_entries: + self._add_entry(channel, TableEntry(*entry)) + self._parameter_constraints = [ParameterConstraint(constraint) for constraint in parameter_constraints] - Args: - times: 1D numpy array with time values - voltages: 1D or 2D numpy array with voltage values - channels: list of channel IDs. Mandatory if voltages is 2D + if self.duration == 0: + warnings.warn('Table pulse template with duration 0 on construction.', + category=ZeroDurationTablePulseTemplate) - Returns: - TablePulseTemplate with the given values, hold interpolation everywhere and no free - parameters. - """ - res = TablePulseTemplate(channels=channels) if channels else TablePulseTemplate() - if voltages.ndim == 1: - for time, voltage in zip(times, voltages): - res.add_entry(time, voltage, interpolation='hold') - elif voltages.ndim == 2: - if not channels: - raise ValueError('For a multi channel table pulse template a list of channel IDs mut be provided.') - if len(channels) != voltages.shape[1]: - raise ValueError('There has to be exactly one channel ID for each channel.') - for channel_index in range(len(channels)): - for time, voltage in zip(times, voltages[:, channel_index]): - res.add_entry(time, voltage, interpolation='hold', channel=channels[channel_index]) - return res - - def add_entry(self, - time: Union[float, str, ParameterDeclaration], - voltage: Union[float, str, ParameterDeclaration], - interpolation: str='hold', - channel: Optional[ChannelID]=None) -> None: - """Add an entry to the end of this TablePulseTemplate. - - The arguments time and voltage may either be real numbers or a string which - references a parameter declaration by name or a ParameterDeclaration object. - - If the first entry provided to the table has a time > 0, a (0,0) entry is automatically - inserted in front. - - The following constraints hold: - - If a non-existing parameter declaration is referenced (via string), it is created without - min, max and default values. - - Parameter declarations for the time domain may not be used multiple times in the same - channel. - - If a ParameterDeclaration is provided, its min and max values will be set to its - neighboring values if they were not set previously or would exceed neighboring bounds. - - Parameter declarations for the time domain used in different channels will have their - bounds set such that range conforms with any channel automatically. - - ParameterDeclaration objects for the time domain may not refer to other - ParameterDeclaration objects as min or max values. - - Each entries time value must be greater than its predecessor's, i.e., - - if the time value is a float and the previous time value is a float, the new value - must be greater or equal - - if the time value is a float and the previous time value is a parameter declaration - and the new value is smaller then the maximum of the parameter declaration, the - maximum is adjusted to the new value - - if the time value is a float and the previous time value is a parameter declaration, - the new value must not be smaller than the minimum of the parameter declaration - - if the time value is a parameter declaration and the previous time value is a float, - the new values minimum must be no smaller - - if the time value is a parameter declaration and the previous time value is a - parameter declaration, the new minimum must not be smaller than the previous minimum - and the previous maximum must not be greater than the new maximum + if consistency_check: + # perform a simple consistency check. All inequalities with more than one free variable are ignored as the + # sympy solver does not support them - Args: - time (float or str or ParameterDeclaration): The time value of the new entry. Either a - constant real number or some parameter name/declaration. - voltage (float or str or ParameterDeclaration): The voltage value of the new entry. - Either a constant real number or some parameter name/declaration. - interpolation (str): The interpolation strategy between the previously last and the new - entry. One of 'linear', 'hold' (hold previous value) or 'jump' (jump immediately - to new value). - channel (int): The channel in which the voltage value will be set. (default = 0) - Raises: - ValueError if the constraints listed above are violated. - """ - # Check if channel is valid - if channel is None: - if len(self.__entries) == 1: - channel = next(iter(self.__entries.keys())) - else: - raise ValueError('Channel ID has to be specified if more than one channel is present') - elif channel not in self.__entries: - raise ValueError("Channel ID not known. Allowed values: {}".format( - ', '.join(self.__entries.keys())) - ) + # collect all conditions + inequalities = [sympy.sympify(eq) for eq in parameter_constraints] +\ + [sympy.Le(previous_entry.t.compare_key, entry.t.compare_key) + for channel_entries in self._entries.values() + for previous_entry, entry in zip(channel_entries, channel_entries[1:])] - # Check if interpolation value is valid - if interpolation not in self.__interpolation_strategies.keys(): - raise ValueError("Interpolation strategy not implemented. Allowed values: {}." - .format(', '.join(self.__interpolation_strategies.keys()))) - else: - interpolation = self.__interpolation_strategies[interpolation] - - entries = self.__entries[channel] - - last_entry = entries[-1] - # Handle time parameter/value - time = self.__add_entry_check_and_modify_time(time, entries) - - # Handle voltage parameter/value - # construct a ParameterDeclaration if voltage is a parameter name string - if isinstance(voltage, str): - voltage = ParameterDeclaration(voltage) - - # if voltage is (now) a ParameterDeclaration, make use of it - if isinstance(voltage, ParameterDeclaration): - # check whether a ParameterDeclaration with the same name already exists and, if so, - # use that instead such that the same object is used consistently for one declaration - if voltage.name in self.__voltage_parameter_declarations: - voltage = self.__voltage_parameter_declarations[voltage.name] - elif (voltage.name in self.__time_parameter_declarations or - (isinstance(time, ParameterDeclaration) and voltage.name == time.name)): - raise ValueError( - "Argument voltage <{}> must not refer to a time parameter declaration." - .format(voltage.name) - ) - - # no special action if voltage is a real number - - # add declarations to declaration sets if necessary - if isinstance(time, ParameterDeclaration): - self.__time_parameter_declarations[time.name] = time - if isinstance(voltage, ParameterDeclaration): - self.__voltage_parameter_declarations[voltage.name] = voltage - # in case we need a time 0 entry previous to the new entry - if not entries and (not isinstance(time, numbers.Real) or time > 0): - entries.append(last_entry) - # finally, add the new entry to the table - new_entry = TableEntry(time, voltage, interpolation) - if last_entry.t == time: - entries[-1] = new_entry - else: - entries.append(new_entry) - self.__entries[channel] = entries - - def __add_entry_check_and_modify_time(self, - time: Union[float, str, Parameter], - entries: List[TableEntry]) -> TableValue: - last_entry = entries[-1] - # Handle time parameter/value - - # first case: time is a real number - if isinstance(time, numbers.Real): - if isinstance(last_entry.t, ParameterDeclaration): - # set maximum value of previous entry if not already set - if last_entry.t.max_value == float('+inf'): - last_entry.t.max_value = time - - if time < last_entry.t.absolute_max_value: - try: - last_entry.t.max_value = time - except ValueError: - raise ValueError( - "Argument time must not be smaller than the minimum of the previous" - "parameter declaration ({}, was {}).".format( - last_entry.t.absolute_min_value, - time - ) - ) - - # if time is a real number, ensure that is it not less than the previous entry - elif time < last_entry.t: - raise ValueError( - "Argument time must not be less than previous time value {0}, was: {1}!".format( - last_entry.t, time - ) - ) - - # second case: time is a string -> Create a new ParameterDeclaration and continue third case - elif isinstance(time, str): - time = ParameterDeclaration(time) - - # third case: time is a ParameterDeclaration - # if time is (now) a ParameterDeclaration, verify it, insert it and establish - # references/dependencies to previous entries if necessary - if isinstance(time, ParameterDeclaration): - if time.name in self.__voltage_parameter_declarations: - raise ValueError( - "Cannot use already declared voltage parameter '{}' in time domain.".format( - time.name - ) - ) - if time.name in [e.t.name for e in entries if isinstance(e.t, ParameterDeclaration)]: - raise ValueError( - "A time parameter with the name {} already exists.".format(time.name) - ) - if time.name not in self.__time_parameter_declarations: - if isinstance(time.min_value, ParameterDeclaration): - raise ValueError( - "A ParameterDeclaration for a time parameter may not have a minimum value " - "reference to another ParameterDeclaration object." - ) - if isinstance(time.max_value, ParameterDeclaration): - raise ValueError( - "A ParameterDeclaration for a time parameter may not have a maximum value " - "reference to another ParameterDeclaration object." - ) - - # make a (shallow) copy of the ParameterDeclaration to ensure that it can't be - # changed from outside the table - time = ParameterDeclaration(time.name, min=time.min_value, max=time.max_value, - default=time.default_value) - # set minimum value if not previously set - # if last_entry.t is a ParameterDeclaration, its max_value field will be set - # accordingly by the min_value setter of the new entry, ensuring a correct boundary - # relationship between both declarations - if time.min_value == float('-inf'): - time.min_value = last_entry.t - else: - time = self.__time_parameter_declarations[time.name] - - # Check dependencies between successive time parameters - if isinstance(last_entry.t, ParameterDeclaration): - - if last_entry.t.max_value == float('inf'): - last_entry.t.max_value = time - - if time.absolute_min_value < last_entry.t.absolute_min_value: - raise ValueError( - "Argument time's minimum value must be no smaller than the previous " - "time parameter declaration's minimum value. Parameter '{0}', Minimum " - "{1}, Provided {2}.".format( - last_entry.t.name, last_entry.t.absolute_min_value, time.min_value - ) - ) - if time.absolute_max_value < last_entry.t.absolute_max_value: - raise ValueError( - "Argument time's maximum value must be no smaller than the previous " - "time parameter declaration's maximum value. Parameter '{0}', Maximum " - "{1}, Provided {2}.".format( - last_entry.t.name, last_entry.t.absolute_max_value, time.max_value - ) - ) - if isinstance(time.min_value, numbers.Real) and isinstance(last_entry.t, numbers.Real): - if time.min_value < last_entry.t: - raise ValueError( - "Argument time's minimum value {0} must be no smaller than the previous" - " time value {1}.".format(time.min_value, last_entry.t) - ) - return time - - @property - def entries(self) -> Union[List[TableEntry],Dict[ChannelID,List[TableEntry]]]: - """Immutable copies of this TablePulseTemplate's entries.""" - if len(self.__entries) == 1: - return copy.deepcopy(next(iter(self.__entries.values()))) - else: - return copy.deepcopy(self.__entries) + # test if any condition is already dissatisfied + if any(isinstance(eq, sympy.boolalg.BooleanAtom) and bool(eq) is False + for eq in inequalities): + raise ValueError('Table pulse template has impossible parametrization') - @property - def parameter_names(self) -> Set[str]: - """The set of names of declared parameters.""" - return set(self.__time_parameter_declarations.keys()) \ - | set(self.__voltage_parameter_declarations.keys()) + # filter conditions that are inequalities with one free variable and test if the solution set is empty + inequalities = [eq for eq in inequalities if isinstance(eq, sympy.Rel) and len(eq.free_symbols) == 1] + if not sympy.reduce_inequalities(inequalities): + raise ValueError('Table pulse template has impossible parametrization') - @property - def parameter_declarations(self) -> Set[ParameterDeclaration]: - """A set of all parameter declaration objects of this TablePulseTemplate.""" - return set(self.__time_parameter_declarations.values()) | \ - set(self.__voltage_parameter_declarations.values()) - - def get_measurement_windows(self, - parameters: Dict[str, Parameter], - measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: - def get_val(v): - return v if not isinstance(v, Expression) else v.evaluate_numeric( - **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] - for name_ in v.variables()}) - - t_max = [entry[-1][0] for entry in self.__entries.values()] - t_max = max([t if isinstance(t,numbers.Number) else t.get_value(parameters) for t in t_max]) - - resulting_windows = [] - for name, windows in self.__measurement_windows.items(): - for begin, end in windows: - resulting_windows.append((measurement_mapping[name], get_val(begin), get_val(end))) - if resulting_windows[-1][2] > t_max: - raise ValueError('Measurement window out of pulse') - return resulting_windows + def _add_entry(self, channel, new_entry: TableEntry): - @property - def measurement_declarations(self): - """ - :return: Measurement declarations as added by the add_measurement_declaration method - """ - as_builtin = lambda x: str(x) if isinstance(x, Expression) else x - return {name: [(as_builtin(begin), as_builtin(end)) - for begin, end in windows] - for name, windows in self.__measurement_windows.items()} - - @property - def measurement_names(self) -> Set[str]: - """ - :return: - """ - return set(self.__measurement_windows.keys()) - - def add_measurement_declaration(self, name: str, begin: Union[float,str], end: Union[float,str]) -> None: - if isinstance(begin,str): - begin = Expression(begin) - for v in begin.variables(): - if not v in self.__time_parameter_declarations: - if v in self.__voltage_parameter_declarations: - raise ValueError("Argument begin=<{}> must not refer to a voltage parameter declaration." - .format(str(begin))) - self.__time_parameter_declarations[v] = ParameterDeclaration(v) - if isinstance(end,str): - end = Expression(end) - for v in end.variables(): - if not v in self.__time_parameter_declarations: - if v in self.__voltage_parameter_declarations: - raise ValueError("Argument begin=<{}> must not refer to a voltage parameter declaration." - .format(str(end))) - self.__time_parameter_declarations[v] = ParameterDeclaration(v) - if name in self.__measurement_windows: - self.__measurement_windows[name].append((begin,end)) - else: - self.__measurement_windows[name] = [(begin,end)] + # comparisons with Expression can yield None -> use 'is True' and 'is False' + if (new_entry.t < 0) is True: + raise ValueError('Time parameter number {} of channel {} is negative.'.format( + len(self._entries[channel]), channel)) - @property - def is_interruptable(self) -> bool: - return False + for previous_entry in self._entries[channel]: + if (new_entry.t < previous_entry.t) is True: + raise ValueError('Time parameter number {} of channel {} is smaller than a previous one'.format( + len(self._entries[channel]), channel)) - @property - def defined_channels(self) -> Set[ChannelID]: - return set(self.__entries.keys()) + self._entries[channel].append(new_entry) @property - def num_channels(self) -> int: - return len(self.__entries) + def entries(self) -> Dict[ChannelID, List[TableEntry]]: + return self._entries - def get_entries_instantiated(self, parameters: Dict[str, Parameter]) \ - -> Dict[ChannelID, List[Tuple[float, float]]]: + def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ + -> Dict[ChannelID, List[WaveformTableEntry]]: """Compute an instantiated list of the table's entries. Args: @@ -476,50 +190,40 @@ def get_entries_instantiated(self, parameters: Dict[str, Parameter]) \ (float, float)-list of all table entries with concrete values provided by the given parameters. """ - instantiated_entries = dict() # type: Dict[ChannelID,List[Tuple[float, float]]] - max_time = 0 - - for channel, channel_entries in self.__entries.items(): - instantiated = [] - if not channel_entries: - instantiated.append(TableEntry(0, 0, self.__interpolation_strategies['hold'])) - else: - for entry in channel_entries: - # resolve time parameter references - if isinstance(entry.t, ParameterDeclaration): - time_value = entry.t.get_value(parameters) - else: - time_value = entry.t - if isinstance(entry.v, ParameterDeclaration): - voltage_value = entry.v.get_value(parameters) - else: - voltage_value = entry.v - - instantiated.append(TableEntry(time_value, voltage_value, entry.interp)) - max_time = max(max_time, instantiated[-1].t) - - # ensure that no time value occurs twice - previous_time = -1 - for (time, _, _) in instantiated: - if time <= previous_time: + instantiated_entries = dict() # type: Dict[ChannelID,List[WaveformTableEntry]] + + for channel, channel_entries in self._entries.items(): + instantiated = [WaveformTableEntry(entry.t.evaluate_numeric(**parameters), + entry.v.evaluate_numeric(**parameters), + entry.interp) + for entry in channel_entries] + + # Add (0, v) entry if wf starts at finite time + if instantiated[0].t > 0: + instantiated.insert(0, WaveformTableEntry(0, + instantiated[0], + TablePulseTemplate.interpolation_strategies['hold'])) + + for (previous_time, _, _), (time, _, _) in zip(instantiated, instantiated[1:]): + if time < previous_time: raise Exception("Time value {0} is smaller than the previous value {1}." .format(time, previous_time)) - previous_time = time - instantiated_entries[channel] = instantiated + duration = max(instantiated[-1].t for instantiated in instantiated_entries.values()) + # ensure that all channels have equal duration for channel, instantiated in instantiated_entries.items(): final_entry = instantiated[-1] - if final_entry.t != max_time: - instantiated.append(TableEntry(max_time, - final_entry.v, - self.__interpolation_strategies['hold'])) - instantiated_entries[channel] = TablePulseTemplate.__clean_entries(instantiated) + if final_entry.t < duration: + instantiated.append(WaveformTableEntry(duration, + final_entry.v, + TablePulseTemplate.interpolation_strategies['hold'])) + instantiated_entries[channel] = TablePulseTemplate._remove_redundant_entries(instantiated) return instantiated_entries @staticmethod - def __clean_entries(entries: List[Tuple[float, float]]) -> List[Tuple[float, float]]: + def _remove_redundant_entries(entries: List[WaveformTableEntry]) -> List[WaveformTableEntry]: """ Checks if three subsequent values in a list of table entries have the same value. If so, the intermediate is redundant and removed in-place. @@ -532,7 +236,7 @@ def __clean_entries(entries: List[Tuple[float, float]]) -> List[Tuple[float, flo if not entries or length < 3: return entries - for index in range(length-2, 0, -1): + for index in range(length - 2, 0, -1): previous_step = entries[index - 1] step = entries[index] next_step = entries[index + 1] @@ -540,22 +244,28 @@ def __clean_entries(entries: List[Tuple[float, float]]) -> List[Tuple[float, flo entries.pop(index) return entries - def build_waveform(self, - parameters: Dict[str, Parameter], - measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: - instantiated = [(channel_mapping[channel], instantiated_channel) - for channel, instantiated_channel in self.get_entries_instantiated(parameters).items()] - measurement_windows = self.get_measurement_windows(parameters=parameters, - measurement_mapping=measurement_mapping) + @property + def parameter_names(self) -> Set[str]: + return set( + var + for channel_entries in self.entries.values() + for entry in channel_entries + for var in itertools.chain(entry.t.variables(), entry.v.variables()) + ) | set(var for constraint in self._parameter_constraints for var in constraint.affected_parameters) - if len(instantiated) == 1: - return TableWaveform(*instantiated.pop(), measurement_windows) - else: - return MultiChannelWaveform( - [TableWaveform(*instantiated.pop(), measurement_windows)] - + - [TableWaveform(channel, instantiated_channel, [])for channel, instantiated_channel in instantiated]) + @property + def is_interruptable(self) -> bool: + return False + + @property + def duration(self) -> Expression: + return Expression('Max({})'.format(','.join( + (str(entries[-1].t) for entries in self._entries.values()) + ))) + + @property + def defined_channels(self) -> Set[ChannelID]: + return set(self._entries.keys()) def requires_stop(self, parameters: Dict[str, Parameter], @@ -569,57 +279,56 @@ def requires_stop(self, except KeyError as key_error: raise ParameterNotProvidedException(str(key_error)) from key_error + @property + def num_channels(self) -> int: + return len(self._entries) + def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - data = dict() - data['time_parameter_declarations'] = \ - [serializer.dictify(self.__time_parameter_declarations[key]) - for key in sorted(self.__time_parameter_declarations.keys())] - data['voltage_parameter_declarations'] = \ - [serializer.dictify(self.__voltage_parameter_declarations[key]) - for key in sorted(self.__voltage_parameter_declarations.keys())] - - serialized_entries = dict() - for channel, channel_entries in self.__entries.items(): - serialized_channel_entries = [] - for (time, voltage, interpolation) in channel_entries: - if isinstance(time, ParameterDeclaration): - time = time.name - if isinstance(voltage, ParameterDeclaration): - voltage = voltage.name - serialized_channel_entries.append((time, voltage, str(interpolation))) - serialized_entries[channel] = serialized_channel_entries - data['entries'] = serialized_entries - data['measurement_declarations'] = self.measurement_declarations - data['type'] = serializer.get_type_identifier(self) + data = dict( + entries=dict( + (channel, [(entry.t.get_most_simple_representation(), + entry.v.get_most_simple_representation(), + str(entry.interp)) for entry in channel_entries]) + for channel, channel_entries in self._entries.items() + ), + parameter_constraints=[str(constraint) for constraint in self._parameter_constraints] + ) return data @staticmethod def deserialize(serializer: Serializer, - time_parameter_declarations: Iterable[Any], - voltage_parameter_declarations: Iterable[Any], - entries: Dict[ChannelID,Any], - measurement_declarations: Dict[str,Iterable[Any]], + entries: Dict[ChannelID, List[EntryInInit]], + parameter_constraints: List[str], identifier: Optional[str]=None) -> 'TablePulseTemplate': - time_parameter_declarations = \ - {declaration['name']: serializer.deserialize(declaration) - for declaration in time_parameter_declarations} - voltage_parameter_declarations = \ - {declaration['name']: serializer.deserialize(declaration) - for declaration in voltage_parameter_declarations} + return TablePulseTemplate(entries=entries, + identifier=identifier, + parameter_constraints=parameter_constraints, + consistency_check=False) - template = TablePulseTemplate(channels=list(entries.keys()), - identifier=identifier) + def build_waveform(self, + parameters: Dict[str, numbers.Real], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + for constraint in self._parameter_constraints: + if not constraint.is_fulfilled(parameters): + affected_parameters = {name: parameters[name] for name in constraint.affected_parameters} + raise ParameterConstraintViolation(constraint, 'parameters: {}'.format(affected_parameters)) - for channel, channel_entries in entries.items(): - for (time, voltage, interpolation) in channel_entries: - if isinstance(time, str): - time = time_parameter_declarations[time] - if isinstance(voltage, str): - voltage = voltage_parameter_declarations[voltage] - template.add_entry(time, voltage, interpolation=interpolation, channel=channel) - - for name, windows in measurement_declarations.items(): - for window in windows: - template.add_measurement_declaration(name, *window) - - return template + instantiated = [(channel_mapping[channel], instantiated_channel) + for channel, instantiated_channel in self.get_entries_instantiated(parameters).items()] + measurement_windows = self.get_measurement_windows(parameters=parameters, + measurement_mapping=measurement_mapping) + if not measurement_windows and self.duration.evaluate_numeric(**parameters) == 0: + return None + + if len(instantiated) == 1: + return TableWaveform(*instantiated.pop(), measurement_windows) + else: + return MultiChannelWaveform( + [TableWaveform(*instantiated.pop(), measurement_windows)] + + + [TableWaveform(channel, instantiated_channel, [])for channel, instantiated_channel in instantiated]) + + +class ZeroDurationTablePulseTemplate(UserWarning): + pass diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 47ee4ce56..aa82fa86f 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -1,42 +1,40 @@ import unittest +import warnings + import numpy +from qctoolkit.expressions import Expression from qctoolkit.pulses.instructions import EXECInstruction -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry -from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry, ZeroDurationTablePulseTemplate +from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException, ParameterConstraintViolation from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyInterpolationStrategy, DummyParameter, DummyCondition from tests.serialization_dummies import DummySerializer -class TablePulseTemplateTest(unittest.TestCase): - def test_add_entry_known_interpolation_strategies(self) -> None: - table = TablePulseTemplate() - strategies = ["linear", "hold", "jump"] - for i,strategy in enumerate(strategies): - table.add_entry(i, i, strategy) +class TableEntryTest(unittest.TestCase): + def test_known_interpolation_strategies(self): + strategies = [("linear", LinearInterpolationStrategy()), + ("hold", HoldInterpolationStrategy()), + ("jump", JumpInterpolationStrategy())] - manual = [(0,0,LinearInterpolationStrategy()), (1,1,HoldInterpolationStrategy()), (2,2,JumpInterpolationStrategy())] - self.assertEqual(manual, table.entries) + for strat_name, strat_val in strategies: + entry = TableEntry('a', Expression('b'), strat_name) - def test_add_entry_unknown_interpolation_strategy(self) -> None: - table = TablePulseTemplate() - with self.assertRaises(ValueError): - table.add_entry(0, 0, 'foo') - with self.assertRaises(ValueError): - table.add_entry(3.2, 0, 'foo') + self.assertEqual(entry.t, Expression('a')) + self.assertEqual(entry.v, Expression('b')) + self.assertEqual(entry.interp, strat_val) - def test_add_entry_for_interpolation(self) -> None: - table = TablePulseTemplate() - strategies = ["linear","hold","jump","hold"] - for i,strategy in enumerate(strategies): - table.add_entry(2*(i+1), i+1, strategy) + def test_unknown_interpolation_strategy(self): + with self.assertRaises(KeyError): + TableEntry(0, 0, 'foo') - with self.assertRaises(ValueError): - table.add_entry(1,2, "bar") +class TablePulseTemplateTest(unittest.TestCase): + + @unittest.skip('Move to AtomicPulseTemplate test') def test_measurement_windows(self) -> None: pulse = TablePulseTemplate() pulse.add_entry(1, 1) @@ -47,6 +45,7 @@ def test_measurement_windows(self) -> None: self.assertEqual([('asd', 0, 5)], windows) self.assertEqual(pulse.measurement_declarations, dict(mw=[(0, 5)])) + @unittest.skip('Move to AtomicPulseTemplate test') def test_no_measurement_windows(self) -> None: pulse = TablePulseTemplate() pulse.add_entry(1, 1) @@ -56,6 +55,7 @@ def test_no_measurement_windows(self) -> None: self.assertEqual([], windows) self.assertEqual(dict(), pulse.measurement_declarations) + @unittest.skip('Move to AtomicPulseTemplate test') def test_measurement_windows_with_parameters(self) -> None: pulse = TablePulseTemplate() pulse.add_entry(1, 1) @@ -66,6 +66,7 @@ def test_measurement_windows_with_parameters(self) -> None: self.assertEqual(windows, [('asd', 1, 101/2)]) self.assertEqual(pulse.measurement_declarations, dict(mw=[(1, '(1+length)/2')])) + @unittest.skip('Move to AtomicPulseTemplate test') def test_multiple_measurement_windows(self) -> None: pulse = TablePulseTemplate() pulse.add_entry(1, 1) @@ -85,305 +86,168 @@ def test_multiple_measurement_windows(self) -> None: dict(A=[(0, '(1+length)/2'), (1, 3)], B=[('begin', 2)])) - def test_add_entry_empty_time_is_negative(self) -> None: - table = TablePulseTemplate() + def test_time_is_negative(self) -> None: with self.assertRaises(ValueError): - table.add_entry(-2, 0) - self.assertEqual([TableEntry(0, 0, HoldInterpolationStrategy())], table.entries) - #self.assertFalse(table.entries[0]) - self.assertFalse(table.parameter_declarations) - self.assertFalse(table.parameter_names) - - def test_add_entry_empty_time_is_0(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 3.1) - self.assertEqual([(0, 3.1, HoldInterpolationStrategy())], table.entries) - self.assertFalse(table.parameter_names) - self.assertFalse(table.parameter_declarations) - - def test_add_entry_empty_time_is_0_voltage_is_parameter(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 'foo') - decl = ParameterDeclaration('foo') - self.assertEqual([(0, decl, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) + TablePulseTemplate({0: [(1, 2), + (2, 3), + (-1, 3)]}) - def test_add_entry_empty_time_is_positive(self) -> None: - table = TablePulseTemplate() - table.add_entry(2, -254.67) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (2, -254.67, HoldInterpolationStrategy())], table.entries) - self.assertFalse(table.parameter_names) - self.assertFalse(table.parameter_declarations) - - def test_add_entry_empty_time_is_str(self) -> None: - table = TablePulseTemplate() - table.add_entry('t', 0) - decl = ParameterDeclaration('t', min=0) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 0, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) - - def test_add_entry_empty_time_is_declaration(self) -> None: - table = TablePulseTemplate() - decl = ParameterDeclaration('foo') - table.add_entry(decl, 0) - decl.min_value = 0 - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 0, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) - - def test_add_entry_time_float_after_float(self) -> None: - table = TablePulseTemplate() - table.add_entry(1.2, -3.8) - # expect ValueError if next float is smaller than previous with self.assertRaises(ValueError): - table.add_entry(0.423, 0) - # adding a higher value or equal value as last entry should work - table.add_entry(1.2, 2.5252) - table.add_entry(3.7, 1.34875) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (1.2, 2.5252, HoldInterpolationStrategy()), (3.7, 1.34875, HoldInterpolationStrategy())], table.entries) - self.assertFalse(table.parameter_names) - self.assertFalse(table.parameter_declarations) - - def test_add_entry_time_float_after_declaration_no_bound(self) -> None: - table = TablePulseTemplate() - table.add_entry('t', 7.1) - table.add_entry(2.1, 5.5) - decl = ParameterDeclaration('t', min=0, max=2.1) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) - - def test_add_entry_time_float_after_declaration_greater_bound(self) -> None: - table = TablePulseTemplate() - decl = ParameterDeclaration('t', max=3.4) - table.add_entry(decl, 7.1) - table.add_entry(2.1, 5.5) - expected_decl = ParameterDeclaration('t', min=0, max=2.1) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (expected_decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({expected_decl}, table.parameter_declarations) - - def test_add_entry_time_float_after_declaration_smaller_bound(self) -> None: - table = TablePulseTemplate() - decl = ParameterDeclaration('t', min=1.0, max=1.3) - table.add_entry(decl, 7.1) - table.add_entry(2.1, 5.5) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 7.1, HoldInterpolationStrategy()), (2.1, 5.5, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) - - def test_add_entry_time_float_after_declaration_smaller_than_min_bound(self) -> None: - table = TablePulseTemplate() - decl = ParameterDeclaration('t', min=1.2, max=83456.2) - table.add_entry(decl, 2.2) - with self.assertRaises(ValueError): - table.add_entry(1.1, -6.3) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (decl, 2.2, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) + TablePulseTemplate({0: [(-1, 2), + (2, 3), + (3, 3)]}) - def test_add_entry_time_parameter_name_in_use_as_voltage(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 'foo') - foo_decl = ParameterDeclaration('foo') - self.assertEqual({'foo'}, table.parameter_names) - self.assertEqual({foo_decl}, table.parameter_declarations) + def test_time_not_increasing(self): with self.assertRaises(ValueError): - table.add_entry('foo', 4.3) - self.assertEqual({'foo'}, table.parameter_names) - self.assertEqual({foo_decl}, table.parameter_declarations) - self.assertEqual([(0, foo_decl, HoldInterpolationStrategy())], table.entries) + TablePulseTemplate({0: [(1, 2), + (2, 3), + (1.9, 3), + (3, 1.1)]}) - def test_add_entry_time_parmeter_name_in_use_as_time(self) -> None: - table = TablePulseTemplate() - table.add_entry('foo', 'bar') - foo_decl = ParameterDeclaration('foo', min=0) - bar_decl = ParameterDeclaration('bar') with self.assertRaises(ValueError): - table.add_entry(ParameterDeclaration('foo'), 3.4) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (foo_decl, bar_decl, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo', 'bar'}, table.parameter_names) - self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) + TablePulseTemplate({0: [('a', 2), + (2, 3), + (1.9, 3), + ('b', 1.1)]}) - def test_add_entry_time_declaration_invalid_bounds(self) -> None: - table = TablePulseTemplate() - bar_decl = ParameterDeclaration('bar') - foo_decl = ParameterDeclaration('foo') - foo_decl.min_value = bar_decl - with self.assertRaises(ValueError): - table.add_entry(foo_decl, 23857.23) with self.assertRaises(ValueError): - table.add_entry(bar_decl, -4967.1) - self.assertEquals([TableEntry(0, 0, HoldInterpolationStrategy())], table.entries) - self.assertFalse(table.parameter_names) - self.assertFalse(table.parameter_declarations) + TablePulseTemplate({0: [(2, 3), + ('a', 2), + (1.9, 3), + ('b', 1.1)]}) - def test_add_entry_time_declaration_no_bounds_after_float(self) -> None: - table = TablePulseTemplate() - table.add_entry(3.2, 92.1) - table.add_entry('t', 1.2) - decl = ParameterDeclaration('t', min=3.2) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy()), (decl, 1.2, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) - - def test_add_entry_time_declaration_higher_min_after_float(self) -> None: - table = TablePulseTemplate() - table.add_entry(3.2, 92.1) - decl = ParameterDeclaration('t', min=4.5) - table.add_entry(decl, 1.2) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy()), (decl, 1.2, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'t'}, table.parameter_names) - self.assertEqual({decl}, table.parameter_declarations) - - def test_add_entry_time_declaration_lower_min_after_float(self) -> None: - table = TablePulseTemplate() - table.add_entry(3.2, 92.1) - decl = ParameterDeclaration('t', min=0.1) with self.assertRaises(ValueError): - table.add_entry(decl, 1.2) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (3.2, 92.1, HoldInterpolationStrategy())], table.entries) - self.assertFalse(table.parameter_names) - self.assertFalse(table.parameter_declarations) - - def test_add_entry_time_declaration_after_declaration_no_upper_bound(self) -> None: - table = TablePulseTemplate() - table.add_entry('bar', 72.14) - table.add_entry('foo', 0) - bar_decl = ParameterDeclaration('bar', min=0) - foo_decl = ParameterDeclaration('foo') - foo_decl.min_value = bar_decl - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, 72.14, HoldInterpolationStrategy()), (foo_decl, 0, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'bar', 'foo'}, table.parameter_names) - self.assertEqual({bar_decl, foo_decl}, table.parameter_declarations) - - def test_add_entry_time_declaration_after_declaration_upper_bound(self) -> None: - table = TablePulseTemplate() - bar_decl = ParameterDeclaration('bar', min=1, max=2) - foo_decl = ParameterDeclaration('foo') - table.add_entry(bar_decl, -3) - table.add_entry(foo_decl, 0.1) - foo_decl.min_value = bar_decl - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo', 'bar'}, table.parameter_names) - self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) - - def test_add_entry_time_declaration_lower_bound_after_declaration_upper_bound(self) -> None: - table = TablePulseTemplate() - bar_decl = ParameterDeclaration('bar', min=1, max=2) - foo_decl = ParameterDeclaration('foo', min=1) - table.add_entry(bar_decl, -3) - table.add_entry(foo_decl, 0.1) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo', 'bar'}, table.parameter_names) - self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) + TablePulseTemplate({0: [(1, 2), + (2, 3), + (1.9, 3)]}) - def test_add_entry_time_declaration_lower_bound_after_declaration_no_upper_bound(self) -> None: - table = TablePulseTemplate() - self.maxDiff = None - bar_decl = ParameterDeclaration('bar', min=1) - foo_decl = ParameterDeclaration('foo', min=1) - table.add_entry(bar_decl, -3) - table.add_entry(foo_decl, 0.1) - bar_decl.max_value = foo_decl - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy()), (foo_decl, 0.1, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo', 'bar'}, table.parameter_names) - self.assertEqual({foo_decl, bar_decl}, table.parameter_declarations) - - def test_add_entry_time_declaration_lower_bound_too_small_after_declaration_no_upper_bound(self) -> None: - table = TablePulseTemplate() - bar_decl = ParameterDeclaration('bar', min=1, max=5) - foo_decl = ParameterDeclaration('foo', min=0) - table.add_entry(bar_decl, -3) with self.assertRaises(ValueError): - table.add_entry(foo_decl, 0.1) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'bar'}, table.parameter_names) - self.assertEqual({bar_decl}, table.parameter_declarations) + TablePulseTemplate({0: [(2, 3), + (1.9, 'k')]}) - def test_add_entry_time_declaration_no_lower_bound_upper_bound_too_small_after_declaration(self) -> None: - table = TablePulseTemplate() - bar_decl = ParameterDeclaration('bar', min=1, max=2) - foo_decl = ParameterDeclaration('foo', max=1) - table.add_entry(bar_decl, -3) with self.assertRaises(ValueError): - table.add_entry(foo_decl, 0.1) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'bar'}, table.parameter_names) - self.assertEqual({bar_decl}, table.parameter_declarations) + TablePulseTemplate({0: [('a', 3), + (2, 'n'), + (3, 'm'), + ('a', 'k')]}) - def test_add_entry_time_declaration_lower_bound_upper_bound_too_small_after_declaration(self) -> None: - table = TablePulseTemplate() - bar_decl = ParameterDeclaration('bar', min=1, max=2) - foo_decl = ParameterDeclaration('foo', min=1, max=1.5) - table.add_entry(bar_decl, -3) + def test_inconsistent_parameters(self): with self.assertRaises(ValueError): - table.add_entry(foo_decl, 0.1) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (bar_decl, -3, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'bar'}, table.parameter_names) - self.assertEqual({bar_decl}, table.parameter_declarations) + TablePulseTemplate({0: [('a', 1), + (2, 0)], + 1: [(3, 6), + ('a', 7)]}) - def test_add_entry_voltage_declaration_reuse(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo', min=0, max=3.3) - bar_decl = ParameterDeclaration('bar', min=-3.3, max=1.15) - table.add_entry(0, foo_decl) - table.add_entry(1.51, bar_decl) - table.add_entry(3, 'foo') - table.add_entry('t', foo_decl) - t_decl = ParameterDeclaration('t', min=3) - self.assertEqual([(0, foo_decl, HoldInterpolationStrategy()), (1.51, bar_decl, HoldInterpolationStrategy()), - (3, foo_decl, HoldInterpolationStrategy()), (t_decl, foo_decl, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo', 'bar', 't'}, table.parameter_names) - self.assertEqual({foo_decl, bar_decl, t_decl}, table.parameter_declarations) - - def test_add_entry_voltage_declaration_in_use_as_time(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo', min=0, max=2) - table.add_entry(foo_decl, 0) with self.assertRaises(ValueError): - table.add_entry(4, foo_decl) - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (foo_decl, 0, HoldInterpolationStrategy())], table.entries) - self.assertEqual({'foo'}, table.parameter_names) - self.assertEqual({foo_decl}, table.parameter_declarations) + TablePulseTemplate({0: [('a', 1), + (2, 0)]}, parameter_constraints=['a>3']) - def test_add_entry_time_and_voltage_same_declaration(self) -> None: - table = TablePulseTemplate() + @unittest.skip(reason='Needs a better inequality solver') + def test_time_not_increasing_hard(self): with self.assertRaises(ValueError): - table.add_entry('foo', 'foo') - self.assertEquals([TableEntry(0, 0, HoldInterpolationStrategy())], table.entries) - self.assertFalse(table.parameter_names) - self.assertFalse(table.parameter_declarations) + TablePulseTemplate({0: [('a*c', 3), + ('b', 1), + ('c*a', 'k')]}, parameter_constraints=['a*c < b']) + + + + def test_time_is_0_on_construction(self) -> None: + with self.assertWarns(ZeroDurationTablePulseTemplate): + warnings.simplefilter('default', ZeroDurationTablePulseTemplate) + table = TablePulseTemplate({0: [(0, 1.4)]}) + self.assertTrue(table.duration == 0) + self.assertTrue(table.duration == 0) + + self.assertIsNone(table.build_waveform(parameters=dict(), + measurement_mapping=dict(), + channel_mapping={0: 0})) + + def test_time_is_0_on_instantiation(self): + table = TablePulseTemplate({0: [('a', 1)]}) + self.assertEqual(table.duration, Expression('a')) + self.assertEqual(table.parameter_names, {'a'}) + + self.assertIsNone(table.build_waveform(parameters=dict(a=0), + measurement_mapping=dict(), + channel_mapping={0: 0})) + + def test_single_channel_no_parameters(self): + raw_entries = [(0., 1.1), (1.1, 2.), (2.2, 2.4)] + table = TablePulseTemplate({0: raw_entries}) + expected = [TableEntry(*entry) for entry in raw_entries] + self.assertEqual(table.entries, dict([(0, expected)])) + self.assertEqual(table.duration, 2.2) + self.assertEqual(table.parameter_names, {}) + + def test_single_channel_no_parameters(self): + raw_entries = [(0., 1.1), (1.1, 2.), (2.2, 2.4)] + table = TablePulseTemplate({0: raw_entries}) + expected = [TableEntry(*entry) for entry in raw_entries] + self.assertEqual(table.entries, dict([(0, expected)])) + self.assertEqual(table.duration, 2.2) + self.assertEqual(table.parameter_names, {}) + + def test_internal_constraints(self): + table = TablePulseTemplate({0: [(1, 'v'), (2, 'w')], + 1: [('t', 'x'), ('t+2', 'y')]}, + parameter_constraints=['x<2', 'y None: - self.assertFalse(TablePulseTemplate().is_interruptable) + self.assertFalse(TablePulseTemplate({0: [(1, 1)]}).is_interruptable) def test_get_entries_instantiated_one_entry_float_float(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 2) - instantiated_entries = table.get_entries_instantiated({})['default'] + table = TablePulseTemplate({0: [(0, 2)]}) + instantiated_entries = table.get_entries_instantiated({})[0] self.assertEqual([(0, 2, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_one_entry_float_declaration(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 'foo') - instantiated_entries = table.get_entries_instantiated({'foo': 2})['default'] + table = TablePulseTemplate({0: [(0, 'foo')]}) + instantiated_entries = table.get_entries_instantiated({'foo': 2})[0] self.assertEqual([(0, 2, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_two_entries_float_float_declaration_float(self) -> None: - table = TablePulseTemplate() - table.add_entry('foo', -3.1415) - instantiated_entries = table.get_entries_instantiated({'foo': 2})['default'] - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (2, -3.1415, HoldInterpolationStrategy())], instantiated_entries) + table = TablePulseTemplate({0: [('foo', -2.)]}) + instantiated_entries = table.get_entries_instantiated({'foo': 2})[0] + self.assertEqual([(0, -2., HoldInterpolationStrategy()), + (2, -2., HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_two_entries_float_declaraton_declaration_declaration(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 'v1') - table.add_entry('t', 'v2') - instantiated_entries = table.get_entries_instantiated({'v1': -5, 'v2': 5, 't': 3})['default'] - self.assertEqual([(0, -5, HoldInterpolationStrategy()), (3, 5, HoldInterpolationStrategy())], instantiated_entries) + table = TablePulseTemplate({0: [(0, 'v1'), + ('t', 'v2')]}) + instantiated_entries = table.get_entries_instantiated({'v1': -5, 'v2': 5, 't': 3})[0] + self.assertEqual([(0, -5, HoldInterpolationStrategy()), + (3, 5, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_two_entries_invalid_parameters(self) -> None: table = TablePulseTemplate() @@ -652,6 +516,7 @@ def test_measurement_windows_multi_out_of_pulse(self) -> None: pulse.get_measurement_windows({'t_meas': 20}, measurement_mapping={'mw': 'asd'}) +@unittest.skip class TablePulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: @@ -709,6 +574,7 @@ def test_deserialize(self) -> None: self.assertEqual('foo', template.identifier) +@unittest.skip class TablePulseTemplateSequencingTests(unittest.TestCase): def test_build_sequence(self) -> None: @@ -836,6 +702,7 @@ def test_build_sequence_multi_one_channel_empty(self) -> None: self.assertEqual(expected_waveform, waveform) +@unittest.skip class TableWaveformDataTests(unittest.TestCase): def test_duration(self) -> None: @@ -895,6 +762,7 @@ def test_simple_properties(self): self.assertIs(waveform.unsafe_get_subset_for_channels('A'), waveform) +@unittest.skip class ParameterValueIllegalExceptionTest(unittest.TestCase): def test(self) -> None: From 385144dff0f88384698c4b31e5153dbf2c59dd04 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 19 Apr 2017 10:36:25 +0200 Subject: [PATCH 054/116] Make expression naming more consistent and add repr and str tests --- qctoolkit/expressions.py | 68 ++++++++++++++++++++++----------------- tests/expression_tests.py | 16 ++++++++- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index 0d7131715..5f6712059 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -27,56 +27,66 @@ def __init__(self, ex: Union[str, Number]) -> None: ex (string): The mathematical expression represented as a string """ super().__init__() - self.__string = str(ex) - self.__expression = sympy.sympify(ex) - self.__variables = tuple(str(var) for var in self.__expression.free_symbols) - self.__expression_lambda = sympy.lambdify(self.variables(), self.__expression, 'numpy') + self._original_expression = ex + self._sympified_expression = sympy.sympify(ex) + self._variables = tuple(str(var) for var in self._sympified_expression.free_symbols) + self._expression_lambda = sympy.lambdify(self._variables, + self._sympified_expression, 'numpy') def __str__(self) -> str: - return self.__string + return str(self._sympified_expression) + + def __repr__(self) -> str: + return 'Expression({})'.format(self._original_expression) def get_most_simple_representation(self) -> Union[str, int, float, complex]: - if self.__expression.free_symbols: - return str(self.__expression) - elif self.__expression.is_integer: - return int(self.__expression) - elif self.__expression.is_complex: - return complex(self.__expression) - elif self.__expression.is_real: - return float(self.__expression) + if self._sympified_expression.free_symbols: + return str(self._sympified_expression) + elif self._sympified_expression.is_integer: + return int(self._sympified_expression) + elif self._sympified_expression.is_complex: + return complex(self._sympified_expression) + elif self._sympified_expression.is_real: + return float(self._sympified_expression) else: - return str(self.__expression) + return self._original_expression - def __lt__(self, other) -> Union[bool, None]: - result = self.__expression < (other.__expression if isinstance(other, Expression) else other) + def __lt__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: + result = self._sympified_expression < (other._sympified_expression if isinstance(other, Expression) else other) return None if isinstance(result, sympy.Rel) else bool(result) - def __gt__(self, other) -> Union[bool, None]: - result = self.__expression > (other.__expression if isinstance(other, Expression) else other) + def __gt__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: + result = self._sympified_expression > (other._sympified_expression if isinstance(other, Expression) else other) return None if isinstance(result, sympy.Rel) else bool(result) - def __ge__(self, other) -> Union[bool, None]: - result = self.__expression >= (other.__expression if isinstance(other, Expression) else other) + def __ge__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: + result = self._sympified_expression >= (other._sympified_expression if isinstance(other, Expression) else other) return None if isinstance(result, sympy.Rel) else bool(result) - def __le__(self, other) -> Union[bool, None]: - result = self.__expression <= (other.__expression if isinstance(other, Expression) else other) + def __le__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: + result = self._sympified_expression <= (other._sympified_expression if isinstance(other, Expression) else other) return None if isinstance(result, sympy.Rel) else bool(result) - def __eq__(self, other) -> bool: - return self.__expression == (other.__expression if isinstance(other, Expression) else other) + def __eq__(self, other: Union['Expression', Number, sympy.Expr]) -> bool: + """Overwrite Comparable's test for equality to incorporate comparisons with Numbers""" + return self._sympified_expression == (other._sympified_expression if isinstance(other, Expression) else other) @property def compare_key(self) -> sympy.Expr: - return self.__expression + return self._sympified_expression + + @property + def original_expression(self) -> Union[str, Number]: + return self._original_expression + @property def variables(self) -> Iterable[str]: """ Get all free variables in the expression. Returns: A collection of all free variables occurring in the expression. """ - return self.__variables + return self._variables def evaluate_numeric(self, **kwargs) -> Union[Number, numpy.ndarray]: """Evaluate the expression with the required variables passed in as kwargs. @@ -93,7 +103,7 @@ def evaluate_numeric(self, **kwargs) -> Union[Number, numpy.ndarray]: """ try: # drop irrelevant variables before passing to lambda - result = self.__expression_lambda(**dict((v, kwargs[v]) for v in self.variables())) + result = self._expression_lambda(**dict((v, kwargs[v]) for v in self.variables)) except KeyError as key_error: raise ExpressionVariableMissingException(key_error.args[0], self) from key_error @@ -111,10 +121,10 @@ def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> 'Expression Returns: """ - return Expression(self.__expression.subs(substitutions)) + return Expression(self._sympified_expression.subs(substitutions)) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - return dict(expression=str(self)) + return dict(expression=self.original_expression) @staticmethod def deserialize(serializer: 'Serializer', **kwargs) -> Serializable: diff --git a/tests/expression_tests.py b/tests/expression_tests.py index 737e14f26..6482b11de 100644 --- a/tests/expression_tests.py +++ b/tests/expression_tests.py @@ -40,7 +40,7 @@ def test_evaluate_symbolic(self): def test_variables(self) -> None: e = Expression('4 ** pi + x * foo') expected = sorted(['foo', 'x']) - received = sorted(e.variables()) + received = sorted(e.variables) self.assertEqual(expected, received) def test_evaluate_variable_missing(self) -> None: @@ -51,6 +51,20 @@ def test_evaluate_variable_missing(self) -> None: with self.assertRaises(ExpressionVariableMissingException): e.evaluate_numeric(**params) + def test_repr(self): + s = 'a * b' + e = Expression(s) + self.assertEqual('Expression(a * b)', repr(e)) + + def test_str(self): + s = 'a * b' + e = Expression(s) + self.assertEqual('a*b', str(e)) + + def test_original_expression(self): + s = 'a * b' + self.assertEqual(Expression(s).original_expression, s) + def test_undefined_comparison(self): valued = Expression(2) unknown = Expression('a') From 83f85b70887271a8d6d47e7076c1fef75a02fd14 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 19 Apr 2017 12:28:43 +0200 Subject: [PATCH 055/116] Fix most unittests FunctionPT uses AtomicPT meas functionality --- qctoolkit/pulses/function_pulse_template.py | 79 +-- qctoolkit/pulses/loop_pulse_template.py | 2 +- .../pulses/multi_channel_pulse_template.py | 20 +- qctoolkit/pulses/parameters.py | 2 +- qctoolkit/pulses/pulse_template.py | 34 +- .../pulse_template_parameter_mapping.py | 4 +- qctoolkit/pulses/table_pulse_template.py | 92 ++- tests/pulses/function_pulse_tests.py | 52 +- tests/pulses/plotting_tests.py | 4 +- tests/pulses/pulse_template_tests.py | 93 ++- tests/pulses/sequence_pulse_template_tests.py | 31 +- tests/pulses/table_pulse_template_tests.py | 622 ++++++------------ ...e_sequence_sequencer_intergration_tests.py | 20 +- tests/serialization_tests.py | 13 +- 14 files changed, 473 insertions(+), 595 deletions(-) diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index b45c9c57f..272879335 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -18,7 +18,7 @@ from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.pulse_template_parameter_mapping import ParameterNotProvidedException @@ -41,6 +41,7 @@ class FunctionPulseTemplate(AtomicPulseTemplate): def __init__(self, expression: Union[str, Expression], duration_expression: Union[str, Expression], + measurements: Optional[List[MeasurementDeclaration]]=None, identifier: str=None, channel: 'ChannelID' = 'default') -> None: """Create a new FunctionPulseTemplate instance. @@ -56,22 +57,26 @@ def __init__(self, window. (optional, default = False) identifier (str): A unique identifier for use in serialization. (optional) """ - super().__init__(identifier) + super().__init__(identifier=identifier, measurements=measurements) self.__expression = expression if not isinstance(self.__expression, Expression): self.__expression = Expression(self.__expression) self.__duration_expression = duration_expression if not isinstance(self.__duration_expression, Expression): self.__duration_expression = Expression(self.__duration_expression) - self.__parameter_names = set(self.__duration_expression.variables() - + self.__expression.variables()) - set(['t']) + self.__parameter_names = set(self.__duration_expression.variables + + self.__expression.variables) - set(['t']) self.__channel = channel self.__measurement_windows = dict() @property - def parameter_names(self) -> Set[str]: + def function_parameters(self) -> Set[str]: return self.__parameter_names + @property + def parameter_names(self) -> Set[str]: + return self.function_parameters | self.measurement_parameters + @property def parameter_declarations(self) -> Set[ParameterDeclaration]: return {ParameterDeclaration(param_name) for param_name in self.parameter_names} @@ -84,12 +89,16 @@ def is_interruptable(self) -> bool: def defined_channels(self) -> Set['ChannelID']: return {self.__channel} + @property + def duration(self) -> Expression: + return self.__duration_expression + def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> 'FunctionWaveform': - substitutions = dict((v, parameters[v].get_value()) for v in self.__expression.variables() if v != 't') - duration_parameters = dict((v, parameters[v].get_value()) for v in self.__duration_expression.variables()) + substitutions = dict((v, parameters[v].get_value()) for v in self.__expression.variables if v != 't') + duration_parameters = dict((v, parameters[v].get_value()) for v in self.__duration_expression.variables) return FunctionWaveform(expression=self.__expression.evaluate_symbolic(substitutions=substitutions), duration=self.__duration_expression.evaluate_numeric(**duration_parameters), measurement_windows=self.get_measurement_windows(parameters=parameters, @@ -119,61 +128,13 @@ def deserialize(serializer: 'Serializer', channel: 'ChannelID', measurement_declarations: Dict[str, List], identifier: Optional[bool]=None) -> 'FunctionPulseTemplate': - template = FunctionPulseTemplate( + return FunctionPulseTemplate( serializer.deserialize(expression), serializer.deserialize(duration_expression), channel=channel, - identifier=identifier + identifier=identifier, + measurements=measurement_declarations ) - for name, windows in measurement_declarations.items(): - for window in windows: - template.add_measurement_declaration(name, *window) - return template - - def get_measurement_windows(self, - parameters: Dict[str, Parameter], - measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: - def get_val(v): - return v if not isinstance(v, Expression) else v.evaluate_numeric( - **{name_: parameters[name_].get_value() if isinstance(parameters[name_], Parameter) else parameters[name_] - for name_ in v.variables()}) - - resulting_windows = [] - for name, windows in self.__measurement_windows.items(): - for begin, end in windows: - resulting_windows.append((measurement_mapping[name], get_val(begin), get_val(end))) - return resulting_windows - - @property - def measurement_declarations(self): - """ - :return: Measurement declarations as added by the add_measurement_declaration method - """ - - return {name: [(begin.get_most_simple_representation(), - end.get_most_simple_representation()) - for begin, end in windows] - for name, windows in self.__measurement_windows.items() } - - @property - def measurement_names(self) -> Set[str]: - """ - :return: - """ - return set(self.__measurement_windows.keys()) - - def add_measurement_declaration(self, name: str, begin: Union[float, str], end: Union[float, str]) -> None: - begin = Expression(begin) - end = Expression(end) - new_parameters = set(itertools.chain(begin.variables(), end.variables())) - - if 't' in new_parameters: - raise ValueError('t is not an allowed measurement window parameter in function templates') - self.__parameter_names |= new_parameters - if name in self.__measurement_windows: - self.__measurement_windows[name].append((begin, end)) - else: - self.__measurement_windows[name] = [(begin, end)] class FunctionWaveform(Waveform): @@ -193,7 +154,7 @@ def __init__(self, expression: Expression, measurement_windows (List): A list of measurement windows """ super().__init__() - if set(expression.variables()) - set('t'): + if set(expression.variables) - set('t'): raise ValueError('FunctionWaveforms may not depend on anything but "t"') self._expression = expression diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index bcbbe553c..9de3562dc 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -89,7 +89,7 @@ def to_range(self, parameters: Dict[str, Any]) -> range: @property def parameter_names(self) -> Set[str]: - return set(self.start.variables()) | set(self.stop.variables()) | set(self.step.variables()) + return set(self.start.variables) | set(self.stop.variables) | set(self.step.variables) class ForLoopPulseTemplate(LoopPulseTemplate): diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 830bf0250..4cea2d686 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -74,7 +74,7 @@ def __init__(self, sub_waveforms: Iterable[Waveform]) -> None: def flatten_sub_waveforms(to_flatten): for sub_waveform in to_flatten: if isinstance(sub_waveform, MultiChannelWaveform): - yield from sub_waveform.__sub_waveforms + yield from sub_waveform._sub_waveforms else: yield sub_waveform @@ -82,26 +82,26 @@ def flatten_sub_waveforms(to_flatten): def get_sub_waveform_sort_key(waveform): return sorted(tuple(waveform.defined_channels)) - self.__sub_waveforms = sorted(flatten_sub_waveforms(sub_waveforms), - key=get_sub_waveform_sort_key) + self._sub_waveforms = sorted(flatten_sub_waveforms(sub_waveforms), + key=get_sub_waveform_sort_key) - if not all(waveform.duration == self.__sub_waveforms[0].duration for waveform in self.__sub_waveforms[1:]): + if not all(waveform.duration == self._sub_waveforms[0].duration for waveform in self._sub_waveforms[1:]): raise ValueError( "MultiChannelWaveform cannot be constructed from channel waveforms of different" "lengths." ) self.__defined_channels = set() - for waveform in self.__sub_waveforms: + for waveform in self._sub_waveforms: if waveform.defined_channels & self.__defined_channels: raise ValueError('Channel may not be defined in multiple waveforms') self.__defined_channels |= waveform.defined_channels @property def duration(self) -> float: - return self.__sub_waveforms[0].duration + return self._sub_waveforms[0].duration def __getitem__(self, key: ChannelID) -> Waveform: - for waveform in self.__sub_waveforms: + for waveform in self._sub_waveforms: if key in waveform.defined_channels: return waveform raise KeyError('Unknown channel ID: {}'.format(key), key) @@ -113,7 +113,7 @@ def defined_channels(self) -> Set[ChannelID]: @property def compare_key(self) -> Any: # sort with channels - return tuple(sub_waveform.compare_key for sub_waveform in self.__sub_waveforms) + return tuple(sub_waveform.compare_key for sub_waveform in self._sub_waveforms) def unsafe_sample(self, channel: ChannelID, @@ -123,10 +123,10 @@ def unsafe_sample(self, def get_measurement_windows(self) -> Iterable[MeasurementWindow]: return itertools.chain.from_iterable(sub_waveform.get_measurement_windows() - for sub_waveform in self.__sub_waveforms) + for sub_waveform in self._sub_waveforms) def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': - relevant_sub_waveforms = tuple(swf for swf in self.__sub_waveforms if swf.defined_channels & channels) + relevant_sub_waveforms = tuple(swf for swf in self._sub_waveforms if swf.defined_channels & channels) if len(relevant_sub_waveforms) == 1: return relevant_sub_waveforms[0].get_subset_for_channels(channels) elif len(relevant_sub_waveforms) > 1: diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index 7c51eb815..dc2151bee 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -143,7 +143,7 @@ def __collect_dependencies(self) -> Iterable[Parameter]: # filter only real dependencies from the dependencies dictionary try: return {dependency_name: self.dependencies[dependency_name] - for dependency_name in self.__expression.variables()} + for dependency_name in self.__expression.variables} except KeyError as key_error: raise ParameterNotProvidedException(str(key_error)) from key_error diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index edd246851..77b360e68 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -7,7 +7,7 @@ directly translated into a waveform. """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict, List, Tuple, Set, Optional, Union, NamedTuple +from typing import Dict, List, Tuple, Set, Optional, Union, Any import itertools from numbers import Real @@ -88,10 +88,10 @@ def __init__(self, identifier: Optional[str]=None): @abstractmethod def build_waveform(self, - parameters: Dict[str, Parameter], + parameters: Dict[str, Real], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: - """Translate this PulseTemplate into a waveform according to the given parameteres. + """Translate this PulseTemplate into a waveform according to the given parameters. Args: parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects. @@ -105,6 +105,8 @@ def atomic_build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: + parameters = dict((parameter_name, parameter_value.get_value()) + for parameter_name, parameter_value in parameters.items()) waveform = self.build_waveform(parameters, measurement_mapping=measurement_mapping, channel_mapping=channel_mapping) @@ -122,13 +124,13 @@ def __init__(self, identifier: Optional[str]=None, measurements: Optional[List[MeasurementDeclaration]]=None): super().__init__(identifier=identifier) - measurements = [] if measurements is None else measurements - self._measurement_windows = [(name, - begin if isinstance(begin, Expression) else Expression(begin), - length if isinstance(length, Expression) else Expression(length)) - for name, begin, length in measurements] + self._measurement_windows = [] if measurements is None else [ + (name, + begin if isinstance(begin, Expression) else Expression(begin), + length if isinstance(length, Expression) else Expression(length)) + for name, begin, length in measurements] for _, _, length in self._measurement_windows: - if length.compare_key < 0 == True: + if (length < 0) is True: raise ValueError('Measurement window length may not be negative') def get_measurement_windows(self, @@ -149,14 +151,20 @@ def get_val(v): return resulting_windows @property - def measurement_declarations(self): + def measurement_parameters(self) -> Set[str]: + return set(var + for _, begin, length in self._measurement_windows + for var in itertools.chain(begin.variables, length.variables)) + + @property + def measurement_declarations(self) -> List[MeasurementDeclaration]: """ :return: Measurement declarations as added by the add_measurement_declaration method """ return [(name, - begin.get_most_simple_representation(), - end.get_most_simple_representation()) - for name, begin, end in self._measurement_windows] + begin.original_expression, + length.original_expression) + for name, begin, length in self._measurement_windows] @property def measurement_names(self) -> Set[str]: diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index b48f97d03..a4bbe5912 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -47,7 +47,7 @@ def __init__(self, template: PulseTemplate, self.__template = template self.__parameter_mapping = parameter_mapping - self.__external_parameters = set(itertools.chain(*(expr.variables() for expr in self.__parameter_mapping.values()))) + self.__external_parameters = set(itertools.chain(*(expr.variables for expr in self.__parameter_mapping.values()))) self.__measurement_mapping = dict(((name,name) for name in missing_name_mappings), **measurement_mapping) self.__channel_mapping = dict(((name,name) for name in missing_channel_mappings), **channel_mapping) @@ -119,7 +119,7 @@ def map_parameters(self, inner_parameters = { parameter: MappedParameter( mapping_function, - {name: parameters[name] for name in mapping_function.variables()} + {name: parameters[name] for name in mapping_function.variables} ) for (parameter, mapping_function) in self.__parameter_mapping.items() } diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 7464d1ffd..5d9a51126 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -50,20 +50,20 @@ def __init__(self, entries of the form (time as float, voltage as float, interpolation strategy). """ super().__init__() - self.__table = tuple(waveform_table) - self.__channel_id = channel - self.__measurement_windows = tuple(measurement_windows) + self._table = tuple(waveform_table) + self._channel_id = channel + self._measurement_windows = tuple(measurement_windows) if len(waveform_table) < 2: raise ValueError("A given waveform table has less than two entries.") @property def compare_key(self) -> Any: - return self.__channel_id, self.__table, self.__measurement_windows + return self._channel_id, self._table, self._measurement_windows @property def duration(self) -> float: - return self.__table[-1].t + return self._table[-1].t def unsafe_sample(self, channel: ChannelID, @@ -72,7 +72,7 @@ def unsafe_sample(self, if output_array is None: output_array = np.empty(len(sample_times)) - for entry1, entry2 in zip(self.__table[:-1], self.__table[1:]): + for entry1, entry2 in zip(self._table[:-1], self._table[1:]): indices = slice(np.searchsorted(sample_times, entry1.t, 'left'), np.searchsorted(sample_times, entry2.t, 'right')) output_array[indices] = \ @@ -81,10 +81,10 @@ def unsafe_sample(self, @property def defined_channels(self) -> Set[ChannelID]: - return {self.__channel_id} + return {self._channel_id} def get_measurement_windows(self) -> Iterable[MeasurementWindow]: - return self.__measurement_windows + return self._measurement_windows def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': return self @@ -116,9 +116,9 @@ def interp(self) -> InterpolationStrategy: class TablePulseTemplate(AtomicPulseTemplate): - interpolation_strategies = {'linear': LinearInterpolationStrategy, - 'hold': HoldInterpolationStrategy, - 'jump': JumpInterpolationStrategy} + interpolation_strategies = {'linear': LinearInterpolationStrategy(), + 'hold': HoldInterpolationStrategy(), + 'jump': JumpInterpolationStrategy()} def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], identifier: Optional[str]=None, @@ -190,6 +190,9 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ (float, float)-list of all table entries with concrete values provided by the given parameters. """ + if not (self.table_parameters <= set(parameters.keys())): + raise ParameterNotProvidedException((self.table_parameters - set(parameters.keys())).pop()) + instantiated_entries = dict() # type: Dict[ChannelID,List[WaveformTableEntry]] for channel, channel_entries in self._entries.items(): @@ -201,7 +204,7 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ # Add (0, v) entry if wf starts at finite time if instantiated[0].t > 0: instantiated.insert(0, WaveformTableEntry(0, - instantiated[0], + instantiated[0].v, TablePulseTemplate.interpolation_strategies['hold'])) for (previous_time, _, _), (time, _, _) in zip(instantiated, instantiated[1:]): @@ -245,13 +248,21 @@ def _remove_redundant_entries(entries: List[WaveformTableEntry]) -> List[Wavefor return entries @property - def parameter_names(self) -> Set[str]: + def table_parameters(self) -> Set[str]: return set( var for channel_entries in self.entries.values() for entry in channel_entries - for var in itertools.chain(entry.t.variables(), entry.v.variables()) - ) | set(var for constraint in self._parameter_constraints for var in constraint.affected_parameters) + for var in itertools.chain(entry.t.variables, entry.v.variables) + ) | set( + var + for constraint in self._parameter_constraints + for var in constraint.affected_parameters + ) + + @property + def parameter_names(self) -> Set[str]: + return self.table_parameters | self.measurement_parameters @property def is_interruptable(self) -> bool: @@ -291,7 +302,8 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: str(entry.interp)) for entry in channel_entries]) for channel, channel_entries in self._entries.items() ), - parameter_constraints=[str(constraint) for constraint in self._parameter_constraints] + parameter_constraints=[str(constraint) for constraint in self._parameter_constraints], + measurements=self.measurement_declarations ) return data @@ -299,10 +311,12 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: def deserialize(serializer: Serializer, entries: Dict[ChannelID, List[EntryInInit]], parameter_constraints: List[str], + measurements: List[MeasurementDeclaration], identifier: Optional[str]=None) -> 'TablePulseTemplate': return TablePulseTemplate(entries=entries, identifier=identifier, parameter_constraints=parameter_constraints, + measurements=measurements, consistency_check=False) def build_waveform(self, @@ -311,24 +325,56 @@ def build_waveform(self, channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: for constraint in self._parameter_constraints: if not constraint.is_fulfilled(parameters): - affected_parameters = {name: parameters[name] for name in constraint.affected_parameters} - raise ParameterConstraintViolation(constraint, 'parameters: {}'.format(affected_parameters)) + raise ParameterConstraintViolation(constraint, 'parameters: {}'.format( + {name: parameters[name] for name in constraint.affected_parameters} + )) instantiated = [(channel_mapping[channel], instantiated_channel) for channel, instantiated_channel in self.get_entries_instantiated(parameters).items()] - measurement_windows = self.get_measurement_windows(parameters=parameters, - measurement_mapping=measurement_mapping) - if not measurement_windows and self.duration.evaluate_numeric(**parameters) == 0: + if self.duration.evaluate_numeric(**parameters) == 0: return None + measurements = self.get_measurement_windows(parameters=parameters, measurement_mapping=measurement_mapping) if len(instantiated) == 1: - return TableWaveform(*instantiated.pop(), measurement_windows) + return TableWaveform(*instantiated.pop(), measurements) else: return MultiChannelWaveform( - [TableWaveform(*instantiated.pop(), measurement_windows)] + [TableWaveform(*instantiated.pop(), measurements)] + [TableWaveform(channel, instantiated_channel, [])for channel, instantiated_channel in instantiated]) + @staticmethod + def from_array(times: np.ndarray, voltages: np.ndarray, channels: List[ChannelID]) -> 'TablePulseTemplate': + """Static constructor to build a TablePulse from numpy arrays. + + Args: + times: 1D numpy array with time values + voltages: 1D or 2D numpy array with voltage values + channels: channels to define + + Returns: + TablePulseTemplate with the given values, hold interpolation everywhere and no free + parameters. + """ + if times.ndim == 0 or voltages.ndim == 0: + raise ValueError('Zero dimensional input is not accepted.') + + if times.ndim > 2 or voltages.ndim > 2: + raise ValueError('Three or higher dimensional input is not accepted.') + + if times.ndim == 2 and times.shape[0] != len(channels): + raise ValueError('First dimension of times must be equal to the number of channels') + + if voltages.ndim == 2 and voltages.shape[0] != len(channels): + raise ValueError('First dimension of voltages must be equal to the number of channels') + + if voltages.shape[-1] != times.shape[-1]: + ValueError('Different number of entries for times and voltages') + + return TablePulseTemplate(dict((channel, list(zip(times if times.ndim == 1 else times[i, :], + voltages if voltages.ndim == 1 else voltages[i, :]))) + for i, channel in enumerate(channels))) + class ZeroDurationTablePulseTemplate(UserWarning): pass diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index e917dc1ed..be3ea4fcb 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -22,9 +22,8 @@ def setUp(self) -> None: self.meas_list = [('mw', 1, 1), ('mw', 'x', 'z'), ('drup', 'j', 'u')] self.meas_dict = {'mw': [(1, 1), ('x', 'z')], 'drup': [('j', 'u')]} - self.fpt = FunctionPulseTemplate(self.s, self.s2,channel='A') - for mw in self.meas_list: - self.fpt.add_measurement_declaration(*mw) + self.fpt = FunctionPulseTemplate(self.s, self.s2, channel='A', + measurements=self.meas_list) self.pars = dict(a=DummyParameter(1), b=DummyParameter(2), c=DummyParameter(136.78)) @@ -51,26 +50,26 @@ def test_serialization_data(self) -> None: expected_data = dict(duration_expression=str(self.s2), expression=str(self.s), channel='A', - measurement_declarations=self.meas_dict) + measurement_declarations=self.meas_list) self.assertEqual(expected_data, self.fpt.get_serialization_data( - DummySerializer(serialize_callback=lambda x: str(x)))) + DummySerializer(serialize_callback=lambda x: x.original_expression))) def test_deserialize(self) -> None: basic_data = dict(duration_expression=str(self.s2), - expression=str(self.s), + expression=self.s, channel='A', identifier='hugo', - measurement_declarations=self.meas_dict) - serializer = DummySerializer(serialize_callback=lambda x: str(x)) - serializer.subelements[str(self.s2)] = Expression(self.s2) - serializer.subelements[str(self.s)] = Expression(self.s) + measurement_declarations=self.meas_list) + serializer = DummySerializer(serialize_callback=lambda x: x.original_expression) + serializer.subelements[self.s2] = Expression(self.s2) + serializer.subelements[self.s] = Expression(self.s) template = FunctionPulseTemplate.deserialize(serializer, **basic_data) self.assertEqual('hugo', template.identifier) self.assertEqual({'a', 'b', 'c', 'x', 'z', 'j', 'u'}, template.parameter_names) self.assertEqual({ParameterDeclaration(name) for name in template.parameter_names}, template.parameter_declarations) self.assertEqual(template.measurement_declarations, - self.meas_dict) + self.meas_list) serialized_data = template.get_serialization_data(serializer) del basic_data['identifier'] self.assertEqual(basic_data, serialized_data) @@ -170,39 +169,34 @@ def assert_declaration_dict_equal(self, d1, d2): self.assert_window_equal(w1, w2) def test_measurement_windows(self) -> None: - pulse = FunctionPulseTemplate(5, 5) + pulse = FunctionPulseTemplate(5, 5, measurements=[('mw', 0, 5)]) - pulse.add_measurement_declaration('mw', 0, 5) windows = pulse.get_measurement_windows(parameters={}, measurement_mapping={'mw': 'asd'}) self.assertEqual([('asd', 0, 5)], windows) - self.assertEqual(pulse.measurement_declarations, dict(mw=[(0, 5)])) + self.assertEqual(pulse.measurement_declarations, [('mw', 0, 5)]) def test_no_measurement_windows(self) -> None: pulse = FunctionPulseTemplate(5, 5) windows = pulse.get_measurement_windows({}, {'mw': 'asd'}) self.assertEqual([], windows) - self.assertEqual(dict(), pulse.measurement_declarations) + self.assertEqual([], pulse.measurement_declarations) def test_measurement_windows_with_parameters(self) -> None: - pulse = FunctionPulseTemplate(5, 'length') + pulse = FunctionPulseTemplate(5, 'length', measurements=[('mw', 1, '(1+length)/2')]) - pulse.add_measurement_declaration('mw',1,'(1+length)/2') parameters = dict(length=100) windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) self.assertEqual(windows, [('asd', 1, 101/2)]) declared = pulse.measurement_declarations - expected = dict(mw=[(1, '(1+length)/2')]) - - self.assert_declaration_dict_equal(declared, expected) + self.assertEqual(declared, [('mw', 1, '(1+length)/2')]) def test_multiple_measurement_windows(self) -> None: - pulse = FunctionPulseTemplate(5, 'length') - - pulse.add_measurement_declaration('A', 0, '(1+length)/2') - pulse.add_measurement_declaration('A', 1, 3) - pulse.add_measurement_declaration('B', 'begin', 2) + pulse = FunctionPulseTemplate(5, 'length', + measurements=[('A', 0, '(1+length)/2'), + ('A', 1, 3), + ('B', 'begin', 2)]) parameters = dict(length=5, begin=1) measurement_mapping = dict(A='A', B='C') @@ -210,7 +204,7 @@ def test_multiple_measurement_windows(self) -> None: measurement_mapping=measurement_mapping) expected = [('A', 0, 3), ('A', 1, 3), ('C', 1, 2)] self.assertEqual(sorted(windows), sorted(expected)) - - self.assert_declaration_dict_equal(pulse.measurement_declarations, - dict(A=[(0, '(1+length)/2'), (1, 3)], - B=[('begin', 2)])) \ No newline at end of file + self.assertEqual(pulse.measurement_declarations, + [('A', 0, '(1+length)/2'), + ('A', 1, 3), + ('B', 'begin', 2)]) \ No newline at end of file diff --git a/tests/pulses/plotting_tests.py b/tests/pulses/plotting_tests.py index d5fc42ce9..955b149cd 100644 --- a/tests/pulses/plotting_tests.py +++ b/tests/pulses/plotting_tests.py @@ -7,7 +7,7 @@ from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate from qctoolkit.pulses.sequencing import Sequencer -from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstruction +from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstruction, DummyPulseTemplate class PlotterTests(unittest.TestCase): @@ -107,7 +107,7 @@ def integrated_test_with_sequencer_and_pulse_templates(self) -> None: class PlottingNotPossibleExceptionTests(unittest.TestCase): def test(self) -> None: - t = TablePulseTemplate() + t = DummyPulseTemplate() exception = PlottingNotPossibleException(t) self.assertIs(t, exception.pulse) self.assertIsInstance(str(exception), str) \ No newline at end of file diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index df690e422..11d46646d 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -4,7 +4,8 @@ from typing import Optional, Dict, Set, Any, List from qctoolkit import MeasurementWindow, ChannelID -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate +from qctoolkit.expressions import Expression +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.instructions import Waveform, EXECInstruction from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform @@ -17,19 +18,16 @@ class AtomicPulseTemplateStub(AtomicPulseTemplate): def is_interruptable(self) -> bool: return super().is_interruptable() - def __init__(self, waveform: Waveform, measurement_windows: List[MeasurementWindow] = [], + def __init__(self, *, waveform: Waveform=None, duration: Expression=None, + measurements: List[MeasurementDeclaration] = [], identifier: Optional[str]=None) -> None: - super().__init__(identifier=identifier) + super().__init__(identifier=identifier, measurements=measurements) self.waveform = waveform - self.measurement_windows = measurement_windows + self._duration = duration def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping, channel_mapping): return self.waveform - - def get_measurement_windows(self, parameters: Dict[str, Parameter] = None): - return self.measurement_windows - def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: @@ -39,10 +37,6 @@ def requires_stop(self, def defined_channels(self) -> Set['ChannelID']: raise NotImplementedError() - @property - def measurement_names(self): - raise NotImplementedError() - @property def parameter_declarations(self) -> Set[ParameterDeclaration]: raise NotImplementedError() @@ -58,21 +52,25 @@ def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: def deserialize(serializer: 'Serializer', **kwargs) -> 'AtomicPulseTemplateStub': raise NotImplementedError() + @property + def duration(self) -> Expression: + return self._duration + class AtomicPulseTemplateTests(unittest.TestCase): def test_is_interruptable(self) -> None: wf = DummyWaveform() - template = AtomicPulseTemplateStub(wf) + template = AtomicPulseTemplateStub(waveform=wf) self.assertFalse(template.is_interruptable()) - template = AtomicPulseTemplateStub(wf, identifier="arbg4") + template = AtomicPulseTemplateStub(waveform=wf, identifier="arbg4") self.assertFalse(template.is_interruptable()) def test_build_sequence_no_waveform(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() - template = AtomicPulseTemplateStub(None) + template = AtomicPulseTemplateStub() template.build_sequence(sequencer, {}, {}, {}, {}, block) self.assertFalse(block.instructions) @@ -84,9 +82,70 @@ def test_build_sequence(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() - template = AtomicPulseTemplateStub(wf, measurement_windows) + template = AtomicPulseTemplateStub(waveform=wf, measurements=measurement_windows) template.build_sequence(sequencer, {}, {}, measurement_mapping={}, channel_mapping={}, instruction_block=block) self.assertEqual(len(block.instructions), 1) self.assertIsInstance(block.instructions[0], EXECInstruction) self.assertEqual(block.instructions[0].waveform.defined_channels, {'A'}) - self.assertEqual(list(block.instructions[0].waveform.get_measurement_windows()), [('M', 0, 5)]) \ No newline at end of file + self.assertEqual(list(block.instructions[0].waveform.get_measurement_windows()), [('M', 0, 5)]) + + def test_measurement_windows(self) -> None: + pulse = AtomicPulseTemplateStub(duration=Expression(5), + measurements=[('mw', 0, 5)]) + with self.assertRaises(KeyError): + pulse.get_measurement_windows(parameters=dict(), measurement_mapping=dict()) + windows = pulse.get_measurement_windows(parameters=dict(), measurement_mapping={'mw': 'asd'}) + self.assertEqual([('asd', 0, 5)], windows) + self.assertEqual(pulse.measurement_declarations, [('mw', 0, 5)]) + + def test_no_measurement_windows(self) -> None: + pulse = AtomicPulseTemplateStub(duration=Expression(4)) + windows = pulse.get_measurement_windows(dict(), {'mw': 'asd'}) + self.assertEqual([], windows) + self.assertEqual([], pulse.measurement_declarations) + + def test_measurement_windows_with_parameters(self) -> None: + pulse = AtomicPulseTemplateStub(duration=Expression('length'), + measurements=[('mw', 1, '(1+length)/2')]) + parameters = dict(length=100) + windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) + self.assertEqual(windows, [('asd', 1, 101 / 2)]) + self.assertEqual(pulse.measurement_declarations, [('mw', 1, '(1+length)/2')]) + + @unittest.skip('Move to AtomicPulseTemplate test') + def test_multiple_measurement_windows(self) -> None: + pulse = AtomicPulseTemplateStub(duration=Expression('length'), + measurements=[('A', 0, '(1+length)/2'), + ('A', 1, 3), + ('B', 'begin', 2)]) + + parameters = dict(length=5, begin=1) + measurement_mapping = dict(A='A', B='C') + windows = pulse.get_measurement_windows(parameters=parameters, + measurement_mapping=measurement_mapping) + expected = [('A', 0, 3), ('A', 1, 3), ('C', 1, 2)] + self.assertEqual(sorted(windows), sorted(expected)) + + expected = [('A', 0, '(1+length)/2'), + ('A', 1, 3), + ('B', 'begin', 2)] + self.assertEqual(pulse.measurement_declarations, + expected) + + def test_measurement_windows_multi_out_of_pulse(self) -> None: + pulse = AtomicPulseTemplateStub(duration=Expression('length'), + measurements=[('mw', 'a', 'd')]) + measurement_mapping = {'mw': 'mw'} + + with self.assertRaises(ValueError): + pulse.get_measurement_windows(measurement_mapping=measurement_mapping, + parameters=dict(length=10, a=-1, d=3)) + with self.assertRaises(ValueError): + pulse.get_measurement_windows(measurement_mapping=measurement_mapping, + parameters=dict(length=10, a=5, d=30)) + with self.assertRaises(ValueError): + pulse.get_measurement_windows(measurement_mapping=measurement_mapping, + parameters=dict(length=10, a=11, d=3)) + with self.assertRaises(ValueError): + pulse.get_measurement_windows(measurement_mapping=measurement_mapping, + parameters=dict(length=10, a=3, d=-1)) diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index d38e4b77a..e544e74dd 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -68,12 +68,11 @@ def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) # Setup test data - self.square = TablePulseTemplate() - self.square.add_entry('up', 'v', 'hold') - self.square.add_entry('down', 0, 'hold') - self.square.add_entry('length', 0) - self.square.add_measurement_declaration('mw1', 'up', 'down+length') - + self.square = TablePulseTemplate({'default': [(0, 0), + ('up', 'v', 'hold'), + ('down', 0, 'hold'), + ('length', 0)]}, + measurements=[('mw1', 'up', 'length-up')]) self.mapping1 = { 'up': 'uptime', 'down': 'uptime + length', @@ -85,7 +84,7 @@ def __init__(self, *args, **kwargs) -> None: self.outer_parameters = {'uptime', 'length', 'pulse_length', 'voltage'} - self.parameters = {} + self.parameters = dict() self.parameters['uptime'] = ConstantParameter(5) self.parameters['length'] = ConstantParameter(10) self.parameters['pulse_length'] = ConstantParameter(100) @@ -137,16 +136,15 @@ class SequencePulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: self.serializer = DummySerializer() - self.table_foo = TablePulseTemplate(identifier='foo') - self.table_foo.add_entry('hugo', 2) - self.table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') - self.table_foo.add_measurement_declaration('mw_foo','hugo','albert') + self.table_foo = TablePulseTemplate({'default': [('hugo', 2), + ('albert', 'voltage')]}, + parameter_constraints=['albert<9.1'], + measurements=[('mw_foo','hugo','albert')], + identifier='foo') self.foo_param_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') self.foo_meas_mappings = dict(mw_foo='mw_bar') - self.table = TablePulseTemplate() - def test_get_serialization_data(self) -> None: dummy1 = DummyPulseTemplate() dummy2 = DummyPulseTemplate() @@ -221,10 +219,9 @@ def test_missing_parameter_declaration_exception(self): SequencePulseTemplate(subtemplates, self.outer_parameters) def test_crash(self) -> None: - table = TablePulseTemplate(identifier='foo') - table.add_entry('ta', 'va', interpolation='hold') - table.add_entry('tb', 'vb', interpolation='linear') - table.add_entry('tend', 0, interpolation='jump') + table = TablePulseTemplate({'default': [('ta', 'va', 'hold'), + ('tb', 'vb', 'linear'), + ('tend', 0, 'jump')]}, identifier='foo') external_parameters = ['ta', 'tb', 'tc', 'td', 'va', 'vb', 'tend'] first_mapping = { diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index aa82fa86f..fbabeaf97 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -34,58 +34,6 @@ def test_unknown_interpolation_strategy(self): class TablePulseTemplateTest(unittest.TestCase): - @unittest.skip('Move to AtomicPulseTemplate test') - def test_measurement_windows(self) -> None: - pulse = TablePulseTemplate() - pulse.add_entry(1, 1) - pulse.add_entry(3, 0) - pulse.add_entry(5, 0) - pulse.add_measurement_declaration('mw', 0, 5) - windows = pulse.get_measurement_windows(parameters={}, measurement_mapping={'mw': 'asd'}) - self.assertEqual([('asd', 0, 5)], windows) - self.assertEqual(pulse.measurement_declarations, dict(mw=[(0, 5)])) - - @unittest.skip('Move to AtomicPulseTemplate test') - def test_no_measurement_windows(self) -> None: - pulse = TablePulseTemplate() - pulse.add_entry(1, 1) - pulse.add_entry(3, 0) - pulse.add_entry(5, 0) - windows = pulse.get_measurement_windows({}, {'mw': 'asd'}) - self.assertEqual([], windows) - self.assertEqual(dict(), pulse.measurement_declarations) - - @unittest.skip('Move to AtomicPulseTemplate test') - def test_measurement_windows_with_parameters(self) -> None: - pulse = TablePulseTemplate() - pulse.add_entry(1, 1) - pulse.add_entry('length', 0) - pulse.add_measurement_declaration('mw',1,'(1+length)/2') - parameters = dict(length=100) - windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) - self.assertEqual(windows, [('asd', 1, 101/2)]) - self.assertEqual(pulse.measurement_declarations, dict(mw=[(1, '(1+length)/2')])) - - @unittest.skip('Move to AtomicPulseTemplate test') - def test_multiple_measurement_windows(self) -> None: - pulse = TablePulseTemplate() - pulse.add_entry(1, 1) - pulse.add_entry('length', 0) - - pulse.add_measurement_declaration('A', 0, '(1+length)/2') - pulse.add_measurement_declaration('A', 1, 3) - pulse.add_measurement_declaration('B', 'begin', 2) - - parameters = dict(length=5, begin=1) - measurement_mapping = dict(A='A', B='C') - windows = pulse.get_measurement_windows(parameters=parameters, - measurement_mapping=measurement_mapping) - expected = [('A', 0, 3), ('A', 1, 3), ('C', 1, 2)] - self.assertEqual(sorted(windows), sorted(expected)) - self.assertEqual(pulse.measurement_declarations, - dict(A=[(0, '(1+length)/2'), (1, 3)], - B=[('begin', 2)])) - def test_time_is_negative(self) -> None: with self.assertRaises(ValueError): TablePulseTemplate({0: [(1, 2), @@ -149,8 +97,6 @@ def test_time_not_increasing_hard(self): ('b', 1), ('c*a', 'k')]}, parameter_constraints=['a*c < b']) - - def test_time_is_0_on_construction(self) -> None: with self.assertWarns(ZeroDurationTablePulseTemplate): warnings.simplefilter('default', ZeroDurationTablePulseTemplate) @@ -177,15 +123,7 @@ def test_single_channel_no_parameters(self): expected = [TableEntry(*entry) for entry in raw_entries] self.assertEqual(table.entries, dict([(0, expected)])) self.assertEqual(table.duration, 2.2) - self.assertEqual(table.parameter_names, {}) - - def test_single_channel_no_parameters(self): - raw_entries = [(0., 1.1), (1.1, 2.), (2.2, 2.4)] - table = TablePulseTemplate({0: raw_entries}) - expected = [TableEntry(*entry) for entry in raw_entries] - self.assertEqual(table.entries, dict([(0, expected)])) - self.assertEqual(table.duration, 2.2) - self.assertEqual(table.parameter_names, {}) + self.assertEqual(table.parameter_names, set()) def test_internal_constraints(self): table = TablePulseTemplate({0: [(1, 'v'), (2, 'w')], @@ -217,10 +155,10 @@ def test_external_constraints(self): with self.assertRaises(ParameterConstraintViolation): table.build_waveform(parameters=dict(v=1., w=2, t=0.1, x=2.2, y=1, h=1), - measurement_mapping=dict(), + measurement_mapping={0: 0, 1: 1}, channel_mapping={0: 0, 1: 1}) table.build_waveform(parameters=dict(v=1., w=2, t=0.1, x=1.2, y=1, h=2), - measurement_mapping=dict(), + measurement_mapping={0: 0, 1: 1}, channel_mapping={0: 0, 1: 1}) def test_is_interruptable(self) -> None: @@ -228,7 +166,7 @@ def test_is_interruptable(self) -> None: def test_get_entries_instantiated_one_entry_float_float(self) -> None: table = TablePulseTemplate({0: [(0, 2)]}) - instantiated_entries = table.get_entries_instantiated({})[0] + instantiated_entries = table.get_entries_instantiated(dict())[0] self.assertEqual([(0, 2, HoldInterpolationStrategy())], instantiated_entries) def test_get_entries_instantiated_one_entry_float_declaration(self) -> None: @@ -249,206 +187,172 @@ def test_get_entries_instantiated_two_entries_float_declaraton_declaration_decla self.assertEqual([(0, -5, HoldInterpolationStrategy()), (3, 5, HoldInterpolationStrategy())], instantiated_entries) - def test_get_entries_instantiated_two_entries_invalid_parameters(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 'v1') - t_decl = ParameterDeclaration('t', min=1, max=2) - v2_decl = ParameterDeclaration('v2', min=10, max=30) - table.add_entry(t_decl, v2_decl) - with self.assertRaises(ParameterValueIllegalException): - table.get_entries_instantiated({'v1': -5, 't': 0, 'v2': 20}) - with self.assertRaises(ParameterValueIllegalException): - table.get_entries_instantiated({'v1': -5, 't': 1, 'v2': -20}) - - def test_get_entries_instantiated_two_entries_parameter_missing(self) -> None: - table = TablePulseTemplate() - t_decl = ParameterDeclaration('t', min=1, max=2) - v2_decl = ParameterDeclaration('v2', min=10, max=30) - table.add_entry(t_decl, v2_decl) + def test_get_entries_instantiated_multiple_parameters_missing(self) -> None: + table = TablePulseTemplate({0: [(0, 'v1'), + ('t', 'v2')]}) with self.assertRaises(ParameterNotProvidedException): table.get_entries_instantiated(dict()) - - def test_get_entries_instantiated_linked_time_declarations(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo', min=1) - bar_decl = ParameterDeclaration('bar') - table.add_entry(foo_decl, 'v', 'linear') - table.add_entry(bar_decl, 0, 'jump') - instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4})['default'] - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy())], instantiated_entries) - with self.assertRaises(Exception): - table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 1}) - with self.assertRaises(Exception): - table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 0.2}) - - def test_get_entries_instantiated_unlinked_time_declarations(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo', min=1, max=2) - bar_decl = ParameterDeclaration('bar', min=1.5, max=4) - table.add_entry(foo_decl, 'v', 'linear') - table.add_entry(bar_decl, 0, 'jump') - instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4})['default'] - self.assertEqual([(0, 0, HoldInterpolationStrategy()), (1, 2.3, LinearInterpolationStrategy()), (4, 0, JumpInterpolationStrategy())], instantiated_entries) - with self.assertRaises(Exception): - table.get_entries_instantiated({'v': 2.3, 'foo': 2, 'bar': 1.5}) - - def test_get_entries_instantiated_empty(self) -> None: - table = TablePulseTemplate() - self.assertEquals([(0, 0, HoldInterpolationStrategy())], table.get_entries_instantiated({})['default']) + with self.assertRaises(ParameterNotProvidedException): + table.get_entries_instantiated(dict(v1=1)) + with self.assertRaises(ParameterNotProvidedException): + table.get_entries_instantiated(dict(v1=1, t=2)) + table.get_entries_instantiated(dict(v1=1, t=2, v2=2)) + + def test_get_entries_auto_insert(self) -> None: + table = TablePulseTemplate({0: [('foo', 'v', 'linear'), + ('bar', 0, 'jump')], + 1: [(0, 3, 'linear'), + ('bar+foo', 2, 'linear')]}) + instantiated_entries = table.get_entries_instantiated({'v': 2.3, 'foo': 1, 'bar': 4}) + self.assertEqual({0: [(0, 2.3, HoldInterpolationStrategy()), + (1, 2.3, LinearInterpolationStrategy()), + (4, 0, JumpInterpolationStrategy()), + (5, 0, HoldInterpolationStrategy())], + 1: [(0, 3, LinearInterpolationStrategy()), + (5, 2, LinearInterpolationStrategy())]}, instantiated_entries) + + def test_empty_instantiated(self) -> None: + with self.assertRaises(TypeError): + TablePulseTemplate() + with self.assertRaises(ValueError): + TablePulseTemplate(entries=dict()) def test_get_entries_instantiated_two_equal_entries(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 0) - table.add_entry(1, 5) - table.add_entry(3, 5) - table.add_entry(5, 1) - entries = table.get_entries_instantiated({})['default'] + table = TablePulseTemplate({0: [(0, 0), + (1, 5), + (3, 5), + (5, 1)]}) + entries = table.get_entries_instantiated(dict()) expected = [ TableEntry(0, 0, HoldInterpolationStrategy()), TableEntry(1, 5, HoldInterpolationStrategy()), TableEntry(3, 5, HoldInterpolationStrategy()), TableEntry(5, 1, HoldInterpolationStrategy()) ] - self.assertEqual(expected, entries) + self.assertEqual({0: expected}, entries) def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries(self) -> None: - table = TablePulseTemplate() - table.add_entry(1, 5) - table.add_entry(1.5, 5) - table.add_entry(2, 5) - table.add_entry(3, 0) - entries = table.get_entries_instantiated({})['default'] + table = TablePulseTemplate({0: [(1, 5), + (1.5, 5), + (2, 5), + (3, 0)]}) + entries = table.get_entries_instantiated(dict()) expected = [ - TableEntry(0, 0, HoldInterpolationStrategy()), - TableEntry(1, 5, HoldInterpolationStrategy()), + TableEntry(0, 5, HoldInterpolationStrategy()), TableEntry(2, 5, HoldInterpolationStrategy()), TableEntry(3, 0, HoldInterpolationStrategy()) ] - self.assertEqual(expected, entries) + self.assertEqual({0: expected}, entries) def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries_does_not_destroy_linear_interpolation(self) -> None: - table = TablePulseTemplate() - table.add_entry(0, 5) - table.add_entry(2, 5, 'linear') - table.add_entry(5, 5) - table.add_entry(10, 0, 'linear') - - entries = table.get_entries_instantiated({})['default'] + table = TablePulseTemplate({0: [(0, 5), + (2, 5, 'linear'), + (5, 5), + (10, 0, 'linear')]}) + entries = table.get_entries_instantiated(dict()) expected = [ TableEntry(0, 5, HoldInterpolationStrategy()), TableEntry(5, 5, HoldInterpolationStrategy()), TableEntry(10, 0, LinearInterpolationStrategy()) ] - self.assertEqual(expected, entries) + self.assertEqual({0: expected}, entries) - result_sampled = TableWaveform(channel='A', waveform_table=entries, measurement_windows=[]).get_sampled( - channel='A', - sample_times=numpy.linspace(0, 10, 11)) + def test_from_array_exceptions(self): + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.arange(0), numpy.arange(1), [0]) - numbers = [5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0] - expected = [float(x) for x in numbers] - self.assertEqual(expected, result_sampled.tolist()) + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.arange(1), numpy.arange(0), [0]) - def test_get_entries_instantiated_two_channels_one_empty(self) -> None: - table = TablePulseTemplate(channels=['A','B']) - table.add_entry('foo', 4, channel='A') - parameters = {'foo': 10} + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.array(numpy.ndindex((1, 2, 1))), numpy.arange(2), [0]) - entries = table.get_entries_instantiated(parameters) + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.array(numpy.ndindex((3, 4))), + numpy.array(numpy.ndindex((3, 5))), [3, 4, 5]) - expected = { - 'A': [ - TableEntry(0, 0, HoldInterpolationStrategy()), - TableEntry(10, 4, HoldInterpolationStrategy()), - ], - 'B': [ - TableEntry(0, 0, HoldInterpolationStrategy()), - TableEntry(10, 0, HoldInterpolationStrategy()) - ] - } + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.array(numpy.ndindex((3, 5))), + numpy.array(numpy.ndindex((3, 4))), [3, 4, 5]) - self.assertEqual(expected, entries) + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.array(numpy.ndindex((2, 4))), + numpy.array(numpy.ndindex((3, 4))), [3, 4, 5]) + + with self.assertRaises(ValueError): + TablePulseTemplate.from_array(numpy.array(numpy.ndindex((3, 4))), + numpy.array(numpy.ndindex((2, 4))), [3, 4, 5]) def test_from_array_1D(self) -> None: times = numpy.array([0, 1, 3]) voltages = numpy.array([5, 0, 5]) - pulse = TablePulseTemplate.from_array(times, voltages) + pulse = TablePulseTemplate.from_array(times, voltages, [0]) entries = [] for (time, voltage) in zip(times, voltages): entries.append(TableEntry(time, voltage, HoldInterpolationStrategy())) + self.assertEqual({0: entries}, pulse.entries) + + def test_from_array_multi_one_time(self) -> None: + times = numpy.array([0, 1, 3]) + voltages = numpy.array([[1, 2, 3], + [2, 3, 4]]) + pulse = TablePulseTemplate.from_array(times, voltages, [0, 1]) + entries = { + i: [TableEntry(time, voltage, HoldInterpolationStrategy()) + for (time, voltage) in zip(times, voltages[i, :])] + for i in range(2)} + self.assertEqual(entries, pulse.entries) def test_from_array_multi(self) -> None: - times = numpy.array([0, 1, 3]) - voltages = numpy.array([[1,2,3], - [2,3,4]]).T # todo: why transposed?? + times = numpy.array([[0, 1, 3], [2, 3, 4]]) + voltages = numpy.array([[1, 2, 3], + [2, 3, 4]]) pulse = TablePulseTemplate.from_array(times, voltages, [0, 1]) entries = { i: [TableEntry(time, voltage, HoldInterpolationStrategy()) - for (time, voltage) in zip(times, channel_voltage)] - for i, channel_voltage in enumerate(voltages.T)} + for (time, voltage) in zip(times[i, :], voltages[i, :])] + for i in range(2)} self.assertEqual(entries, pulse.entries) - def test_add_entry_multi_invalid_channel(self) -> None: - pulse = TablePulseTemplate() - with self.assertRaises(ValueError): - pulse.add_entry(2,2, channel=1) - - def test_add_entry_multi(self) -> None: - pulse = TablePulseTemplate(channels=[0, 1]) - pulse.add_entry(1,1, channel=0) - pulse.add_entry(1,1, channel=1) - entries = {0: [(0,0,HoldInterpolationStrategy()), - (1,1,HoldInterpolationStrategy())], - 1: [(0,0,HoldInterpolationStrategy()), - (1,1,HoldInterpolationStrategy())]} + def test_from_array_multi_one_voltage(self) -> None: + times = numpy.array([[0, 1, 3], [2, 3, 4]]) + voltages = numpy.array([1, 2, 3]) + pulse = TablePulseTemplate.from_array(times, voltages, [0, 1]) + entries = { + i: [TableEntry(time, voltage, HoldInterpolationStrategy()) + for (time, voltage) in zip(times[i, :], voltages)] + for i in range(2)} + self.assertEqual(entries, pulse.entries) def test_add_entry_multi_same_time_param(self) -> None: - pulse = TablePulseTemplate(channels=[0, 1]) - pulse.add_entry(1, 3, channel=0) - pulse.add_entry('foo', 'bar', channel=0) - pulse.add_entry(7, 3, channel=0) - - pulse.add_entry(0, -5, channel=1) - pulse.add_entry(0.5, -2, channel=1) - pulse.add_entry('foo', 0, channel=1) - pulse.add_entry(5, 'bar', channel=1) - - expected_foo = ParameterDeclaration('foo', min=1, max=5) - expected_bar = ParameterDeclaration('bar') - entries = {0: [TableEntry(0, 0, HoldInterpolationStrategy()), - TableEntry(1, 3, HoldInterpolationStrategy()), - TableEntry(expected_foo, expected_bar, HoldInterpolationStrategy()), - TableEntry(7, 3, HoldInterpolationStrategy())], - 1: [TableEntry(0, -5, HoldInterpolationStrategy()), - TableEntry(0.5, -2, HoldInterpolationStrategy()), - TableEntry(expected_foo, 0, HoldInterpolationStrategy()), - TableEntry(5, expected_bar, HoldInterpolationStrategy())]} - self.assertEqual(entries, pulse.entries) + pulse = TablePulseTemplate({0: [(1, 3), + ('foo', 'bar'), + (7, 3)], + 1: [(0, -5), + (0.5, -2), + ('foo', 0), + (5, 'bar')]}) self.assertEqual({'foo', 'bar'}, pulse.parameter_names) - self.assertEqual({expected_bar, expected_foo}, pulse.parameter_declarations) def test_get_instantiated_entries_multi_same_time_param(self) -> None: - table = TablePulseTemplate(channels=[0, 1]) - table.add_entry(1, 3, channel=0) - table.add_entry('foo', 'bar', channel=0) - table.add_entry(7, 3, channel=0) - - table.add_entry(0, -5, channel=1) - table.add_entry(0.5, -2, channel=1) - table.add_entry('foo', 0, channel=1) - table.add_entry(5, 'bar', channel=1) - + table = TablePulseTemplate({0: [(1, 3), + ('foo', 'bar'), + (7, 3)], + 1: [(0, -5), + (0.5, -2), + ('foo', 0), + (5, 'bar')]}) parameters = {'foo': 2.7, 'bar': -3.3} entries = table.get_entries_instantiated(parameters) expected = { 0: [ - TableEntry(0, 0, HoldInterpolationStrategy()), + TableEntry(0, 3, HoldInterpolationStrategy()), TableEntry(1, 3, HoldInterpolationStrategy()), TableEntry(2.7, -3.3, HoldInterpolationStrategy()), TableEntry(7, 3, HoldInterpolationStrategy()), @@ -461,173 +365,141 @@ def test_get_instantiated_entries_multi_same_time_param(self) -> None: TableEntry(7, -3.3, HoldInterpolationStrategy()) ] } - self.assertEqual(expected, entries) - def test_get_instaniated_entries_multi_one_empty_channel(self) -> None: - table = TablePulseTemplate(channels=[0, 1]) - table.add_entry(1, 3, channel=1) - table.add_entry('foo', 'bar', 'linear', channel=1) - - parameters = {'foo': 5.2, 'bar': -83.8} - - entries = table.get_entries_instantiated(parameters) - - expected = { - 0: [ - TableEntry(0, 0, HoldInterpolationStrategy()), - TableEntry(5.2, 0, HoldInterpolationStrategy()) - ], - 1: [ - TableEntry(0, 0, HoldInterpolationStrategy()), - TableEntry(1, 3, HoldInterpolationStrategy()), - TableEntry(5.2, -83.8, LinearInterpolationStrategy()) - ] - } - - self.assertEqual(expected, entries) - def test_measurement_windows_multi(self) -> None: - pulse = TablePulseTemplate(channels=[0, 1]) - pulse.add_entry(1, 1, channel=0) - pulse.add_entry(3, 0, channel=0) - pulse.add_entry(5, 0, channel=0) - - pulse.add_entry(1, 1, channel=1) - pulse.add_entry(3, 0, channel=1) - pulse.add_entry(10, 0, channel=1) - - pulse.add_measurement_declaration('mw',1,7) - windows = pulse.get_measurement_windows({}, measurement_mapping={'mw': 'asd'}) - self.assertEqual([('asd',1,7)], windows) - - def test_measurement_windows_multi_out_of_pulse(self) -> None: - pulse = TablePulseTemplate(channels=[0, 1]) - pulse.add_entry(1, 1, channel=0) - pulse.add_entry(3, 0, channel=0) - pulse.add_entry(5, 0, channel=0) - - pulse.add_entry(1, 1, channel=1) - pulse.add_entry(3, 0, channel=1) - pulse.add_entry(10, 0, channel=1) - - with self.assertRaises(ValueError): - pulse.add_measurement_declaration('mw', 1, 't_meas') - pulse.get_measurement_windows({'t_meas': 20}, measurement_mapping={'mw': 'asd'}) - - -@unittest.skip class TablePulseTemplateSerializationTests(unittest.TestCase): def setUp(self) -> None: self.serializer = DummySerializer(lambda x: dict(name=x.name), lambda x: x.name, lambda x: x['name']) - self.template = TablePulseTemplate(identifier='foo', channels=['A', 'B']) + self.entries = dict(A=[('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')], + B=[(0, 5, 'hold'), (1, 7, 'jump'), ('k', 't', 'hold')]) + self.measurements = [('m', 1, 1), ('foo', 'z', 'o')] + self.template = TablePulseTemplate(entries=self.entries, + measurements=self.measurements, + identifier='foo', parameter_constraints=['ilse>2', 'k>foo']) self.expected_data = dict(type=self.serializer.get_type_identifier(self.template)) self.maxDiff = None def test_get_serialization_data(self) -> None: - self.template.add_entry('foo', 2, channel='A') - self.template.add_entry('hugo', 'ilse', interpolation='linear',channel='A') - - self.template.add_entry(2, 2, channel='B', interpolation='jump') - - self.template.add_measurement_declaration('mw',2,'hugo+franz') - - self.expected_data['measurement_declarations'] = {'mw': [(2,'hugo+franz')]} - self.expected_data['time_parameter_declarations'] = [dict(name=name) for name in sorted(['foo','hugo','franz'])] - self.expected_data['voltage_parameter_declarations'] = [dict(name='ilse')] - self.expected_data['entries'] = dict(A=[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')], B=[(0, 0, 'hold'), (2, 2, 'jump')]) + expected_data = dict(measurements=self.measurements, + entries=self.entries, + parameter_constraints=[str(Expression('ilse>2')), str(Expression('k>foo'))]) data = self.template.get_serialization_data(self.serializer) - self.assertEqual(self.expected_data, data) + self.assertEqual(expected_data, data) def test_deserialize(self) -> None: - data = dict(measurement_declarations={'mw': [(2,'hugo+franz')]}, - time_parameter_declarations=[dict(name='hugo'), dict(name='foo'), dict(name='franz')], - voltage_parameter_declarations=[dict(name='ilse')], - entries=dict(default=[(0, 0, 'hold'), ('foo', 2, 'hold'), ('hugo', 'ilse', 'linear')]), + data = dict(measurements=self.measurements, + entries=self.entries, + parameter_constraints=['ilse>2', 'k>foo'], identifier='foo') - # prepare dependencies for deserialization - self.serializer.subelements['foo'] = ParameterDeclaration('foo') - self.serializer.subelements['hugo'] = ParameterDeclaration('hugo') - self.serializer.subelements['ilse'] = ParameterDeclaration('ilse') - self.serializer.subelements['franz'] = ParameterDeclaration('franz') - # deserialize template = TablePulseTemplate.deserialize(self.serializer, **data) - # prepare expected parameter declarations - self.serializer.subelements['foo'].min_value = 0 - self.serializer.subelements['foo'].max_value = self.serializer.subelements['hugo'] - all_declarations = set(self.serializer.subelements.values()) + self.assertEqual(template.entries, self.template.entries) + self.assertEqual(template.measurement_declarations, self.template.measurement_declarations) + - # prepare expected entries - entries = [(0, 0, HoldInterpolationStrategy()), - (self.serializer.subelements['foo'], 2, HoldInterpolationStrategy()), - (self.serializer.subelements['hugo'], self.serializer.subelements['ilse'], LinearInterpolationStrategy())] +class TablePulseTemplateSequencingTests(unittest.TestCase): + def test_build_waveform_single_channel(self): + table = TablePulseTemplate({0: [(0, 0), + ('foo', 'v', 'linear'), + ('bar', 0, 'jump')]}, + parameter_constraints=['foo>1'], + measurements=[('M', 'b', 'l'), + ('N', 1, 2)]) + + parameters = {'v': 2.3, 'foo': 1, 'bar': 4, 'b': 2, 'l': 1} + measurement_mapping = {'M': 'P', 'N': 'N'} + channel_mapping = {0: 'ch'} - # compare! - self.assertEqual(all_declarations, template.parameter_declarations) - self.assertEqual({'foo', 'hugo', 'ilse', 'franz'}, template.parameter_names) - self.assertEqual(entries, template.entries) - self.assertEqual('foo', template.identifier) + with self.assertRaises(ParameterConstraintViolation): + table.build_waveform(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) + parameters['foo'] = 1.1 + waveform = table.build_waveform(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) -@unittest.skip -class TablePulseTemplateSequencingTests(unittest.TestCase): + self.assertIsInstance(waveform, TableWaveform) + self.assertEqual(waveform._table, + ((0, 0, HoldInterpolationStrategy()), + (1.1, 2.3, LinearInterpolationStrategy()), + (4, 0, JumpInterpolationStrategy()))) + self.assertEqual(sorted(waveform._measurement_windows), + sorted((('P', 2, 1), + ('N', 1, 2)))) + self.assertEqual(waveform._channel_id, + 'ch') + + def test_build_waveform_multi_channel(self): + table = TablePulseTemplate({0: [(0, 0), + ('foo', 'v', 'linear'), + ('bar', 0, 'jump')], + 3: [(0, 1), + ('bar+foo', 0, 'linear')]}, + parameter_constraints=['foo>1'], + measurements=[('M', 'b', 'l'), + ('N', 1, 2)]) + + parameters = {'v': 2.3, 'foo': 1, 'bar': 4, 'b': 2, 'l': 1} + measurement_mapping = {'M': 'P', 'N': 'N'} + channel_mapping = {0: 'ch', 3: 'oh'} - def test_build_sequence(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo', min=1) - bar_decl = ParameterDeclaration('bar') - table.add_entry(foo_decl, 'v', 'linear') - table.add_entry(bar_decl, 0, 'jump') - parameters = {'v': 2.3, 'foo': 1, 'bar': 4} - instantiated_entries = table.get_entries_instantiated(parameters) - channel_mapping = {'default': 'default'} - waveform = table.build_waveform(parameters, - measurement_mapping={}, + with self.assertRaises(ParameterConstraintViolation): + table.build_waveform(parameters=parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) + + parameters['foo'] = 1.1 + waveform = table.build_waveform(parameters=parameters, + measurement_mapping=measurement_mapping, channel_mapping=channel_mapping) - sequencer = DummySequencer() - instruction_block = DummyInstructionBlock() - - table.build_sequence(sequencer, parameters, {}, {}, channel_mapping, instruction_block) - if len(instantiated_entries) == 1: - expected_waveform = TableWaveform(*instantiated_entries.popitem(), measurement_windows=[]) - else: - expected_waveform = MultiChannelWaveform([TableWaveform(channel=channel, - waveform_table=inst, - measurement_windows=[]) - for channel, inst in instantiated_entries.items()]) - self.assertEqual(1, len(instruction_block.instructions)) - instruction = instruction_block.instructions[0] - self.assertIsInstance(instruction, EXECInstruction) - self.assertEqual(expected_waveform, instruction.waveform) - self.assertEqual(expected_waveform, waveform) - - @unittest.skip("What exactly is the point of allowing empty/non-existent waveforms?") - def test_build_sequence_empty(self) -> None: - table = TablePulseTemplate() - sequencer = DummySequencer() - instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, {}, {}, {}, instruction_block) - self.assertFalse(instruction_block.instructions) - self.assertIsNone(table.build_waveform({})) + + self.assertIsInstance(waveform, MultiChannelWaveform) + self.assertEqual(len(waveform._sub_waveforms), 2) + + channels = {'oh', 'ch'} + found_measurements = False + for wf in waveform._sub_waveforms: + self.assertIsInstance(wf, TableWaveform) + self.assertIn(wf._channel_id, channels) + channels.remove(wf._channel_id) + if wf.defined_channels == {'ch'}: + self.assertEqual(wf._table, + ((0, 0, HoldInterpolationStrategy()), + (1.1, 2.3, LinearInterpolationStrategy()), + (4, 0, JumpInterpolationStrategy()), + (5.1, 0, HoldInterpolationStrategy()))) + elif wf.defined_channels == {'oh'}: + self.assertEqual(wf._table, + ((0, 1, HoldInterpolationStrategy()), + (5.1, 0, LinearInterpolationStrategy()))) + + if wf._measurement_windows: + self.assertFalse(found_measurements, 'Measurements found twice') + self.assertEqual(sorted(wf._measurement_windows), + sorted((('P', 2, 1), + ('N', 1, 2)))) + found_measurements = True + self.assertTrue(found_measurements, 'Measurements not found') + + def test_build_waveform_empty(self) -> None: + table = TablePulseTemplate(dict(a=[('t', 0)])) + self.assertIsNone(table.build_waveform(dict(t=0), dict(), dict(a='a'))) def test_requires_stop_missing_param(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo') - table.add_entry(foo_decl, 'v', 'linear') + table = TablePulseTemplate({0: [('foo', 'v')]}) with self.assertRaises(ParameterNotProvidedException): table.requires_stop({'foo': DummyParameter(0, False)}, {}) def test_requires_stop(self) -> None: - table = TablePulseTemplate() - foo_decl = ParameterDeclaration('foo', min=1) - bar_decl = ParameterDeclaration('bar') - table.add_entry(foo_decl, 'v', 'linear') - table.add_entry(bar_decl, 0, 'jump') + table = TablePulseTemplate({0: [('foo', 'v'), + ('bar', 0)]}) test_sets = [(False, {'foo': DummyParameter(0, False), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(False)}), (False, {'foo': DummyParameter(0, False), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(True)}), (True, {'foo': DummyParameter(0, True), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(False)}), @@ -641,68 +513,11 @@ def test_requires_stop(self) -> None: def test_identifier(self) -> None: identifier = 'some name' - pulse = TablePulseTemplate(identifier=identifier) + pulse = TablePulseTemplate(entries={0: [(1, 0)]}, identifier=identifier) self.assertEqual(pulse.identifier, identifier) - def test_build_sequence_multi(self) -> None: - table = TablePulseTemplate(channels=['A', 'B']) - table.add_entry(1, 3, channel='A') - table.add_entry('foo', 'bar', channel='A') - table.add_entry(7, 3, channel='A') - - table.add_entry(0, -5, channel='B') - table.add_entry('foo', 0, channel='B') - table.add_entry(5, 'bar', channel='B') - - parameters = {'foo': 3, 'bar': 17} - channel_mapping = {'A': 'CHA', 'B': 'CHB'} - - instantiated_entries = table.get_entries_instantiated(parameters) - expected_waveform = MultiChannelWaveform( - [TableWaveform(channel=('CH'+channel), waveform_table=instantiated, measurement_windows=[]) - for channel, instantiated in - instantiated_entries.items()]) - - sequencer = DummySequencer() - instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, {}, {}, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - - self.assertEqual(1, len(instruction_block.instructions)) - instruction = instruction_block.instructions[0] - self.assertIsInstance(instruction, EXECInstruction) - self.assertEqual(expected_waveform, instruction.waveform) - waveform = table.build_waveform(parameters, measurement_mapping={}, channel_mapping=channel_mapping) - for ch in waveform.defined_channels: - self.assertEqual(expected_waveform, waveform) - - def test_build_sequence_multi_one_channel_empty(self) -> None: - table = TablePulseTemplate(channels={'A', 'B'}) - table.add_entry('foo', 4, channel='A') - parameters = {'foo': 3} - channel_mapping = {'A': 'CHA', 'B': 'CHB'} - - instantiated_entries = table.get_entries_instantiated(parameters) - - sequencer = DummySequencer() - instruction_block = DummyInstructionBlock() - table.build_sequence(sequencer, parameters, - conditions={}, - measurement_mapping={}, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - expected_waveform = MultiChannelWaveform([TableWaveform('CH'+channel, instantiated, []) for channel, instantiated in instantiated_entries.items()]) - self.assertEqual(1, len(instruction_block.instructions)) - instruction = instruction_block.instructions[0] - self.assertIsInstance(instruction, EXECInstruction) - self.assertEqual(expected_waveform, instruction.waveform) - waveform = table.build_waveform(parameters, measurement_mapping={}, channel_mapping=channel_mapping) - - self.assertEqual(expected_waveform, waveform) - - -@unittest.skip + + class TableWaveformDataTests(unittest.TestCase): def test_duration(self) -> None: @@ -762,7 +577,6 @@ def test_simple_properties(self): self.assertIs(waveform.unsafe_get_subset_for_channels('A'), waveform) -@unittest.skip class ParameterValueIllegalExceptionTest(unittest.TestCase): def test(self) -> None: diff --git a/tests/pulses/table_sequence_sequencer_intergration_tests.py b/tests/pulses/table_sequence_sequencer_intergration_tests.py index b2a0c5acc..8805849f2 100644 --- a/tests/pulses/table_sequence_sequencer_intergration_tests.py +++ b/tests/pulses/table_sequence_sequencer_intergration_tests.py @@ -13,16 +13,14 @@ class TableSequenceSequencerIntegrationTests(unittest.TestCase): def test_table_sequence_sequencer_integration(self) -> None: - t1 = TablePulseTemplate() - t1.add_entry(2, 'foo') - t1.add_entry(5, 0) - t1.add_measurement_declaration('foo', 2, 5) + t1 = TablePulseTemplate(entries={'default': [(2, 'foo'), + (5, 0)]}, + measurements=[('foo', 2, 2)]) - t2 = TablePulseTemplate() - t2.add_entry(4, 0) - t2.add_entry(4.5, 'bar', 'linear') - t2.add_entry(5, 0) - t2.add_measurement_declaration('foo', 4, 5) + t2 = TablePulseTemplate(entries={'default': [(4, 0), + (4.5, 'bar', 'linear'), + (5, 0)]}, + measurements=[('foo', 4, 1)]) seqt = SequencePulseTemplate([MappingTemplate(t1, {'foo': 'foo'}, measurement_mapping={'foo': 'bar'}), MappingTemplate(t2, {'bar': '2 * hugo'})], {'foo', 'hugo'}) @@ -75,5 +73,5 @@ def test_table_sequence_sequencer_integration(self) -> None: self.assertEqual(3, len(instructions)) for instruction in instructions: - if isinstance(instruction,EXECInstruction): - self.assertIn(instruction.waveform.get_measurement_windows()[0], [('my', 2, 5), ('thy', 4, 5)]) + if isinstance(instruction, EXECInstruction): + self.assertIn(instruction.waveform.get_measurement_windows()[0], [('my', 2, 2), ('thy', 4, 1)]) diff --git a/tests/serialization_tests.py b/tests/serialization_tests.py index ab5ac1c33..16c29828c 100644 --- a/tests/serialization_tests.py +++ b/tests/serialization_tests.py @@ -324,13 +324,14 @@ def test_deserialize_identifier(self) -> None: self.assertEqual(self.deserialization_data['data'], deserialized.data) def test_serialization_and_deserialization_combined(self) -> None: - table_foo = TablePulseTemplate(identifier='foo') - table_foo.add_entry('hugo', 2) - table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') - table = TablePulseTemplate() + table_foo = TablePulseTemplate(identifier='foo', entries={'default': [('hugo', 2), + ('albert', 'voltage')]}, + parameter_constraints=['albert<9.1']) + table = TablePulseTemplate({'default': [('t', 0)]}) + foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') - sequence = SequencePulseTemplate([(table_foo, foo_mappings, {}), - (table, {}, {})], + sequence = SequencePulseTemplate([(table_foo, foo_mappings, dict()), + (table, dict(t=0), dict())], ['ilse', 'albert', 'voltage'], identifier=None) From 91ed64a92340e32758c8f510ae82d1188183febd Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 19 Apr 2017 13:13:25 +0200 Subject: [PATCH 056/116] Extend dummy device capabilities Fix TablePT uses --- qctoolkit/hardware/awgs/tabor.py | 8 ++- tests/hardware/alazar_tests.py | 1 + tests/hardware/awg_tests.py | 9 ++-- tests/hardware/dummy_modules.py | 83 +++++++++++++++++++++++--------- tests/hardware/tabor_tests.py | 35 ++++++++------ tests/hardware/util_tests.py | 2 +- 6 files changed, 93 insertions(+), 45 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 3a676f6b2..f9edb84f7 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -379,14 +379,20 @@ def select_marker(self, marker) -> None: self.send_cmd(':SOUR:MARK:SEL {marker}'.format(marker=marker)) def sample_rate(self, channel) -> int: + if channel not in (1, 2, 3, 4): + raise TaborException('Invalid channel: {}'.format(channel)) return int(float(self.send_query(':INST:SEL {channel}; :FREQ:RAST?'.format(channel=channel)))) def amplitude(self, channel) -> float: + if channel not in (1, 2, 3, 4): + raise TaborException('Invalid channel: {}'.format(channel)) coupling = self.send_query(':INST:SEL {channel}; :OUTP:COUP?'.format(channel=channel)) if coupling == 'DC': return float(self.send_query(':VOLT?')) elif coupling == 'HV': - return float(self.send_query(':VOLD:HV?')) + return float(self.send_query(':VOLT:HV?')) + else: + raise TaborException('Unknown coupling: {}'.format(coupling)) def offset(self, channel) -> float: return float(self.send_query(':INST:SEL {channel}; :VOLT:OFFS?'.format(channel=channel))) diff --git a/tests/hardware/alazar_tests.py b/tests/hardware/alazar_tests.py index 3e5188574..d23aeca1a 100644 --- a/tests/hardware/alazar_tests.py +++ b/tests/hardware/alazar_tests.py @@ -1,6 +1,7 @@ import unittest from . import dummy_modules +dummy_modules.import_package('atsaverage') import atsaverage import atsaverage.config diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index f1ce158ec..45c94cd2b 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -9,10 +9,10 @@ class DummyAWGTest(unittest.TestCase): def setUp(self): - self.pulse_template = pls.TablePulseTemplate() - self.pulse_template.add_entry('value', 5) + self.pulse_template = pls.TablePulseTemplate({'default': [('value', 5)]}) + self.sequencer = pls.Sequencer() - for i in range(1,12): + for i in range(1, 12): pars = dict(value=i) self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) self.program = self.sequencer.build() @@ -27,8 +27,7 @@ def test_ProgramOverwriteException(self): class TektronixAWGTest(unittest.TestCase): def setUp(self): - self.pulse_template = pls.TablePulseTemplate() - self.pulse_template.add_entry('value', 5) + self.pulse_template = pls.TablePulseTemplate({'default': [('value', 5)]}) self.sequencer = pls.Sequencer() for i in range(1,12): pars = dict(value=i) diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 1428dd135..20ed17730 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -17,16 +17,42 @@ class MessageBasedResource: def __init__(self, *args, **kwargs): self.logged_writes = [] self.logged_asks = [] + self.answers = dict() + self.default_answer = '0, bla' + def write(self, *args, **kwargs): self.logged_writes.append((args, kwargs)) + def ask(self, *args, **kwargs): self.logged_asks.append((args, kwargs)) - return ';'.join( '0, bla'*args[0].count('?') ) + ques = args[0].split(';') + ques = [q.strip(' ?') for q in ques if q.strip().endswith('?')] + answers = [self.answers[q] if q in self.answers else self.default_answer + for q in ques] + return ';'.join(answers) dummy_pyvisa.resources.MessageBasedResource = dummy_pyvisa.resources.messagebased.MessageBasedResource class dummy_teawg(dummy_package): - model_properties_dict = dict() + model_properties_dict = { + 'model_name': 'Dummy_WX2184', # the model name + 'num_parts': 2, # number of instrument parts + 'chan_per_part': 2, # number of channels per part + 'seg_quantum': 16, # segment-length quantum + 'min_seg_len': 192, # minimal segment length + 'max_arb_mem': 32E6, # maximal arbitrary-memory (points per channel) + 'min_dac_val': 0, # minimal DAC value + 'max_dac_val': 2 ** 14 - 1, # maximal DAC value + 'max_num_segs': 32E+3, # maximal number of segments + 'max_seq_len': 48 * 1024 - 2, # maximal sequencer-table length (# rows) + 'min_seq_len': 3, # minimal sequencer-table length (# rows) + 'max_num_seq': 1000, # maximal number of sequencer-table + 'max_aseq_len': 48 * 1024 - 2, # maximal advanced-sequencer table length + 'min_aseq_len': 3, # minimal advanced-sequencer table length + 'min_sclk': 75e6, # minimal sampling-rate (samples/seconds) + 'max_sclk': 2300e6, # maximal sampling-rate (samples/seconds) + 'digital_support': False, # is digital-wave supported? + } class TEWXAwg: def __init__(self, *args, **kwargs): self.logged_commands = [] @@ -38,8 +64,7 @@ def visa_inst(self): def send_cmd(self, *args, **kwargs): self.logged_commands.append((args, kwargs)) def send_query(self, *args, **kwargs): - self.logged_queries.append((args, kwargs)) - return 0 + return self._visa_inst.ask(*args, **kwargs) send_binary_data = send_cmd download_sequencer_table = send_cmd @@ -69,7 +94,17 @@ class CrossBufferMask: pass -def import_package(name, package) -> Set[dummy_package]: +def import_package(name, package=None) -> Set[dummy_package]: + if package is None: + package_dict = dict(atsaverage=dummy_atsaverage, + pyvisa=dummy_pyvisa, + pytabor=dummy_pytabor, + teawg=dummy_teawg) + if name in package_dict: + package = package_dict[name] + else: + raise KeyError('Unknown package', name) + imported = set() sys.modules[name] = package imported.add(package) @@ -79,25 +114,27 @@ def import_package(name, package) -> Set[dummy_package]: return imported -failed_imports = set() -try: - import pytabor -except ImportError: - failed_imports |= import_package('pytabor', dummy_pytabor) +def replace_missing(): + failed_imports = set() + try: + import pytabor + except ImportError: + failed_imports |= import_package('pytabor', dummy_pytabor) -try: - import pyvisa -except ImportError: - failed_imports |= import_package('pyvisa', dummy_pyvisa) + try: + import pyvisa + except ImportError: + failed_imports |= import_package('pyvisa', dummy_pyvisa) -try: - import teawg -except ImportError: - failed_imports |= import_package('teawg', dummy_teawg) + try: + import teawg + except ImportError: + failed_imports |= import_package('teawg', dummy_teawg) -try: - import atsaverage - import atsaverage.config -except ImportError: - failed_imports |= import_package('atsaverage', dummy_atsaverage) + try: + import atsaverage + import atsaverage.config + except ImportError: + failed_imports |= import_package('atsaverage', dummy_atsaverage) + return failed_imports diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index eadf4694d..83ce4af97 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -4,7 +4,13 @@ from copy import copy, deepcopy import numpy as np -from . import dummy_modules +with_hardware = False +if not with_hardware: + from . import dummy_modules + dummy_modules.import_package('pytabor', dummy_modules.dummy_pytabor) + dummy_modules.import_package('pyvisa', dummy_modules.dummy_pyvisa) + dummy_modules.import_package('atsaverage', dummy_modules.dummy_atsaverage) + dummy_modules.import_package('teawg', dummy_modules.dummy_teawg) from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair from qctoolkit.hardware.program import MultiChannelProgram @@ -23,28 +29,27 @@ def __init__(self, *args, **kwargs): select_channel = dummy_modules.dummy_teawg.TEWXAwg.send_cmd -instrument = None -if pytabor not in dummy_modules.failed_imports: +if with_hardware: # fix on your machine possible_addresses = ('127.0.0.1', ) for instrument_address in possible_addresses: - try: - instrument = TaborAWGRepresentation(instrument_address, - reset=True, - paranoia_level=2) - instrument._visa_inst.timeout = 25000 - break - except: - pass - -if instrument is None: - instrument = DummyTaborAWGRepresentation() + instrument = TaborAWGRepresentation(instrument_address, + reset=True, + paranoia_level=2) + instrument._visa_inst.timeout = 25000 + break +else: + instrument = TaborAWGRepresentation('dummy_address', reset=True, paranoia_level=2) + instrument._visa_inst.answers[':OUTP:COUP'] = 'DC' + instrument._visa_inst.answers[':VOLT'] = '1.0' + instrument._visa_inst.answers[':FREQ:RAST'] = '1e9' + class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.instr_props = next(model_properties_dict.values().__iter__()) + self.instr_props = model_properties_dict @property def waveform_data_generator(self): diff --git a/tests/hardware/util_tests.py b/tests/hardware/util_tests.py index 67de70897..75d4f3ead 100644 --- a/tests/hardware/util_tests.py +++ b/tests/hardware/util_tests.py @@ -30,7 +30,7 @@ def test_voltage_to_uint16(self): self.assertTrue(np.all(expected_data == received_data)) -@unittest.skipIf(pytabor in dummy_modules.failed_imports, "Cannot compare to pytabor results") +@unittest.skipIf(pytabor is dummy_modules.dummy_pytabor, "Cannot compare to pytabor results") class TaborMakeCombinedTest(unittest.TestCase): def exec_general(self, data_1, data_2): tabor_segments = [TaborSegment(d1, d2) for d1, d2 in zip(data_1, data_2)] From e75f6130b4847db75c5663204921533978f4e31d Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 19 Apr 2017 13:48:52 +0200 Subject: [PATCH 057/116] Missing type annotations --- qctoolkit/hardware/awgs/tabor.py | 2 +- qctoolkit/pulses/parameters.py | 6 +++--- qctoolkit/pulses/table_pulse_template.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index f9edb84f7..d8696b328 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -691,7 +691,7 @@ def set_channel_state(self, channel, active) -> None: command_string = ':INST:SEL {}; :OUTP {}'.format(self._channels[channel], 'ON' if active else 'OFF') self._device.send_cmd(command_string) - def arm(self, name: str): + def arm(self, name: str) -> None: if self._current_program == name: self._device.send_cmd('SEQ:SEL 1') else: diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index dc2151bee..9bf606fa3 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -197,17 +197,17 @@ def is_fulfilled(self, parameter: Dict[str, Any]) -> bool: def compare_key(self) -> sympy.Expr: return self._relation - def __str__(self): + def __str__(self) -> str: if isinstance(self._relation, sympy.Eq): return '{}=={}'.format(self._relation.lhs, self._relation.rhs) else: return str(self._relation) - def get_serialization_data(self, serializer: 'Serializer'): + def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, str]: return dict(relation=str(self)) @staticmethod - def deserialize(serializer: 'Serializer', relation: str): + def deserialize(serializer: 'Serializer', relation: str) -> 'ParameterConstraint': return ParameterConstraint(relation) diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 5d9a51126..fd0a426fe 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -162,7 +162,7 @@ def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], if not sympy.reduce_inequalities(inequalities): raise ValueError('Table pulse template has impossible parametrization') - def _add_entry(self, channel, new_entry: TableEntry): + def _add_entry(self, channel, new_entry: TableEntry) -> None: # comparisons with Expression can yield None -> use 'is True' and 'is False' if (new_entry.t < 0) is True: From d34c546836f9a345fe3c63ae4becee928e400f45 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Fri, 19 May 2017 10:52:04 +0200 Subject: [PATCH 058/116] Introduce AtomicMultiChannelPulseTemplate SequencePulseTemplate __init__ overhaul Simpler Expression serialization Replace ParameterDeclaration with ParameterConstraint Drop atomicity and is_interruptable for now --- qctoolkit/comparable.py | 29 ++ qctoolkit/expressions.py | 32 +- qctoolkit/pulses/branch_pulse_template.py | 18 +- qctoolkit/pulses/function_pulse_template.py | 51 ++- qctoolkit/pulses/interpolation.py | 11 +- qctoolkit/pulses/loop_pulse_template.py | 64 +-- qctoolkit/pulses/measurement.py | 52 +++ .../pulses/multi_channel_pulse_template.py | 231 +++++++--- qctoolkit/pulses/parameters.py | 395 +++--------------- qctoolkit/pulses/pulse_template.py | 137 ++---- .../pulse_template_parameter_mapping.py | 188 +++++++-- qctoolkit/pulses/repetition_pulse_template.py | 105 ++--- qctoolkit/pulses/sequence_pulse_template.py | 91 ++-- qctoolkit/pulses/sequencing.py | 4 +- qctoolkit/pulses/table_pulse_template.py | 44 +- qctoolkit/serialization.py | 18 +- 16 files changed, 712 insertions(+), 758 deletions(-) create mode 100644 qctoolkit/pulses/measurement.py diff --git a/qctoolkit/comparable.py b/qctoolkit/comparable.py index 3d3803952..35af40a30 100644 --- a/qctoolkit/comparable.py +++ b/qctoolkit/comparable.py @@ -34,3 +34,32 @@ def __eq__(self, other: Any) -> bool: def __ne__(self, other: Any) -> bool: """True, if other is not equal to this Comparable object.""" return not self == other + + +def extend_comparison(cls): + if not hasattr(cls, '__lt__'): + raise ValueError('Class does not implement __lt__') + + def __eq__(self, other): + return not self < other and not other < self + + def __ne__(self, other): + return self < other or other < self + + def __gt__(self, other): + return other < self + + def __ge__(self, other): + return not self < other + + def __le__(self, other): + return not other < self + operations = {'__eq__': __eq__, + '__ne__': __ne__, + '__gt__': __gt__, + '__ge__': __ge__, + '__le__': __le__} + for operation_name, operation_func in operations.items(): + if not hasattr(cls, operation_name): + setattr(cls, operation_name, operation_func) + return cls diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index 5f6712059..16be58ab5 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -8,7 +8,7 @@ import numpy from qctoolkit.comparable import Comparable -from qctoolkit.serialization import Serializable, Serializer +from qctoolkit.serialization import Serializable, Serializer, ExtendedJSONEncoder __all__ = ["Expression", "ExpressionVariableMissingException"] @@ -16,7 +16,7 @@ class Expression(Serializable, Comparable): """A mathematical expression instantiated from a string representation.""" - def __init__(self, ex: Union[str, Number]) -> None: + def __init__(self, ex: Union[str, Number, sympy.Expr]) -> None: """Create an Expression object. Receives the mathematical expression which shall be represented by the object as a string @@ -27,7 +27,7 @@ def __init__(self, ex: Union[str, Number]) -> None: ex (string): The mathematical expression represented as a string """ super().__init__() - self._original_expression = ex + self._original_expression = str(ex) if isinstance(ex, sympy.Expr) else ex self._sympified_expression = sympy.sympify(ex) self._variables = tuple(str(var) for var in self._sympified_expression.free_symbols) self._expression_lambda = sympy.lambdify(self._variables, @@ -51,25 +51,29 @@ def get_most_simple_representation(self) -> Union[str, int, float, complex]: else: return self._original_expression + @staticmethod + def _sympify(other: Union['Expression', Number, sympy.Expr]) -> sympy.Expr: + return other._sympified_expression if isinstance(other, Expression) else sympy.sympify(other) + def __lt__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: - result = self._sympified_expression < (other._sympified_expression if isinstance(other, Expression) else other) + result = self._sympified_expression < Expression._sympify(other) return None if isinstance(result, sympy.Rel) else bool(result) def __gt__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: - result = self._sympified_expression > (other._sympified_expression if isinstance(other, Expression) else other) + result = self._sympified_expression > Expression._sympify(other) return None if isinstance(result, sympy.Rel) else bool(result) def __ge__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: - result = self._sympified_expression >= (other._sympified_expression if isinstance(other, Expression) else other) + result = self._sympified_expression >= Expression._sympify(other) return None if isinstance(result, sympy.Rel) else bool(result) def __le__(self, other: Union['Expression', Number, sympy.Expr]) -> Union[bool, None]: - result = self._sympified_expression <= (other._sympified_expression if isinstance(other, Expression) else other) + result = self._sympified_expression <= Expression._sympify(other) return None if isinstance(result, sympy.Rel) else bool(result) def __eq__(self, other: Union['Expression', Number, sympy.Expr]) -> bool: """Overwrite Comparable's test for equality to incorporate comparisons with Numbers""" - return self._sympified_expression == (other._sympified_expression if isinstance(other, Expression) else other) + return self._sympified_expression == Expression._sympify(other) @property def compare_key(self) -> sympy.Expr: @@ -79,6 +83,10 @@ def compare_key(self) -> sympy.Expr: def original_expression(self) -> Union[str, Number]: return self._original_expression + @property + def sympified_expression(self) -> sympy.Expr: + return self._sympified_expression + @property def variables(self) -> Iterable[str]: """ Get all free variables in the expression. @@ -113,7 +121,7 @@ def evaluate_numeric(self, **kwargs) -> Union[Number, numpy.ndarray]: return result raise NonNumericEvaluation(self, result, kwargs) - def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> 'Expression': + def evaluate_symbolic(self, substitutions: Dict[Any, Any]) -> 'Expression': """Evaluate the expression symbolically. Args: @@ -121,6 +129,8 @@ def evaluate_symbolic(self, substitutions: Dict[Any, Any]=dict()) -> 'Expression Returns: """ + substitutions = dict((k, v.sympified_expression if isinstance(v, Expression) else v) + for k, v in substitutions.items()) return Expression(self._sympified_expression.subs(substitutions)) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: @@ -134,6 +144,10 @@ def deserialize(serializer: 'Serializer', **kwargs) -> Serializable: def identifier(self) -> Optional[str]: return None + def is_nan(self) -> bool: + return sympy.sympify('nan') == self._sympified_expression +ExtendedJSONEncoder.str_constructable_types.add(Expression) + class ExpressionVariableMissingException(Exception): """An exception indicating that a variable value was not provided during expression evaluation. diff --git a/qctoolkit/pulses/branch_pulse_template.py b/qctoolkit/pulses/branch_pulse_template.py index d82a9c7f8..5c4cea149 100644 --- a/qctoolkit/pulses/branch_pulse_template.py +++ b/qctoolkit/pulses/branch_pulse_template.py @@ -1,9 +1,11 @@ """This module defines BranchPulseTemplate, a higher-order hierarchical pulse template that conditionally executes one out of two possible PulseTemplates.""" -from typing import Dict, Set, List, Optional, Any +from typing import Dict, Set, List, Optional, Any, Union -from qctoolkit.pulses.parameters import Parameter +from qctoolkit.expressions import Expression + +from qctoolkit.pulses.parameters import Parameter, ParameterConstraint from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.conditions import Condition, ConditionMissingException from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock @@ -56,10 +58,6 @@ def __str__(self) -> str: def parameter_names(self) -> Set[str]: return self.__if_branch.parameter_names | self.__else_branch.parameter_names - @property - def parameter_declarations(self) -> Set[str]: - return self.__if_branch.parameter_declarations | self.__else_branch.parameter_declarations - @property def is_interruptable(self) -> bool: return self.__if_branch.is_interruptable and self.__else_branch.is_interruptable @@ -69,12 +67,12 @@ def defined_channels(self) -> Set['ChannelID']: return self.__if_branch.defined_channels @property - def measurement_names(self) -> Set[str]: - return self.__if_branch.measurement_names | self.__else_branch.measurement_names + def duration(self) -> Expression: + return Expression('nan') @property - def atomicity(self) -> bool: - return False + def measurement_names(self) -> Set[str]: + return self.__if_branch.measurement_names | self.__else_branch.measurement_names def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: try: diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 272879335..73a7820db 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -9,7 +9,6 @@ from typing import Any, Dict, List, Set, Optional, Union import numbers -import itertools import numpy as np @@ -17,16 +16,16 @@ from qctoolkit.serialization import Serializer from qctoolkit import MeasurementWindow, ChannelID -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter +from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.instructions import Waveform -from qctoolkit.pulses.pulse_template_parameter_mapping import ParameterNotProvidedException +from qctoolkit.pulses.measurement import MeasurementDefiner __all__ = ["FunctionPulseTemplate", "FunctionWaveform"] -class FunctionPulseTemplate(AtomicPulseTemplate): +class FunctionPulseTemplate(AtomicPulseTemplate, MeasurementDefiner, ParameterConstrainer): """Defines a pulse via a time-domain expression. FunctionPulseTemplate stores the expression and its external parameters. The user must provide @@ -41,9 +40,11 @@ class FunctionPulseTemplate(AtomicPulseTemplate): def __init__(self, expression: Union[str, Expression], duration_expression: Union[str, Expression], + channel: ChannelID = 'default', + identifier: Optional[str] = None, + *, measurements: Optional[List[MeasurementDeclaration]]=None, - identifier: str=None, - channel: 'ChannelID' = 'default') -> None: + parameter_constraints: Optional[List[Union[str, 'ParameterConstraint']]]=None) -> None: """Create a new FunctionPulseTemplate instance. Args: @@ -57,29 +58,31 @@ def __init__(self, window. (optional, default = False) identifier (str): A unique identifier for use in serialization. (optional) """ - super().__init__(identifier=identifier, measurements=measurements) + AtomicPulseTemplate.__init__(self, identifier=identifier) + MeasurementDefiner.__init__(self, measurements=measurements) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) + self.__expression = expression if not isinstance(self.__expression, Expression): self.__expression = Expression(self.__expression) self.__duration_expression = duration_expression if not isinstance(self.__duration_expression, Expression): self.__duration_expression = Expression(self.__duration_expression) - self.__parameter_names = set(self.__duration_expression.variables - + self.__expression.variables) - set(['t']) + self.__parameter_names = {*self.__duration_expression.variables, *self.__expression.variables} - {'t'} self.__channel = channel self.__measurement_windows = dict() + @property + def expression(self) -> Expression: + return self.__expression + @property def function_parameters(self) -> Set[str]: return self.__parameter_names @property def parameter_names(self) -> Set[str]: - return self.function_parameters | self.measurement_parameters - - @property - def parameter_declarations(self) -> Set[ParameterDeclaration]: - return {ParameterDeclaration(param_name) for param_name in self.parameter_names} + return self.function_parameters | self.measurement_parameters | self.constrained_parameters @property def is_interruptable(self) -> bool: @@ -93,12 +96,17 @@ def defined_channels(self) -> Set['ChannelID']: def duration(self) -> Expression: return self.__duration_expression + @property + def measurement_names(self) -> Set[str]: + return {name for name, _, _ in self._measurement_windows} + def build_waveform(self, - parameters: Dict[str, Parameter], + parameters: Dict[str, numbers.Real], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> 'FunctionWaveform': - substitutions = dict((v, parameters[v].get_value()) for v in self.__expression.variables if v != 't') - duration_parameters = dict((v, parameters[v].get_value()) for v in self.__duration_expression.variables) + self.validate_parameter_constraints(parameters=parameters) + substitutions = {v: parameters[v] for v in self.__expression.variables if v != 't'} + duration_parameters = {v: parameters[v] for v in self.__duration_expression.variables} return FunctionWaveform(expression=self.__expression.evaluate_symbolic(substitutions=substitutions), duration=self.__duration_expression.evaluate_numeric(**duration_parameters), measurement_windows=self.get_measurement_windows(parameters=parameters, @@ -118,7 +126,8 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: duration_expression=serializer.dictify(self.__duration_expression), expression=serializer.dictify(self.__expression), channel=self.__channel, - measurement_declarations=self.measurement_declarations + measurement_declarations=self.measurement_declarations, + parameter_constraints=[str(c) for c in self.parameter_constraints] ) @staticmethod @@ -126,14 +135,16 @@ def deserialize(serializer: 'Serializer', expression: str, duration_expression: str, channel: 'ChannelID', - measurement_declarations: Dict[str, List], + measurement_declarations: List[MeasurementDeclaration], + parameter_constraints: List, identifier: Optional[bool]=None) -> 'FunctionPulseTemplate': return FunctionPulseTemplate( serializer.deserialize(expression), serializer.deserialize(duration_expression), channel=channel, identifier=identifier, - measurements=measurement_declarations + measurements=measurement_declarations, + parameter_constraints=parameter_constraints ) diff --git a/qctoolkit/pulses/interpolation.py b/qctoolkit/pulses/interpolation.py index 0c0b8038f..d6b4ad169 100644 --- a/qctoolkit/pulses/interpolation.py +++ b/qctoolkit/pulses/interpolation.py @@ -60,8 +60,7 @@ def __call__(self, end: Tuple[float, float], times: np.ndarray) -> np.ndarray: m = (end[1] - start[1])/(end[0] - start[0]) - interpolator = lambda t: m * (t - start[0]) + start[1] - return interpolator(times) + return m * (times - start[0]) + start[1] def __str__(self) -> str: return 'linear' @@ -84,9 +83,7 @@ def __call__(self, start[0], end[0] ) ) - - voltages = np.ones_like(times) * start[1] - return voltages + return np.full_like(times, fill_value=start[1], dtype=float) def __str__(self) -> str: return 'hold' @@ -109,9 +106,7 @@ def __call__(self, start[0], end[0] ) ) - - voltages = np.ones_like(times) * end[1] - return voltages + return np.full_like(times, fill_value=end[1], dtype=float) def __str__(self) -> str: return 'jump' diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index 9de3562dc..f44a08a18 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -8,7 +8,7 @@ from qctoolkit.expressions import Expression from qctoolkit.pulses.parameters import Parameter, ConstantParameter, InvalidParameterNameException -from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate, ChannelID +from qctoolkit.pulses.pulse_template import PulseTemplate, ChannelID from qctoolkit.pulses.conditions import Condition, ConditionMissingException from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.pulses.sequencing import Sequencer @@ -17,12 +17,11 @@ __all__ = ['WhileLoopPulseTemplate', 'ConditionMissingException'] -class LoopPulseTemplate(PossiblyAtomicPulseTemplate): +class LoopPulseTemplate(PulseTemplate): """Base class for loop based pulse templates""" def __init__(self, body: PulseTemplate, identifier: Optional[str]=None): super().__init__(identifier=identifier) self.__body = body - self.__atomicity = False @property def body(self) -> PulseTemplate: @@ -40,18 +39,6 @@ def measurement_names(self) -> Set[str]: def is_interruptable(self): raise NotImplementedError() - @property - def atomicity(self) -> bool: - if self.__body.atomicity is False: - self.__atomicity = False - return self.__atomicity - - @atomicity.setter - def atomicity(self, val) -> None: - if val and self.__body.atomicity is False: - raise ValueError('Cannot make {} atomic as the body is not'.format(type(self))) - self.__atomicity = val - class ParametrizedRange: """Parametrized range """ @@ -133,6 +120,12 @@ def loop_index(self) -> str: def loop_range(self) -> ParametrizedRange: return self._loop_range + @property + def duration(self) -> Expression: + count = (self._loop_range.stop.sympified_expression - self._loop_range.start.sympified_expression) + step = self._loop_range.step.sympified_expression + return Expression((count - count % step)/step * self.body.duration.sympified_expression) + @property def parameter_names(self) -> Set[str]: parameter_names = self.body.parameter_names @@ -162,22 +155,14 @@ def build_sequence(self, conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], - instruction_block: InstructionBlock) -> None: - - if self.atomicity: - # atomicity can only be enabled if the loop index is not used - self.atomic_build_sequence(parameters=parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - else: - for local_parameters in self._body_parameter_generator(parameters, forward=False): - sequencer.push(self.body, - parameters=local_parameters, - conditions=conditions, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) + instruction_block: InstructionBlock) -> None: + for local_parameters in self._body_parameter_generator(parameters, forward=False): + sequencer.push(self.body, + parameters=local_parameters, + conditions=conditions, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) def build_waveform(self, parameters: Dict[str, Parameter]) -> ForLoopWaveform: return ForLoopWaveform([self.body.build_waveform(local_parameters) @@ -186,13 +171,7 @@ def build_waveform(self, parameters: Dict[str, Parameter]) -> ForLoopWaveform: def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: - if any(parameters[parameter_name].requires_stop() for parameter_name in self._loop_range.parameter_names): - return True - if self.atomicity: - return any(self.body.requires_stop(local_parameters, conditions) - for local_parameters in self._body_parameter_generator(parameters=parameters)) - else: - return False + return any(parameters[parameter_name].requires_stop for parameter_name in self._loop_range.parameter_names) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict( @@ -248,6 +227,10 @@ def condition(self) -> str: def parameter_names(self) -> Set[str]: return self.body.parameter_names + @property + def duration(self) -> Expression: + return Expression('nan') + @property def parameter_declarations(self) -> Set[str]: return self.body.parameter_declarations @@ -289,8 +272,7 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict( type=serializer.get_type_identifier(self), condition=self._condition, - body=serializer.dictify(self.body), - atomicity=self.atomicity + body=serializer.dictify(self.body) ) return data @@ -298,13 +280,11 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: def deserialize(serializer: Serializer, condition: str, body: Dict[str, Any], - atomicity: bool, identifier: Optional[str]=None) -> 'WhileLoopPulseTemplate': body = serializer.deserialize(body) result = WhileLoopPulseTemplate(condition=condition, body=body, identifier=identifier) - result.atomicity = atomicity return result diff --git a/qctoolkit/pulses/measurement.py b/qctoolkit/pulses/measurement.py new file mode 100644 index 000000000..7ee1793ae --- /dev/null +++ b/qctoolkit/pulses/measurement.py @@ -0,0 +1,52 @@ +from typing import Optional, List, Tuple, Union, Dict, Set, Iterable +from numbers import Real +import itertools + +from qctoolkit.expressions import Expression +from qctoolkit.pulses.parameters import ParameterConstraint, ParameterConstraintViolation, Parameter + + +MeasurementDeclaration = Tuple[str, Union[Expression, str, Real], Union[Expression, str, Real]] +MeasurementWindow = Tuple[str, Real, Real] + + +class MeasurementDefiner: + def __init__(self, measurements: Optional[List[MeasurementDeclaration]]=None): + if measurements is None: + self._measurement_windows = [] + else: + self._measurement_windows = [(name, + begin if isinstance(begin, Expression) else Expression(begin), + length if isinstance(length, Expression) else Expression(length)) + for name, begin, length in measurements] + for _, _, length in self._measurement_windows: + if (length < 0) is True: + raise ValueError('Measurement window length may not be negative') + + def get_measurement_windows(self, + parameters: Dict[str, Real], + measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: + """Calculate measurement windows with the given parameter set and rename them woth the measurement mapping""" + def get_val(v): + return v.evaluate_numeric(**parameters) + + resulting_windows = [(measurement_mapping[name], get_val(begin), get_val(length)) + for name, begin, length in self._measurement_windows] + + for _, begin, length in resulting_windows: + if begin < 0 or length < 0: + raise ValueError('Measurement window with negative begin or length: {}, {}'.format(begin, length)) + return resulting_windows + + @property + def measurement_parameters(self) -> Set[str]: + return set(var + for _, begin, length in self._measurement_windows + for var in itertools.chain(begin.variables, length.variables)) + + @property + def measurement_declarations(self) -> List[MeasurementDeclaration]: + return [(name, + begin.original_expression, + length.original_expression) + for name, begin, length in self._measurement_windows] diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 4cea2d686..753ca66f0 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -9,6 +9,7 @@ from typing import Dict, List, Tuple, FrozenSet, Optional, Any, Iterable, Union, Set import itertools +import numbers import numpy @@ -17,17 +18,36 @@ from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.pulses.instructions import InstructionBlock, Waveform, InstructionPointer -from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate +from qctoolkit.pulses.pulse_template import PulseTemplate, AtomicPulseTemplate from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingTemplate,\ - MissingParameterDeclarationException -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ - ParameterNotProvidedException + MissingParameterDeclarationException, MappingTuple +from qctoolkit.pulses.parameters import Parameter, ParameterConstraint, ParameterConstrainer from qctoolkit.pulses.conditions import Condition -from qctoolkit.comparable import Comparable +from qctoolkit.expressions import Expression __all__ = ["MultiChannelWaveform", "MultiChannelPulseTemplate"] +def _parse_subtemplates(subtemplates): + subtemplates = [st if isinstance(st, PulseTemplate) else MappingTemplate.from_tuple(st) for st in subtemplates] + + defined_channels = [st.defined_channels for st in subtemplates] + + # check there are no intersections between channels + for i, channels_i in enumerate(defined_channels): + for j, channels_j in enumerate(defined_channels[i + 1:]): + if channels_i & channels_j: + raise ChannelMappingException(subtemplates[i], + subtemplates[i + 1 + j], + (channels_i | channels_j).pop()) + + return subtemplates + + +def split_mappings(templates, mappings): + pass + + class MultiChannelWaveform(Waveform): """A MultiChannelWaveform is a Waveform object that allows combining arbitrary Waveform objects to into a single waveform defined for several channels. @@ -80,7 +100,7 @@ def flatten_sub_waveforms(to_flatten): # sort the waveforms with their defined channels to make compare key reproducible def get_sub_waveform_sort_key(waveform): - return sorted(tuple(waveform.defined_channels)) + return tuple(sorted(tuple(waveform.defined_channels))) self._sub_waveforms = sorted(flatten_sub_waveforms(sub_waveforms), key=get_sub_waveform_sort_key) @@ -137,7 +157,116 @@ def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform' raise KeyError('Unknown channels: {}'.format(channels)) -class MultiChannelPulseTemplate(PossiblyAtomicPulseTemplate): +class AtomicMultiChannelPulseTemplate(AtomicPulseTemplate, ParameterConstrainer): + def __init__(self, + *subtemplates: Union[AtomicPulseTemplate, MappingTuple, MappingTemplate], + external_parameters: Optional[Set[str]]=None, + identifier: Optional[str]=None, + parameter_constraints: Optional[List]=None) -> None: + AtomicPulseTemplate.__init__(self, identifier=identifier) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) + + self._subtemplates = [st if isinstance(st, PulseTemplate) else MappingTemplate.from_tuple(st) for st in + subtemplates] + + for subtemplate in self._subtemplates: + if isinstance(subtemplate, AtomicPulseTemplate): + continue + elif isinstance(subtemplate, MappingTemplate): + if isinstance(subtemplate.template, AtomicPulseTemplate): + continue + else: + raise TypeError('Non atomic subtemplate of MappingTemplate: {}'.format(subtemplate.template)) + else: + raise TypeError('Non atomic subtemplate: {}'.format(subtemplate)) + + if not self._subtemplates: + raise ValueError('Cannot create empty MultiChannelPulseTemplate') + + defined_channels = [st.defined_channels for st in self._subtemplates] + + # check there are no intersections between channels + for i, channels_i in enumerate(defined_channels): + for j, channels_j in enumerate(defined_channels[i + 1:]): + if channels_i & channels_j: + raise ChannelMappingException(self._subtemplates[i], + self._subtemplates[i + 1 + j], + (channels_i | channels_j).pop()) + + if external_parameters is not None: + remaining = external_parameters.copy() + for subtemplate in self._subtemplates: + missing = subtemplate.parameter_names - external_parameters + if missing: + raise MissingParameterDeclarationException(subtemplate, missing.pop()) + remaining -= subtemplate.parameter_names + missing = self.constrained_parameters - external_parameters + if missing: + raise MissingParameterDeclarationException(self, missing.pop()) + remaining -= self.constrained_parameters + if remaining: + raise MissingMappingException(subtemplate, remaining.pop()) + + duration = self._subtemplates[0].duration + for subtemplate in self._subtemplates[1:]: + if (duration == subtemplate.duration) is True: + continue + else: + raise ValueError('Could not assert duration equality of {} and {}'.format(duration, + subtemplate.duration)) + + @property + def duration(self) -> Expression: + return self._subtemplates[0].duration + + @property + def parameter_names(self) -> Set[str]: + return set.union(*(st.parameter_names for st in self._subtemplates)) | self.constrained_parameters + + @property + def subtemplates(self) -> List[AtomicPulseTemplate]: + return self._subtemplates + + @property + def defined_channels(self) -> Set[ChannelID]: + return set.union(*(st.defined_channels for st in self._subtemplates)) + + @property + def measurement_names(self) -> Set[str]: + return set.union(*(st.measurement_names for st in self._subtemplates)) + + def build_waveform(self, parameters: Dict[str, numbers.Real], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['MultiChannelWaveform']: + self.validate_parameter_constraints(parameters=parameters) + return MultiChannelWaveform( + [subtemplate.build_waveform(parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) for subtemplate in self._subtemplates]) + + def requires_stop(self, + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition']) -> bool: + return any(st.requires_stop(parameters, conditions) for st in self._subtemplates) + + def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: + data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], + parameter_constraints=self.parameter_constraints) + return data + + @staticmethod + def deserialize(serializer: Serializer, + subtemplates: Iterable[Dict[str, Any]], + atomicity: bool, + identifier: Optional[str] = None) -> 'MultiChannelPulseTemplate': + subtemplates = [serializer.deserialize(st) for st in subtemplates] + external_parameters = set.union(*(st.parameter_names for st in subtemplates)) + mul_template = MultiChannelPulseTemplate(subtemplates, external_parameters, identifier=identifier) + mul_template.atomicity = atomicity + return mul_template + + +class MultiChannelPulseTemplate(PulseTemplate): """A multi-channel group of several AtomicPulseTemplate objects. While SequencePulseTemplate combines several subtemplates (with an identical number of channels) @@ -166,10 +295,8 @@ class MultiChannelPulseTemplate(PossiblyAtomicPulseTemplate): - MultiChannelWaveform """ - SimpleSubTemplate = Tuple[PulseTemplate, Dict[str, str], Dict[ChannelID, ChannelID]] - def __init__(self, - subtemplates: Iterable[Union[PulseTemplate, SimpleSubTemplate]], + subtemplates: Iterable[Union[PulseTemplate, MappingTuple]], external_parameters: Set[str], identifier: str=None) -> None: """Creates a new MultiChannelPulseTemplate instance. @@ -200,26 +327,24 @@ def __init__(self, MissingParameterDeclarationException, if a parameter mapping requires a parameter that was not declared in the external parameters of this MultiChannelPulseTemplate. """ - super().__init__(identifier) + super().__init__(identifier=identifier) + + self._subtemplates = [st if isinstance(st, PulseTemplate) else MappingTemplate.from_tuple(st) for st in subtemplates] + if not self._subtemplates: + raise ValueError('Cannot create empty MultiChannelPulseTemplate') - def to_mapping_template(template, parameter_mapping, channel_mapping): - if not (isinstance(channel_mapping, dict) and all( - isinstance(ch1,(int,str)) and isinstance(ch2,(int,str)) for ch1, ch2 in channel_mapping.items())): - raise ValueError('{} is not a valid channel mapping.'.format(channel_mapping)) - return MappingTemplate(template, parameter_mapping, channel_mapping=channel_mapping) - self.__subtemplates = [st if isinstance(st, PulseTemplate) else to_mapping_template(*st) for st in subtemplates] + defined_channels = [st.defined_channels for st in self._subtemplates] - defined_channels = [st.defined_channels for st in self.__subtemplates] # check there are no intersections between channels - for i, chans1 in enumerate(defined_channels): - for j, chans2 in enumerate(defined_channels[i+1:]): - if chans1 & chans2: - raise ChannelMappingException(self.__subtemplates[i], - self.__subtemplates[i+1+j], - (chans1 | chans2).pop()) + for i, channels_i in enumerate(defined_channels): + for j, channels_j in enumerate(defined_channels[i+1:]): + if channels_i & channels_j: + raise ChannelMappingException(self._subtemplates[i], + self._subtemplates[i + 1 + j], + (channels_i | channels_j).pop()) remaining = external_parameters.copy() - for subtemplate in self.__subtemplates: + for subtemplate in self._subtemplates: missing = subtemplate.parameter_names - external_parameters if missing: raise MissingParameterDeclarationException(subtemplate.template, missing.pop()) @@ -227,20 +352,20 @@ def to_mapping_template(template, parameter_mapping, channel_mapping): if remaining: raise MissingMappingException(subtemplate.template, remaining.pop()) - self.__atomicity = False - @property def parameter_names(self) -> Set[str]: - return set.union(*(st.parameter_names for st in self.__subtemplates)) + return set.union(*(st.parameter_names for st in self._subtemplates)) @property - def parameter_declarations(self) -> Set[ParameterDeclaration]: - # TODO: min, max, default values not mapped (required?) - return {ParameterDeclaration(name) for name in self.parameter_names} + def subtemplates(self) -> Iterable[MappingTemplate]: + return iter(self._subtemplates) @property - def subtemplates(self) -> Iterable[MappingTemplate]: - return iter(self.__subtemplates) + def duration(self) -> Expression: + durations = [subtemplate.duration for subtemplate in self.subtemplates] + equality_condition = ','.join('Eq({}, {})'.format(d1, d2) for d1, d2 in zip(durations, durations[1:])) + return Expression('Piecewise( ({duration}, And({condition})), (nan, True))'.format(duration=durations[0], + condition=equality_condition)) @property def is_interruptable(self) -> bool: @@ -248,23 +373,11 @@ def is_interruptable(self) -> bool: @property def defined_channels(self) -> Set[ChannelID]: - return set.union(*(st.defined_channels for st in self.__subtemplates)) + return set.union(*(st.defined_channels for st in self._subtemplates)) @property def measurement_names(self) -> Set[str]: - return set.union(*(st.measurement_names for st in self.__subtemplates)) - - @property - def atomicity(self): - if any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): - self.__atomicity = False - return self.__atomicity - - @atomicity.setter - def atomicity(self, val: bool): - if val and any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): - raise ValueError('Cannot make atomic as not all sub templates are atomic') - self.__atomicity = val + return set.union(*(st.measurement_names for st in self._subtemplates)) def build_waveform(self, parameters: Dict[str, Parameter], measurement_mapping: Dict[str, str], @@ -272,7 +385,7 @@ def build_waveform(self, parameters: Dict[str, Parameter], return MultiChannelWaveform( [subtemplate.build_waveform(parameters, measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping) for subtemplate in self.__subtemplates]) + channel_mapping=channel_mapping) for subtemplate in self._subtemplates]) def build_sequence(self, sequencer: 'Sequencer', @@ -281,11 +394,21 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - if self.atomicity: - self.atomic_build_sequence(parameters=parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) + + atomic_build = False + if all(subtemplate.atomicity for subtemplate in self.subtemplates): + duration = self.duration + duration = duration.evaluate_numeric(**dict((k, parameters[k].get_value()) + for k in duration.variables)) + if duration >= 0: + atomic_build = True + + if atomic_build: + waveform = self.build_waveform(parameters={k: parameters[k].get_value() for k in self.parameter_names}, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) + if waveform: + instruction_block.add_instruction_exec(waveform) else: channel_to_instruction_block = dict() for subtemplate in self.subtemplates: @@ -297,7 +420,7 @@ def build_sequence(self, def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: - return any(st.requires_stop(parameters, conditions) for st in self.__subtemplates) + return any(st.requires_stop(parameters, conditions) for st in self._subtemplates) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index 9bf606fa3..1cd9e593e 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -10,17 +10,17 @@ """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Optional, Union, Dict, Any, Iterable, Set +from typing import Optional, Union, Dict, Any, Iterable, Set, List from numbers import Real import sympy -from qctoolkit.serialization import Serializable, Serializer +from qctoolkit.serialization import Serializable, Serializer, ExtendedJSONEncoder from qctoolkit.expressions import Expression from qctoolkit.comparable import Comparable -__all__ = ["make_parameter", "ParameterDict", "Parameter", "ParameterDeclaration", "ConstantParameter", - "ParameterNotProvidedException", "ParameterValueIllegalException"] +__all__ = ["make_parameter", "ParameterDict", "Parameter", "ConstantParameter", + "ParameterNotProvidedException", "ParameterConstraintViolation"] def make_parameter(value): @@ -176,357 +176,77 @@ def deserialize(serializer: Serializer, expression: str) -> 'MappedParameter': return MappedParameter(serializer.deserialize(expression)) -class ParameterConstraint(Comparable, Serializable): - def __init__(self, relation: str): - if '==' in relation: +class ParameterConstraint(Comparable): + def __init__(self, relation: Union[str, sympy.Expr]): + super().__init__() + if isinstance(relation, str) and '==' in relation: # The '==' operator is interpreted by sympy as exactly, however we need a symbolical evaluation - self._relation = sympy.Eq(*sympy.sympify(relation.split('=='))) + self._expression = sympy.Eq(*sympy.sympify(relation.split('=='))) else: - self._relation = sympy.sympify(relation) - if not isinstance(self._relation, (sympy.Rel, sympy.boolalg.BooleanAtom)): - raise ValueError('Constraint is no relation') + self._expression = sympy.sympify(relation) + if not isinstance(self._expression, sympy.boolalg.Boolean): + raise ValueError('Constraint is not boolean') @property def affected_parameters(self) -> Set[str]: - return set(str(v) for v in self._relation.free_symbols) + return set(str(v) for v in self._expression.free_symbols) def is_fulfilled(self, parameter: Dict[str, Any]) -> bool: - return bool(self._relation.subs(parameter)) + if not self.affected_parameters <= set(parameter.keys()): + raise ParameterNotProvidedException((self.affected_parameters-set(parameter.keys())).pop()) + return bool(self._expression.subs(parameter)) + + @property + def sympified_expression(self) -> sympy.Expr: + return self._expression @property def compare_key(self) -> sympy.Expr: - return self._relation + return self._expression def __str__(self) -> str: - if isinstance(self._relation, sympy.Eq): - return '{}=={}'.format(self._relation.lhs, self._relation.rhs) + if isinstance(self._expression, sympy.Eq): + return '{}=={}'.format(self._expression.lhs, self._expression.rhs) else: - return str(self._relation) - - def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, str]: - return dict(relation=str(self)) - - @staticmethod - def deserialize(serializer: 'Serializer', relation: str) -> 'ParameterConstraint': - return ParameterConstraint(relation) + return str(self._expression) +ExtendedJSONEncoder.str_constructable_types.add(ParameterConstraint) -class ParameterConstraintViolation(Exception): - def __init__(self, constraint: ParameterConstraint, context: str): - super().__init__("The constraint '{}' is not fulfilled. ".format(constraint) + context) - - -class ParameterDeclaration(Serializable, Comparable): - """A declaration of a parameter required by a pulse template. - - PulseTemplates may declare parameters to allow for variations of values in an otherwise - static pulse structure. ParameterDeclaration represents a declaration of such a parameter - and allows for the definition of boundaries and a default value for a parameter. Boundaries - may be either defined as constant value or as references to another ParameterDeclaration object. - """ - - BoundaryValue = Union[float, 'ParameterDeclaration'] - - def __init__(self, name: str, - min: BoundaryValue=float('-inf'), - max: BoundaryValue=float('+inf'), - default: Optional[float]=None) -> None: - """Creates a ParameterDeclaration object. - - Args: - name (str): A name for the declared parameter. The name must me a valid variable name. - min (float or ParameterDeclaration): An optional real number or - ParameterDeclaration object specifying the minimum value allowed. (default: -inf) - max (float or ParameterDeclaration): An optional real number or - ParameterDeclaration object specifying the maximum value allowed. (default: +inf) - default (float): An optional real number specifying a default value for the declared - pulse template parameter. - """ - super().__init__(None) - if not name.isidentifier(): - raise InvalidParameterNameException(name) - - self.__name = name - self.__min_value = float('-inf') - self.__max_value = float('+inf') - self.__default_value = default # type: Optional[float] - self.min_value = min # type: BoundaryValue - self.max_value = max # type: BoundaryValue - - self.__assert_values_valid() - - def __assert_values_valid(self) -> None: - # ensures that min <= default <= max or raises a ValueError - if self.absolute_min_value > self.absolute_max_value: - raise ValueError("Max value ({0}) is less than min value ({1}).".format( - self.max_value, self.min_value - ) - ) - - if isinstance(self.min_value, ParameterDeclaration): - if self.min_value.absolute_max_value > self.absolute_max_value: - raise ValueError("Max value ({0}) is less than min value ({1}).".format( - self.max_value, self.min_value - ) - ) - - if isinstance(self.max_value, ParameterDeclaration): - if self.max_value.absolute_min_value < self.absolute_min_value: - raise ValueError("Max value ({0}) is less than min value ({1}).".format( - self.max_value, self.min_value - ) - ) - - if self.default_value is not None and self.absolute_min_value > self.default_value: - raise ValueError("Default value ({0}) is less than min value ({1}).".format( - self.default_value, self.min_value - ) - ) - - if self.default_value is not None and self.absolute_max_value < self.default_value: - raise ValueError("Default value ({0}) is greater than max value ({1}).".format( - self.__default_value, self.__max_value - ) - ) - - @property - def name(self) -> str: - """The name of the declared parameter.""" - return self.__name - - @property - def min_value(self) -> BoundaryValue: - """This ParameterDeclaration's minimum value or reference.""" - return self.__min_value - - @min_value.setter - def min_value(self, value: BoundaryValue) -> None: - """Set this ParameterDeclaration's minimum value or reference.""" - old_value = self.__min_value - self.__min_value = value - try: - if (isinstance(value, ParameterDeclaration) and - (isinstance(value.max_value, ParameterDeclaration) or - value.absolute_max_value == float('+inf'))): - value.__internal_set_max_value(self) - self.__assert_values_valid() - except: - self.__min_value = old_value - raise - - def __internal_set_min_value(self, value: BoundaryValue) -> None: - old_value = self.__min_value - self.__min_value = value - try: - self.__assert_values_valid() - except: - self.__min_value = old_value - raise - - @property - def max_value(self) -> BoundaryValue: - """This ParameterDeclaration's maximum value or reference.""" - return self.__max_value - - @max_value.setter - def max_value(self, value: BoundaryValue) -> None: - """Set this ParameterDeclaration's maximum value or reference.""" - old_value = self.__max_value - self.__max_value = value - try: - if (isinstance(value, ParameterDeclaration) and - (isinstance(value.min_value, ParameterDeclaration) or - value.absolute_min_value == float('-inf'))): - value.__internal_set_min_value(self) - self.__assert_values_valid() - except: - self.__max_value = old_value - raise - - def __internal_set_max_value(self, value: BoundaryValue) -> None: - old_value = self.__max_value - self.__max_value = value - try: - self.__assert_values_valid() - except: - self.__max_value = old_value - raise - - @property - def default_value(self) -> Optional[float]: - """This ParameterDeclaration's default value.""" - return self.__default_value - - @property - def absolute_min_value(self) -> float: - """Return this ParameterDeclaration's minimum value. - - If the minimum value of this ParameterDeclaration instance is a reference to another - instance, references are resolved until a concrete value or None is obtained. - """ - if isinstance(self.min_value, ParameterDeclaration): - return self.min_value.absolute_min_value +class ParameterConstrainer: + def __init__(self, *, + parameter_constraints: Optional[Iterable[Union[str, ParameterConstraint]]]) -> None: + if parameter_constraints is None: + self._parameter_constraints = [] else: - return self.min_value + self._parameter_constraints = [constraint if isinstance(constraint, ParameterConstraint) + else ParameterConstraint(constraint) + for constraint in parameter_constraints] @property - def absolute_max_value(self) -> float: - """Return this ParameterDeclaration's maximum value. - - If the maximum value of this ParameterDeclaration instance is a reference to another - instance, references are resolved until a concrete value or None is obtained. - """ - if isinstance(self.max_value, ParameterDeclaration): - return self.max_value.absolute_max_value - else: - return self.max_value - - def is_parameter_valid(self, p: Parameter) -> bool: - """Check whether a given parameter satisfies this ParameterDeclaration statically. - - A parameter is valid if all of the following statements hold: - - If the declaration specifies a minimum value, the parameter's value must be greater or - equal - - If the declaration specifies a maximum value, the parameter's value must be less or equal - - Checks only against the static boundaries. For example, if the min value for this - ParameterDeclaration would be another ParameterDeclaration named 'foo' with a min value of - 3.5, this method only checks whether the given parameter value is greater than or equal to - 3.5. However, the implicit meaning of the reference minimum declaration is, that the value - provided for this ParameterDeclaration must indeed by greater than or equal than the value - provided for the referenced minimum declaration. - - Args: - p (Parameter): The Parameter object checked for validity. - Returns: - True, if p is a valid parameter for this ParameterDeclaration. - See also: - check_parameter_set_valid() - """ - parameter_value = float(p) - is_valid = True - is_valid &= self.absolute_min_value <= parameter_value - is_valid &= self.absolute_max_value >= parameter_value - return is_valid - - def get_value(self, parameters: Dict[str, Parameter]) -> float: - """Retrieve the value of the parameter corresponding to this ParameterDeclaration object - from a set of parameter assignments. - - Args: - parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects. - Returns: - The value of the parameter corresponding to this ParameterDeclaration as a float. - Raises: - ParameterNotProvidedException if no parameter is assigned to the name of this - ParameterDeclaration or any other ParameterDeclaration required to evaluate the - boundary conditions of this ParameterDeclaration. - ParameterValueIllegalException if a parameter exists but its value exceeds the bounds - specified by the corresponding ParameterDeclaration. - """ - value = self.__get_value_internal(parameters) - if not self.check_parameter_set_valid(parameters): - raise ParameterValueIllegalException(self, value) - return value - - def check_parameter_set_valid(self, parameters: Dict[str, Parameter]) -> bool: - """Check whether an entire set of parameters is consistent with this ParameterDeclaration. - - Recursively evaluates referenced min and max ParameterDeclarations (if existent) and checks - whether the values provided for these compare correctly, i.e., does not only perform static - boundary checks. - - Args: - parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects. - Returns: - True, if the values provided for the parameters satisfy all boundary checks for this - ParameterDeclaration. - Raises: - ParameterNotProvidedException if no parameter is assigned to the name of this - ParameterDeclaration or any other ParameterDeclaration required to evaluate the - boundary conditions of this ParameterDeclaration. - ParameterValueIllegalException if a parameter exists but its value exceeds the bounds - specified by the corresponding ParameterDeclaration. - """ - parameter_value = self.__get_value_internal(parameters) - - # get actual instantiated values for boundaries. - min_value = self.min_value - if isinstance(min_value, ParameterDeclaration): - min_value = min_value.__get_value_internal(parameters) - - max_value = self.max_value - if isinstance(max_value, ParameterDeclaration): - max_value = max_value.__get_value_internal(parameters) + def parameter_constraints(self) -> List[ParameterConstraint]: + return self._parameter_constraints - return min_value <= parameter_value and max_value >= parameter_value - - def __get_value_internal(self, parameters: Dict[str, Parameter]) -> float: - try: - return float(parameters[self.name]) # float() wraps get_value for Parameters and works - # for normal floats also - except KeyError: - if self.default_value is not None: - return self.default_value - else: - raise ParameterNotProvidedException(self.name) - - def __str__(self) -> str: - min_value_str = self.absolute_min_value - if isinstance(self.min_value, ParameterDeclaration): - min_value_str = "Parameter '{0}' (min {1})".format(self.min_value.name, min_value_str) - max_value_str = self.absolute_max_value - if isinstance(self.max_value, ParameterDeclaration): - max_value_str = "Parameter '{0}' (max {1})".format(self.max_value.name, max_value_str) - return "{4} '{0}', range ({1}, {2}), default {3}".format( - self.name, min_value_str, max_value_str, self.default_value, type(self) - ) - - def __repr__(self) -> str: - return "<"+self.__str__()+">" + def validate_parameter_constraints(self, parameters: [str, Union[Parameter, Real]]) -> None: + for constraint in self._parameter_constraints: + constraint_parameters = {k: v.get_value() if isinstance(v, Parameter) else v for k, v in parameters.items()} + if not constraint.is_fulfilled(constraint_parameters): + raise ParameterConstraintViolation(constraint, constraint_parameters) @property - def compare_key(self) -> Any: - min_value = self.min_value - if isinstance(min_value, ParameterDeclaration): - min_value = min_value.name - max_value = self.max_value - if isinstance(max_value, ParameterDeclaration): - max_value = max_value.name - return (self.name, min_value, max_value, self.default_value) - - def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - data = dict() - - min_value = self.min_value - if isinstance(min_value, ParameterDeclaration): - min_value = min_value.name - - max_value = self.max_value - if isinstance(max_value, ParameterDeclaration): - max_value = max_value.name + def constrained_parameters(self) -> Set[str]: + if self._parameter_constraints: + return set.union(*(c.affected_parameters for c in self._parameter_constraints)) + else: + return set() - data['name'] = self.name - data['min_value'] = min_value - data['max_value'] = max_value - data['default_value'] = self.default_value - data['type'] = serializer.get_type_identifier(self) - return data +class ParameterConstraintViolation(Exception): + def __init__(self, constraint: ParameterConstraint, parameters: Dict[str, Real]): + super().__init__("The constraint '{}' is not fulfilled.\nParameters: {}".format(constraint, parameters)) + self.constraint = constraint + self.parameters = parameters - @staticmethod - def deserialize(serializer: Serializer, - name: str, - min_value: Union[str, float], - max_value: Union[str, float], - default_value: float) -> 'ParameterDeclaration': - if isinstance(min_value, str): - min_value = float("-inf") - if isinstance(max_value, str): - max_value = float("+inf") - return ParameterDeclaration(name, min=min_value, max=max_value, default=default_value) - class ParameterNotProvidedException(Exception): """Indicates that a required parameter value was not provided.""" @@ -539,21 +259,6 @@ def __str__(self) -> str: "and no default value was specified.".format(self.parameter_name) -class ParameterValueIllegalException(Exception): - """Indicates that the value provided for a parameter is illegal, i.e., is outside the - parameter's bounds or of wrong type.""" - - def __init__(self, parameter_declaration: ParameterDeclaration, parameter_value: float) -> None: - super().__init__() - self.parameter_value = parameter_value - self.parameter_declaration = parameter_declaration - - def __str__(self) -> str: - return "The value {0} provided for parameter {1} is illegal (min = {2}, max = {3})".format( - self.parameter_value, self.parameter_declaration.name, - self.parameter_declaration.min_value, self.parameter_declaration.max_value) - - class InvalidParameterNameException(Exception): def __init__(self, parameter_name: str): self.parameter_name = parameter_name diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index 77b360e68..8790c61f5 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -7,7 +7,7 @@ directly translated into a waveform. """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict, List, Tuple, Set, Optional, Union, Any +from typing import Dict, List, Tuple, Set, Optional, Union, Any, Iterable import itertools from numbers import Real @@ -15,7 +15,7 @@ from qctoolkit.serialization import Serializable from qctoolkit.expressions import Expression -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter +from qctoolkit.pulses.parameters import Parameter from qctoolkit.pulses.sequencing import SequencingElement, InstructionBlock @@ -38,18 +38,14 @@ class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): called instantiation of the PulseTemplate and achieved by invoking the sequencing process. """ - def __init__(self, identifier: Optional[str]=None) -> None: + def __init__(self, *, + identifier: Optional[str]) -> None: super().__init__(identifier) @abstractproperty def parameter_names(self) -> Set[str]: """The set of names of parameters required to instantiate this PulseTemplate.""" - def parameter_declarations(self) -> Set[ParameterDeclaration]: - """The set of ParameterDeclaration objects detailing all parameters required to instantiate - this PulseTemplate. - """ - @abstractproperty def measurement_names(self) -> Set[str]: """The set of measurement identifiers in this pulse template""" @@ -59,10 +55,19 @@ def is_interruptable(self) -> bool: """Return true, if this PulseTemplate contains points at which it can halt if interrupted. """ + @property + @abstractmethod + def duration(self) -> Expression: + """An expression for the duration""" + @abstractproperty def defined_channels(self) -> Set['ChannelID']: """Returns the number of hardware output channels this PulseTemplate defines.""" + @property + def num_channels(self) -> int: + return len(self.defined_channels) + def __matmul__(self, other: 'PulseTemplate') -> 'SequencePulseTemplate': """This method enables us to use the @-operator (intended for matrix multiplication) for concatenating pulses. If one of the pulses is a SequencePulseTemplate the other pulse gets merged into it""" @@ -75,100 +80,20 @@ def __matmul__(self, other: 'PulseTemplate') -> 'SequencePulseTemplate': if double_parameters: raise DoubleParameterNameException(self, other, double_parameters) - external_parameters = self.parameter_names | other.parameter_names subtemplates = itertools.chain(self.subtemplates if isinstance(self, SequencePulseTemplate) else [self], other.subtemplates if isinstance(other, SequencePulseTemplate) else [other]) - return SequencePulseTemplate(subtemplates, external_parameters) - - -class PossiblyAtomicPulseTemplate(PulseTemplate): - """This PulseTemplate may be atomic.""" - def __init__(self, identifier: Optional[str]=None): - super().__init__(identifier=identifier) - - @abstractmethod - def build_waveform(self, - parameters: Dict[str, Real], - measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: - """Translate this PulseTemplate into a waveform according to the given parameters. - - Args: - parameters (Dict(str -> Parameter)): A mapping of parameter names to Parameter objects. - Returns: - Waveform object represented by this AtomicPulseTemplate object or None, if this object - does not represent a valid waveform. - """ - - def atomic_build_sequence(self, - parameters: Dict[str, Parameter], - measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], - instruction_block: InstructionBlock) -> None: - parameters = dict((parameter_name, parameter_value.get_value()) - for parameter_name, parameter_value in parameters.items()) - waveform = self.build_waveform(parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping) - if waveform: - instruction_block.add_instruction_exec(waveform) + return SequencePulseTemplate(*subtemplates) -class AtomicPulseTemplate(PossiblyAtomicPulseTemplate, metaclass=ABCMeta): +class AtomicPulseTemplate(PulseTemplate, metaclass=ABCMeta): """A PulseTemplate that does not imply any control flow disruptions and can be directly translated into a waveform. Implies that no AtomicPulseTemplate object is interruptable. """ - def __init__(self, - identifier: Optional[str]=None, - measurements: Optional[List[MeasurementDeclaration]]=None): + def __init__(self, *, + identifier: Optional[str]): super().__init__(identifier=identifier) - self._measurement_windows = [] if measurements is None else [ - (name, - begin if isinstance(begin, Expression) else Expression(begin), - length if isinstance(length, Expression) else Expression(length)) - for name, begin, length in measurements] - for _, _, length in self._measurement_windows: - if (length < 0) is True: - raise ValueError('Measurement window length may not be negative') - - def get_measurement_windows(self, - parameters: Dict[str, Real], - measurement_mapping: Dict[str, str]) -> List[MeasurementWindow]: - def get_val(v): - return v.evaluate_numeric(**parameters) - - resulting_windows = [(measurement_mapping[name], get_val(begin), get_val(length)) - for name, begin, length in self._measurement_windows] - - duration = get_val(self.duration) - for _, begin, length in resulting_windows: - if begin < 0 or length < 0 or duration < begin + length: - raise ValueError('Measurement window not in pulse or with negative length: {}, {}, {}'.format(begin, - length, - duration)) - return resulting_windows - - @property - def measurement_parameters(self) -> Set[str]: - return set(var - for _, begin, length in self._measurement_windows - for var in itertools.chain(begin.variables, length.variables)) - - @property - def measurement_declarations(self) -> List[MeasurementDeclaration]: - """ - :return: Measurement declarations as added by the add_measurement_declaration method - """ - return [(name, - begin.original_expression, - length.original_expression) - for name, begin, length in self._measurement_windows] - - @property - def measurement_names(self) -> Set[str]: - return set(name for name, _, _ in self._measurement_windows) def is_interruptable(self) -> bool: return False @@ -184,10 +109,30 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - self.atomic_build_sequence(parameters=parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) + parameters = {parameter_name: parameter_value.get_value() + for parameter_name, parameter_value in parameters.items() + if parameter_name in self.parameter_names} + waveform = self.build_waveform(parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) + if waveform: + instruction_block.add_instruction_exec(waveform) + + @abstractmethod + def build_waveform(self, + parameters: Dict[str, Real], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + """Translate this PulseTemplate into a waveform according to the given parameters. + + Args: + parameters (Dict(str -> Parameter)): A mapping of parameter names to real numbers. + measurement_mapping (Dict(str -> str)): A mapping of measurement names + channel_mapping (Dict(ChannelID -> ChannelID): A mapping of Channel IDs + Returns: + Waveform object represented by this PulseTemplate object or None, if this object + does not represent a valid waveform of finite length. + """ class DoubleParameterNameException(Exception): diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index a4bbe5912..fc29c45ed 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -1,13 +1,14 @@ """This module defines PulseTemplateParameterMapping, a helper class for pulse templates that offer mapping of parameters of subtemplates.""" -from typing import Optional, Set, Dict, Union, Iterable, List, Any +from typing import Optional, Set, Dict, Union, Iterable, List, Any, Tuple import itertools +import numbers from qctoolkit import ChannelID from qctoolkit.expressions import Expression -from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate -from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration, MappedParameter, ParameterNotProvidedException +from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.parameters import Parameter, MappedParameter, ParameterNotProvidedException, ParameterConstrainer __all__ = [ @@ -18,38 +19,115 @@ ] -class MappingTemplate(PossiblyAtomicPulseTemplate): - def __init__(self, template: PulseTemplate, - parameter_mapping: Dict[str, str], - measurement_mapping: Dict[str, str] = dict(), - channel_mapping: Dict[ChannelID, ChannelID] = dict()): - super().__init__(None) - - mapped_internal_parameters = set(parameter_mapping.keys()) - internal_parameters = template.parameter_names - if mapped_internal_parameters - internal_parameters: - raise UnnecessaryMappingException(template, mapped_internal_parameters - internal_parameters) - elif internal_parameters - mapped_internal_parameters: - raise MissingMappingException(template, internal_parameters - mapped_internal_parameters) +MappingTuple = Union[Tuple[PulseTemplate], + Tuple[PulseTemplate, Dict], + Tuple[PulseTemplate, Dict, Dict], + Tuple[PulseTemplate, Dict, Dict, Dict]] + + +class MappingTemplate(PulseTemplate, ParameterConstrainer): + def __init__(self, template: PulseTemplate, *, + parameter_mapping: Optional[Dict[str, str]]=None, + measurement_mapping: Optional[Dict[str, str]] = None, + channel_mapping: Optional[Dict[ChannelID, ChannelID]] = None, + parameter_constraints: Optional[List[str]]=None): + """TODO mention nested mappings + + :param template: + :param parameter_mapping: if not none, mappings for all parameters must be specified + :param measurement_mapping: mappings for other measurement names are inserted + :param channel_mapping: mappings for other channels are auto inserted + """ + PulseTemplate.__init__(self, identifier=None) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) + + if parameter_mapping is None: + parameter_mapping = dict((par, par) for par in template.parameter_names) + else: + mapped_internal_parameters = set(parameter_mapping.keys()) + internal_parameters = template.parameter_names + if mapped_internal_parameters - internal_parameters: + raise UnnecessaryMappingException(template, mapped_internal_parameters - internal_parameters) + elif internal_parameters - mapped_internal_parameters: + raise MissingMappingException(template, internal_parameters - mapped_internal_parameters) parameter_mapping = dict((k, Expression(v)) for k, v in parameter_mapping.items()) + measurement_mapping = dict() if measurement_mapping is None else measurement_mapping internal_names = template.measurement_names mapped_internal_names = set(measurement_mapping.keys()) if mapped_internal_names - internal_names: raise UnnecessaryMappingException(template, mapped_internal_names - internal_names) missing_name_mappings = internal_names - mapped_internal_names + channel_mapping = dict() if channel_mapping is None else channel_mapping internal_channels = template.defined_channels mapped_internal_channels = set(channel_mapping.keys()) if mapped_internal_channels - internal_channels: raise UnnecessaryMappingException(template,mapped_internal_channels - internal_channels) missing_channel_mappings = internal_channels - mapped_internal_channels + if isinstance(template, MappingTemplate): + # avoid nested mappings + parameter_mapping = {p: expr.evaluate_symbolic(parameter_mapping) + for p, expr in template.parameter_mapping.items()} + measurement_mapping = {k: measurement_mapping[v] + for k, v in template.measurement_mapping.items()} + channel_mapping = {k: channel_mapping[v] + for k, v in template.channel_mapping.items()} + template = template.template + self.__template = template self.__parameter_mapping = parameter_mapping self.__external_parameters = set(itertools.chain(*(expr.variables for expr in self.__parameter_mapping.values()))) - self.__measurement_mapping = dict(((name,name) for name in missing_name_mappings), **measurement_mapping) - self.__channel_mapping = dict(((name,name) for name in missing_channel_mappings), **channel_mapping) + self.__external_parameters |= self.constrained_parameters + self.__measurement_mapping = dict(itertools.chain(((name, name) for name in missing_name_mappings), + measurement_mapping.items())) + self.__channel_mapping = dict(itertools.chain(((name, name) for name in missing_channel_mappings), + channel_mapping.items())) + + @staticmethod + def from_tuple(mapping_tuple: MappingTuple) -> 'MappingTemplate': + """Auto detect what mappings are meant""" + template, *mappings = mapping_tuple + + parameter_mapping = None + measurement_mapping = None + channel_mapping = None + + for mapping in mappings: + if len(mapping) == 0: + continue + + mapped = set(mapping.keys()) + if sum((mapped <= template.parameter_names, + mapped <= template.measurement_names, + mapped <= template.defined_channels)) > 1: + raise AmbiguousMappingException(template, mapping) + + if mapped == template.parameter_names: + if parameter_mapping: + raise MappingCollisionException(template, object_type='parameter', + mapped=template.parameter_names, + mappings=(parameter_mapping, mapping)) + parameter_mapping = mapping + elif mapped <= template.measurement_names: + if measurement_mapping: + raise MappingCollisionException(template, object_type='measurement', + mapped=template.measurement_names, + mappings=(measurement_mapping, mapping)) + measurement_mapping = mapping + elif mapped <= template.defined_channels: + if channel_mapping: + raise MappingCollisionException(template, object_type='channel', + mapped=template.defined_channels, + mappings=(channel_mapping, mapping)) + channel_mapping = mapping + else: + raise ValueError('Could not match mapping to mapped objects: {}'.format(mapping)) + return MappingTemplate(template, + parameter_mapping=parameter_mapping, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) @property def template(self) -> PulseTemplate: @@ -60,13 +138,16 @@ def measurement_mapping(self) -> Dict[str, str]: return self.__measurement_mapping @property - def parameter_names(self) -> Set[str]: - return self.__external_parameters + def parameter_mapping(self) -> Dict[str, Expression]: + return self.__parameter_mapping @property - def parameter_declarations(self) -> Set[ParameterDeclaration]: - # TODO: min, max, default values not mapped (required?) - return {ParameterDeclaration(name) for name in self.parameter_names} + def channel_mapping(self) -> Dict[ChannelID, ChannelID]: + return self.__channel_mapping + + @property + def parameter_names(self) -> Set[str]: + return self.__external_parameters @property def measurement_names(self) -> Set[str]: @@ -84,9 +165,9 @@ def defined_channels(self) -> Set[ChannelID]: def atomicity(self) -> bool: return self.__template.atomicity - @atomicity.setter - def atomicity(self, val) -> None: - self.__template.atomicity = val + @property + def duration(self) -> Expression: + return self.__template.duration.evaluate_symbolic(self.__parameter_mapping) def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: parameter_mapping_dict = dict((key, str(expression)) for key, expression in self.__parameter_mapping.items()) @@ -102,7 +183,7 @@ def deserialize(serializer: 'Serializer', return MappingTemplate(template=serializer.deserialize(template), **kwargs) def map_parameters(self, - parameters: Dict[str, Parameter]) -> Dict[str, Parameter]: + parameters: Dict[str, Union[Parameter, numbers.Real]]) -> Dict[str, Parameter]: """Map parameter values according to the defined mappings. Args: @@ -116,14 +197,15 @@ def map_parameters(self, if missing: raise ParameterNotProvidedException(missing.pop()) - inner_parameters = { - parameter: MappedParameter( - mapping_function, - {name: parameters[name] for name in mapping_function.variables} - ) - for (parameter, mapping_function) in self.__parameter_mapping.items() - } - return inner_parameters + self.validate_parameter_constraints(parameters=parameters) + if all(isinstance(parameter, Parameter) for parameter in parameters.values()): + return {parameter: MappedParameter(mapping_function, {name: parameters[name] + for name in mapping_function.variables}) + for (parameter, mapping_function) in self.__parameter_mapping.items()} + if all(isinstance(parameter, numbers.Real) for parameter in parameters.values()): + return {parameter: mapping_function.evaluate_numeric(**parameters) + for parameter, mapping_function in self.__parameter_mapping.items()} + raise TypeError('Values of parameter dict are neither all Parameter nor Real') def get_updated_measurement_mapping(self, measurement_mapping: Dict[str, str]) -> Dict[str, str]: return {k: measurement_mapping[v] for k, v in self.__measurement_mapping.items()} @@ -146,7 +228,7 @@ def build_sequence(self, instruction_block=instruction_block) def build_waveform(self, - parameters: Dict[str, Parameter], + parameters: Dict[str, numbers.Real], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> 'Waveform': """This gets called if the parent is atomic""" @@ -207,3 +289,37 @@ def __init__(self, template: PulseTemplate, key: Union[str, Set[str]]) -> None: def __str__(self) -> str: return "Mapping function for parameter(s) '{}', which template {} does not need"\ .format(self.key, self.template) + + +class AutoMappingMatchingException(Exception): + """Indicates that the auto match of mappings to mapped objects by the keys failed""" + + def __init__(self, template: PulseTemplate): + super().__init__() + self.template = template + + +class AmbiguousMappingException(AutoMappingMatchingException): + """Indicates that a mapping may apply to multiple objects""" + + def __init__(self, template: PulseTemplate, mapping: Dict): + super().__init__(template) + self.mapping = mapping + + def __str__(self) -> str: + return "Could not match mapping uniquely to object type: {}\nParameters: {}\nChannels: {}\nMeasurements: {}"\ + .format(self.mapping, self.template.parameter_names, self.template.defined_channels, + self.template.measurement_names) + + +class MappingCollisionException(AutoMappingMatchingException): + """Indicates that multiple mappings are fitting for the same parameter type""" + def __init__(self, template: PulseTemplate, object_type: str, mapped: Set, mappings: Tuple[Dict, ...]): + super().__init__(template) + self.parameter_type = object_type + self.mappings = mappings + self.message = 'Got multiple candidates for the {type} mapping.\nMapped: {mapped}\nCandidates:\n'\ + .format(type=object_type, mapped=mapped) + + def __str__(self) -> str: + return self.message + '\n'.join(str(mapping) for mapping in self.mappings) diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index af6fcd1fb..9d7b22ee8 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -8,11 +8,12 @@ from qctoolkit.serialization import Serializer from qctoolkit import MeasurementWindow, ChannelID -from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate +from qctoolkit.expressions import Expression +from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.loop_pulse_template import LoopPulseTemplate from qctoolkit.pulses.sequencing import Sequencer from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer, Waveform -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, ConstantParameter +from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer, ParameterNotProvidedException from qctoolkit.pulses.conditions import Condition @@ -70,7 +71,7 @@ def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Repetitio repetition_count=self._repetition_count) -class RepetitionPulseTemplate(LoopPulseTemplate): +class RepetitionPulseTemplate(LoopPulseTemplate, ParameterConstrainer): """Repeat a PulseTemplate a constant number of times. The equivalent to a simple for-loop in common programming languages in qctoolkit's pulse @@ -79,8 +80,9 @@ class RepetitionPulseTemplate(LoopPulseTemplate): def __init__(self, body: PulseTemplate, - repetition_count: Union[int, ParameterDeclaration, str], - identifier: Optional[str]=None) -> None: + repetition_count: Union[int, str, Expression], + identifier: Optional[str]=None, + parameter_constraints: Optional[List]=None) -> None: """Create a new RepetitionPulseTemplate instance. Args: @@ -90,29 +92,30 @@ def __init__(self, loop_index (str): If specified the loop index identifier (str): A unique identifier for use in serialization. (optional) """ - super().__init__(identifier=identifier, body=body) - if isinstance(repetition_count, float) and repetition_count.is_integer(): - repetition_count = int(repetition_count) + LoopPulseTemplate.__init__(self, identifier=identifier, body=body) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) - if isinstance(repetition_count, str): - self._repetition_count = ParameterDeclaration(repetition_count, min=0) - elif isinstance(repetition_count, (int, ParameterDeclaration)): - self._repetition_count = repetition_count - else: - raise ValueError('Invalid repetition count type: {}'.format(type(repetition_count))) + if not isinstance(repetition_count, Expression): + repetition_count = Expression(repetition_count) + + if (repetition_count < 0) is True: + raise ValueError('Repetition count may not be negative') + + self._repetition_count = repetition_count @property - def repetition_count(self) -> ParameterDeclaration: + def repetition_count(self) -> Expression: """The amount of repetitions. Either a constant integer or a ParameterDeclaration object.""" return self._repetition_count - def get_repetition_count_value(self, parameters: Dict[str, Parameter]) -> int: - if isinstance(self._repetition_count, ParameterDeclaration): - value = self._repetition_count.get_value(parameters) - if isinstance(value, float) and not value.is_integer(): - raise ParameterNotIntegerException(self._repetition_count.name, value) - return int(value) - else: return self._repetition_count + def get_repetition_count_value(self, parameters: Dict[str, 'Real']) -> int: + value = self._repetition_count.evaluate_numeric(**parameters) + if isinstance(value, float): + if value.is_integer(): + value = int(value) + else: + raise ParameterNotIntegerException(str(self._repetition_count), value) + return value def __str__(self) -> str: return "RepetitionPulseTemplate: <{}> times <{}>"\ @@ -120,20 +123,15 @@ def __str__(self) -> str: @property def parameter_names(self) -> Set[str]: - return set(parameter.name for parameter in self.parameter_declarations) - - @property - def parameter_declarations(self) -> Set[str]: - return self.body.parameter_declarations | ({self._repetition_count} if isinstance(self._repetition_count, - ParameterDeclaration) - else set()) + return self.body.parameter_names | set(self.repetition_count.variables) @property def measurement_names(self) -> Set[str]: return self.body.measurement_names - def build_waveform(self, parameters: Dict[str, Parameter]) -> RepetitionWaveform: - return RepetitionWaveform(self.body.build_waveform(parameters), self.get_repetition_count_value(parameters)) + @property + def duration(self) -> Expression: + return Expression(self.repetition_count * self.body.duration.sympified_expression) def build_sequence(self, sequencer: Sequencer, @@ -142,51 +140,40 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: + self.validate_parameter_constraints(parameters=parameters) + + body_block = InstructionBlock() + body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) - if self.atomicity: - # atomicity can only be enabled if the loop index is not used - self.atomic_build_sequence(parameters=parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - else: - body_block = InstructionBlock() - body_block.return_ip = InstructionPointer(instruction_block, len(instruction_block)) + try: + real_parameters = {v: parameters[v].get_value() for v in self._repetition_count.variables} + except KeyError: + raise ParameterNotProvidedException(next(v for v in self.repetition_count.variables if v not in parameters)) - instruction_block.add_instruction_repj(self.get_repetition_count_value(parameters), body_block) - sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) + instruction_block.add_instruction_repj(self.get_repetition_count_value(real_parameters), body_block) + sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition]) -> bool: - if isinstance(self._repetition_count, ParameterDeclaration): - return parameters[self._repetition_count.name].requires_stop - else: - return False + return any(parameters[v].requires_stop for v in self.repetition_count.variables) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - repetition_count = self._repetition_count - if isinstance(repetition_count, ParameterDeclaration): - repetition_count = serializer.dictify(repetition_count) return dict( - type=serializer.get_type_identifier(self), body=serializer.dictify(self.body), - repetition_count=repetition_count, - atomicity=self.atomicity, + repetition_count=self.repetition_count.original_expression, + parameter_constraints=self.parameter_constraints ) @staticmethod def deserialize(serializer: Serializer, - repetition_count: Dict[str, Any], + repetition_count: Union[str, int], body: Dict[str, Any], - atomicity: bool, + parameter_constraints: List[str], identifier: Optional[str]=None) -> 'Serializable': body = serializer.deserialize(body) - if isinstance(repetition_count, dict): - repetition_count = serializer.deserialize(repetition_count) - result = RepetitionPulseTemplate(body, repetition_count, identifier=identifier) - result.atomicity = atomicity - return result + return RepetitionPulseTemplate(body, repetition_count, + identifier=identifier, parameter_constraints=parameter_constraints) class ParameterNotIntegerException(Exception): diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index a165cec7a..b1b1a1f9c 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -8,13 +8,14 @@ from qctoolkit.serialization import Serializer from qctoolkit import MeasurementWindow, ChannelID -from qctoolkit.pulses.pulse_template import PulseTemplate, PossiblyAtomicPulseTemplate -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter +from qctoolkit.pulses.pulse_template import PulseTemplate +from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer from qctoolkit.pulses.sequencing import InstructionBlock, Sequencer from qctoolkit.pulses.conditions import Condition from qctoolkit.pulses.pulse_template_parameter_mapping import \ - MissingMappingException, MappingTemplate, MissingParameterDeclarationException + MissingMappingException, MappingTemplate, MissingParameterDeclarationException, MappingTuple from qctoolkit.pulses.instructions import Waveform +from qctoolkit.expressions import Expression __all__ = ["SequencePulseTemplate"] @@ -90,7 +91,7 @@ def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform' for sub_waveform in self.__sequenced_waveforms if sub_waveform.defined_channels & channels) -class SequencePulseTemplate(PossiblyAtomicPulseTemplate): +class SequencePulseTemplate(PulseTemplate, ParameterConstrainer): """A sequence of different PulseTemplates. SequencePulseTemplate allows to group several @@ -103,13 +104,11 @@ class SequencePulseTemplate(PossiblyAtomicPulseTemplate): renaming and mathematical transformation of parameters. """ - # a subtemplate consists of a pulse template and mapping functions for its "internal" parameters - SimpleSubTemplate = Tuple[PulseTemplate, Dict[str, str]] # pylint: disable=invalid-name - def __init__(self, - subtemplates: Iterable[Union[SimpleSubTemplate, MappingTemplate]], - external_parameters: Union[Iterable[str], Set[str]], # pylint: disable=invalid-sequence-index - identifier: Optional[str]=None) -> None: + *subtemplates: Union[PulseTemplate, MappingTuple], + external_parameters: Optional[Union[Iterable[str], Set[str]]]=None, + identifier: Optional[str]=None, + parameter_constraints: Optional[List[Union[str, Expression]]]=None) -> None: """Create a new SequencePulseTemplate instance. Requires a (correctly ordered) list of subtemplates in the form @@ -136,36 +135,39 @@ def __init__(self, MissingParameterDeclarationException, if a parameter mapping requires a parameter that was not declared in the external parameters of this SequencePulseTemplate. """ - super().__init__(identifier) + PulseTemplate.__init__(self, identifier=identifier) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) + + self.__subtemplates = [MappingTemplate.from_tuple(st) if isinstance(st, tuple) else st + for st in subtemplates] - self.__subtemplates = [st if not isinstance(st, tuple) else MappingTemplate(*st) for st in subtemplates] - external_parameters = external_parameters if isinstance(external_parameters,set) else set(external_parameters) - # check that all subtempaltes live on the same channels + # check that all subtemplates live on the same channels defined_channels = self.__subtemplates[0].defined_channels for subtemplate in self.__subtemplates[1:]: if subtemplate.defined_channels != defined_channels: raise ValueError('The subtemplates are defined for different channels') - remaining = external_parameters.copy() - for subtemplate in self.__subtemplates: - missing = subtemplate.parameter_names - external_parameters - if missing: - raise MissingParameterDeclarationException(subtemplate.template,missing.pop()) - remaining = remaining - subtemplate.parameter_names - if remaining: - MissingMappingException(subtemplate.template,remaining.pop()) + if external_parameters: + external_parameters = set(external_parameters) + remaining = external_parameters.copy() + for subtemplate in self.__subtemplates: + missing = subtemplate.parameter_names - external_parameters + if missing: + raise MissingParameterDeclarationException(subtemplate, missing.pop()) + remaining -= subtemplate.parameter_names + if not external_parameters >= self.constrained_parameters: + raise MissingParameterDeclarationException(self, + (self.constrained_parameters-external_parameters).pop()) + remaining -= self.constrained_parameters + if remaining: + MissingMappingException(self, remaining.pop()) self.__atomicity = False @property def parameter_names(self) -> Set[str]: return set.union(*(st.parameter_names for st in self.__subtemplates)) - @property - def parameter_declarations(self) -> Set[ParameterDeclaration]: - # TODO: min, max, default values not mapped (required?) - return {ParameterDeclaration(name) for name in self.parameter_names} - @property def subtemplates(self) -> List[MappingTemplate]: return self.__subtemplates @@ -174,6 +176,10 @@ def subtemplates(self) -> List[MappingTemplate]: def is_interruptable(self) -> bool: return any(st.is_interruptable for st in self.subtemplates) + @property + def duration(self) -> Expression: + return Expression(sum(sub.duration.sympified_expression for sub in self.__subtemplates)) + @property def defined_channels(self) -> Set[ChannelID]: return self.__subtemplates[0].defined_channels if self.__subtemplates else set() @@ -182,18 +188,6 @@ def defined_channels(self) -> Set[ChannelID]: def measurement_names(self) -> Set[str]: return set.union(*(st.measurement_names for st in self.subtemplates)) - @property - def atomicity(self) -> bool: - if any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): - self.__atomicity = False - return self.__atomicity - - @atomicity.setter - def atomicity(self, val: bool) -> None: - if val and any(subtemplate.atomicity is False for subtemplate in self.__subtemplates): - raise ValueError('Cannot make atomic as not all sub templates are atomic') - self.__atomicity = val - def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition']) -> bool: @@ -211,30 +205,23 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - # todo: currently ignores is_interruptable - if self.atomicity: - self.atomic_build_sequence(parameters=parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - else: for subtemplate in reversed(self.subtemplates): sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - data = dict() - - data['subtemplates'] = [serializer.dictify(subtemplate) for subtemplate in self.subtemplates] - data['type'] = serializer.get_type_identifier(self) + data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], + parameter_constraints=self.parameter_constraints) return data @staticmethod def deserialize(serializer: Serializer, subtemplates: Iterable[Dict[str, Any]], + parameter_constraints: List[str], identifier: Optional[str]=None) -> 'SequencePulseTemplate': subtemplates = [serializer.deserialize(st) for st in subtemplates] - external_parameters = set.union( *(st.parameter_names for st in subtemplates) ) - seq_template = SequencePulseTemplate(subtemplates, external_parameters, identifier=identifier) + seq_template = SequencePulseTemplate(*subtemplates, + parameter_constraints=parameter_constraints, + identifier=identifier) return seq_template diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index 988bdff23..ec4bcb770 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -26,11 +26,11 @@ class SequencingElement(metaclass=ABCMeta): """ def __init__(self) -> None: - super().__init__() + pass - @abstractproperty def atomicity(self) -> bool: """Is the element translated to a single EXECInstruction with one waveform""" + raise NotImplementedError() @abstractmethod def build_sequence(self, diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index fd0a426fe..1f80aa3c3 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -17,8 +17,8 @@ from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.serialization import Serializer -from qctoolkit.pulses.parameters import ParameterDeclaration, Parameter, \ - ParameterNotProvidedException, ParameterConstraint, ParameterConstraintViolation +from qctoolkit.pulses.parameters import Parameter, \ + ParameterNotProvidedException, ParameterConstraint, ParameterConstraintViolation, ParameterConstrainer from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.interpolation import InterpolationStrategy, LinearInterpolationStrategy, \ HoldInterpolationStrategy, JumpInterpolationStrategy @@ -26,6 +26,7 @@ from qctoolkit.pulses.conditions import Condition from qctoolkit.expressions import Expression from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform +from qctoolkit.pulses.measurement import MeasurementDefiner __all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] @@ -115,19 +116,20 @@ def interp(self) -> InterpolationStrategy: return self[2] -class TablePulseTemplate(AtomicPulseTemplate): +class TablePulseTemplate(AtomicPulseTemplate, ParameterConstrainer, MeasurementDefiner): interpolation_strategies = {'linear': LinearInterpolationStrategy(), 'hold': HoldInterpolationStrategy(), 'jump': JumpInterpolationStrategy()} def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], identifier: Optional[str]=None, - parameter_constraints: Optional[List[str]]=None, + *, + parameter_constraints: Optional[List[Union[str, ParameterConstraint]]]=None, measurements: Optional[List[MeasurementDeclaration]]=None, consistency_check=True): - super().__init__(identifier=identifier, measurements=measurements) - if parameter_constraints is None: - parameter_constraints = list() + AtomicPulseTemplate.__init__(self, identifier=identifier) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) + MeasurementDefiner.__init__(self, measurements=measurements) self._entries = dict((ch, list()) for ch in entries.keys()) for channel, channel_entries in entries.items(): @@ -136,7 +138,6 @@ def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], for entry in channel_entries: self._add_entry(channel, TableEntry(*entry)) - self._parameter_constraints = [ParameterConstraint(constraint) for constraint in parameter_constraints] if self.duration == 0: warnings.warn('Table pulse template with duration 0 on construction.', @@ -147,7 +148,7 @@ def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], # sympy solver does not support them # collect all conditions - inequalities = [sympy.sympify(eq) for eq in parameter_constraints] +\ + inequalities = [eq.sympified_expression for eq in self._parameter_constraints] +\ [sympy.Le(previous_entry.t.compare_key, entry.t.compare_key) for channel_entries in self._entries.values() for previous_entry, entry in zip(channel_entries, channel_entries[1:])] @@ -180,6 +181,10 @@ def _add_entry(self, channel, new_entry: TableEntry) -> None: def entries(self) -> Dict[ChannelID, List[TableEntry]]: return self._entries + @property + def measurement_names(self) -> Set[str]: + return {name for name, _, _ in self._measurement_windows} + def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ -> Dict[ChannelID, List[WaveformTableEntry]]: """Compute an instantiated list of the table's entries. @@ -254,11 +259,7 @@ def table_parameters(self) -> Set[str]: for channel_entries in self.entries.values() for entry in channel_entries for var in itertools.chain(entry.t.variables, entry.v.variables) - ) | set( - var - for constraint in self._parameter_constraints - for var in constraint.affected_parameters - ) + ) | self.constrained_parameters @property def parameter_names(self) -> Set[str]: @@ -295,17 +296,16 @@ def num_channels(self) -> int: return len(self._entries) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - data = dict( + return dict( entries=dict( (channel, [(entry.t.get_most_simple_representation(), entry.v.get_most_simple_representation(), str(entry.interp)) for entry in channel_entries]) for channel, channel_entries in self._entries.items() ), - parameter_constraints=[str(constraint) for constraint in self._parameter_constraints], + parameter_constraints=[str(c) for c in self.parameter_constraints], measurements=self.measurement_declarations ) - return data @staticmethod def deserialize(serializer: Serializer, @@ -323,11 +323,7 @@ def build_waveform(self, parameters: Dict[str, numbers.Real], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: - for constraint in self._parameter_constraints: - if not constraint.is_fulfilled(parameters): - raise ParameterConstraintViolation(constraint, 'parameters: {}'.format( - {name: parameters[name] for name in constraint.affected_parameters} - )) + self.validate_parameter_constraints(parameters) instantiated = [(channel_mapping[channel], instantiated_channel) for channel, instantiated_channel in self.get_entries_instantiated(parameters).items()] @@ -336,10 +332,10 @@ def build_waveform(self, measurements = self.get_measurement_windows(parameters=parameters, measurement_mapping=measurement_mapping) if len(instantiated) == 1: - return TableWaveform(*instantiated.pop(), measurements) + return TableWaveform(*instantiated.pop(), measurement_windows=measurements) else: return MultiChannelWaveform( - [TableWaveform(*instantiated.pop(), measurements)] + [TableWaveform(*instantiated.pop(), measurement_windows=measurements)] + [TableWaveform(channel, instantiated_channel, [])for channel, instantiated_channel in instantiated]) diff --git a/qctoolkit/serialization.py b/qctoolkit/serialization.py index 3b4388286..f783bbc64 100644 --- a/qctoolkit/serialization.py +++ b/qctoolkit/serialization.py @@ -410,7 +410,7 @@ def serialize(self, serializable: Serializable, overwrite=False) -> None: storage_identifier = identifier if identifier == '': storage_identifier = 'main' - json_str = json.dumps(repr_[identifier], indent=4, sort_keys=True) + json_str = json.dumps(repr_[identifier], indent=4, sort_keys=True, cls=ExtendedJSONEncoder) self.__storage_backend.put(storage_identifier, json_str, overwrite) def deserialize(self, representation: Union[str, Dict[str, Any]]) -> Serializable: @@ -437,3 +437,19 @@ def deserialize(self, representation: Union[str, Dict[str, Any]]) -> Serializabl repr_.pop('type') return class_.deserialize(self, **repr_) + + +class ExtendedJSONEncoder(json.JSONEncoder): + """Encodes types that are registered in str_constructable_types as str and sets as lists.""" + str_constructable_types = set() + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def default(self, o: Any) -> Any: + if type(o) in ExtendedJSONEncoder.str_constructable_types: + return str(o) + elif type(o) is set: + return list(o) + else: + return super().default(o) From 13eabe8c7ad569df17d9034889dfc575cc936f73 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Fri, 19 May 2017 10:54:13 +0200 Subject: [PATCH 059/116] Extend tests Add tools to test all methods are tested --- tests/__init__.py | 3 +- tests/coverage_tests.py | 13 + tests/expression_tests.py | 2 +- tests/format_tests.py | 3 +- tests/hardware/setup_tests.py | 127 ++++++ tests/property_test_helper.py | 55 +++ tests/pulses/branch_pulse_template_tests.py | 4 +- tests/pulses/function_pulse_tests.py | 161 +++++-- tests/pulses/loop_pulse_template_tests.py | 8 +- tests/pulses/measurement_tests.py | 110 +++++ .../multi_channel_pulse_template_tests.py | 207 ++++++--- tests/pulses/parameters_tests.py | 411 +----------------- .../pulse_template_parameter_mapping_tests.py | 87 +++- tests/pulses/pulse_template_tests.py | 143 +++--- .../pulses/repetition_pulse_template_tests.py | 56 ++- tests/pulses/sequence_pulse_template_tests.py | 97 ++--- tests/pulses/sequencing_dummies.py | 20 +- tests/pulses/table_pulse_template_tests.py | 82 +++- ...e_sequence_sequencer_intergration_tests.py | 4 +- tests/serialization_tests.py | 35 +- 20 files changed, 916 insertions(+), 712 deletions(-) create mode 100644 tests/coverage_tests.py create mode 100644 tests/hardware/setup_tests.py create mode 100644 tests/property_test_helper.py create mode 100644 tests/pulses/measurement_tests.py diff --git a/tests/__init__.py b/tests/__init__.py index 38d7827ff..1ba212dc6 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,5 +6,6 @@ 'format_tests', 'pyflakes_syntax_tests', 'serialization_dummies', - 'serialization_tests' + 'serialization_tests', + 'coverage_tests' ] \ No newline at end of file diff --git a/tests/coverage_tests.py b/tests/coverage_tests.py new file mode 100644 index 000000000..8d737ec76 --- /dev/null +++ b/tests/coverage_tests.py @@ -0,0 +1,13 @@ +import qctoolkit.pulses.function_pulse_template +import qctoolkit.pulses.table_pulse_template + +import tests.pulses.function_pulse_tests +import tests.pulses.table_pulse_template_tests + +from tests.property_test_helper import assert_public_functions_tested_tester + + +FunctionPublicFunctionsTested = assert_public_functions_tested_tester(tests.pulses.function_pulse_tests, + qctoolkit.pulses.function_pulse_template) +TablePublicFunctionsTested = assert_public_functions_tested_tester(tests.pulses.table_pulse_template_tests, + qctoolkit.pulses.table_pulse_template) \ No newline at end of file diff --git a/tests/expression_tests.py b/tests/expression_tests.py index 6482b11de..01ccddbba 100644 --- a/tests/expression_tests.py +++ b/tests/expression_tests.py @@ -4,7 +4,7 @@ from sympy import sympify from qctoolkit.expressions import Expression, ExpressionVariableMissingException - +from qctoolkit.serialization import Serializer class ExpressionTests(unittest.TestCase): diff --git a/tests/format_tests.py b/tests/format_tests.py index 41cd099c4..f67dfb5f5 100644 --- a/tests/format_tests.py +++ b/tests/format_tests.py @@ -58,6 +58,7 @@ def test_annotations(self): if method not in whitelist: if hasattr(imported, method): loaded_method = getattr(imported, method) - if hasattr(loaded_method,"__call__"): + if hasattr(loaded_method, "__call__") and hasattr(loaded_method, + "__annotations__"): self.assertIn("return",loaded_method.__annotations__,"No Return annotation found for Module: {}, Class: {}, Method: {}".format(package,name,method)) self.assertNotEqual(len(loaded_method.__annotations__.keys()),0,"No Annotation found. Module: {}, Class: {}, Method: {}".format(package,name,method)) diff --git a/tests/hardware/setup_tests.py b/tests/hardware/setup_tests.py new file mode 100644 index 000000000..72b45dc7c --- /dev/null +++ b/tests/hardware/setup_tests.py @@ -0,0 +1,127 @@ +import unittest +import itertools + + +from qctoolkit.hardware.setup import HardwareSetup, ChannelID, PlaybackChannel, _SingleChannel, MarkerChannel +from qctoolkit.hardware.awgs.base import DummyAWG + +from.program_tests import get_two_chan_test_block, WaveformGenerator + + +class SingleChannelTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.awg1 = DummyAWG(num_channels=2, num_markers=2) + self.awg2 = DummyAWG(num_channels=2, num_markers=2) + + def test_eq_play_play(self): + self.assertEqual(PlaybackChannel(self.awg1, 0), + PlaybackChannel(self.awg1, 0)) + self.assertEqual(PlaybackChannel(self.awg1, 0, lambda x: 0), + PlaybackChannel(self.awg1, 0)) + self.assertEqual(PlaybackChannel(self.awg1, 0), + PlaybackChannel(self.awg1, 0, lambda x: 0)) + + self.assertNotEqual(PlaybackChannel(self.awg1, 0), + PlaybackChannel(self.awg1, 1)) + self.assertNotEqual(PlaybackChannel(self.awg1, 0), + PlaybackChannel(self.awg2, 0)) + + def test_eq_play_mark(self): + self.assertNotEqual(MarkerChannel(self.awg1, 0), + PlaybackChannel(self.awg1, 0)) + self.assertNotEqual(PlaybackChannel(self.awg1, 0), + MarkerChannel(self.awg1, 0)) + self.assertNotEqual(PlaybackChannel(self.awg1, 0, lambda x: 0), + MarkerChannel(self.awg1, 0)) + self.assertNotEqual(MarkerChannel(self.awg1, 0), + PlaybackChannel(self.awg1, 0, lambda x: 0)) + + self.assertNotEqual(MarkerChannel(self.awg1, 0), + PlaybackChannel(self.awg1, 1)) + self.assertNotEqual(PlaybackChannel(self.awg1, 0), + MarkerChannel(self.awg2, 0)) + + def test_eq_mark_mark(self): + self.assertEqual(MarkerChannel(self.awg1, 0), + MarkerChannel(self.awg1, 0)) + + self.assertNotEqual(MarkerChannel(self.awg1, 0), + MarkerChannel(self.awg1, 1)) + self.assertNotEqual(MarkerChannel(self.awg1, 0), + MarkerChannel(self.awg2, 0)) + + +class HardwareSetupTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_set_channel(self): + awg1 = DummyAWG() + awg2 = DummyAWG(num_channels=2) + + setup = HardwareSetup() + + setup.set_channel('A', PlaybackChannel(awg1, 0)) + setup.set_channel('B', PlaybackChannel(awg2, 0)) + self.assertEqual(setup.registered_channels(), + dict(A={PlaybackChannel(awg1, 0)}, + B={PlaybackChannel(awg2, 0)})) + + with self.assertRaises(ValueError): + setup.set_channel('C', PlaybackChannel(awg1, 0)) + setup.set_channel('A', PlaybackChannel(awg2, 1)) + self.assertEqual(setup.registered_channels(), + dict(A={PlaybackChannel(awg2, 1), PlaybackChannel(awg1, 0)}, + B={PlaybackChannel(awg2, 0)})) + + def test_register_program(self): + awg1 = DummyAWG() + awg2 = DummyAWG(num_channels=2, num_markers=5) + + setup = HardwareSetup() + + wfg = WaveformGenerator(num_channels=2, duration_generator=itertools.repeat(1)) + block = get_two_chan_test_block(wfg) + + class ProgStart: + def __init__(self): + self.was_started = False + + def __call__(self): + self.was_started = True + program_started = ProgStart() + + trafo1 = lambda x: x + trafo2 = lambda x: x + + setup.set_channel('A', PlaybackChannel(awg1, 0, trafo1)) + setup.set_channel('B', PlaybackChannel(awg2, 1, trafo2)) + setup.register_program('p1', block, program_started) + + self.assertEqual(tuple(setup.registered_programs.keys()), ('p1',)) + self.assertEqual(setup.registered_programs['p1'].run_callback, program_started) + self.assertEqual(setup.registered_programs['p1'].awgs_to_upload_to, {awg1, awg2}) + + self.assertEqual(len(awg1._programs), 1) + self.assertEqual(len(awg2._programs), 1) + + _, channels, markers, trafos = awg1._programs['p1'] + self.assertEqual(channels, ('A', )) + self.assertEqual(markers, (None,)) + self.assertEqual(trafos, (trafo1,)) + + _, channels, markers, trafos = awg2._programs['p1'] + self.assertEqual(channels, (None, 'B')) + self.assertEqual(markers, (None,)*5) + self.assertEqual(trafos, (None, trafo2)) + + self.assertEqual(awg1._armed, None) + self.assertEqual(awg2._armed, None) + setup.arm_program('p1') + self.assertEqual(awg1._armed, 'p1') + self.assertEqual(awg2._armed, 'p1') + + + diff --git a/tests/property_test_helper.py b/tests/property_test_helper.py new file mode 100644 index 000000000..065399470 --- /dev/null +++ b/tests/property_test_helper.py @@ -0,0 +1,55 @@ +import inspect +import unittest + +class assert_all_properties_tested: + def __init__(self, to_test: type): + self.expected_tests = [] + base_class_members = [member + for baseclass in inspect.getmro(to_test)[1:] + for member in inspect.getmembers(baseclass)] + for name, member in inspect.getmembers(to_test): + if name.startswith('_'): + continue + if (name, member) in base_class_members: + continue + if inspect.isdatadescriptor(member): + self.expected_tests.append((name, 'test_' + name)) + + def __call__(self, tester_type: type): + def test_all_properties_tested(tester: unittest.TestCase): + not_tested = [member_name + for member_name, test_name in self.expected_tests if not hasattr(tester, test_name)] + tester.assertFalse(not_tested, 'Missing property test for {}'.format(not_tested)) + + tester_type.test_all_properties_tested = test_all_properties_tested + return tester_type + + +def assert_public_functions_tested_tester(testing_module, tested_module): + def get_public_functions(cls): + for name, member in inspect.getmembers(cls): + if inspect.getmodule(member) is tested_module: + if not name.startswith('_'): + if inspect.isfunction(member): + yield name + elif inspect.isclass(member): + yield from get_public_functions(member) + to_test = list(get_public_functions(tested_module)) + + def get_tests(cls): + for name, member in inspect.getmembers(cls): + if not name.startswith('_') and inspect.getmodule(member) is testing_module: + if inspect.isfunction(member) and name.startswith('test_'): + yield name + elif inspect.isclass(member) and issubclass(member, unittest.TestCase): + yield from get_tests(member) + testing_functions = list(get_tests(testing_module)) + + class PublicFunctionTest(unittest.TestCase): + def test_all_public_functions_tested(self): + non_tested_functions = [name for name in to_test if not any(testing_function.startswith('test_' + name) + for testing_function in testing_functions)] + self.assertFalse(non_tested_functions, "{} has no tests for {}".format(testing_module.__name__, + non_tested_functions)) + + return PublicFunctionTest diff --git a/tests/pulses/branch_pulse_template_tests.py b/tests/pulses/branch_pulse_template_tests.py index 105640bbe..5f1437602 100644 --- a/tests/pulses/branch_pulse_template_tests.py +++ b/tests/pulses/branch_pulse_template_tests.py @@ -1,8 +1,8 @@ import unittest from qctoolkit.pulses.branch_pulse_template import BranchPulseTemplate -from qctoolkit.pulses.parameters import ParameterDeclaration from qctoolkit.pulses.conditions import ConditionMissingException +from qctoolkit.pulses.parameters import ParameterConstraint from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummyParameter, DummyCondition, DummySequencer, DummyInstructionBlock from tests.serialization_dummies import DummySerializer @@ -27,7 +27,6 @@ def test_parameter_names_and_declarations(self) -> None: else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}) template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) self.assertEqual({'foo', 'bar', 'hugo'}, template.parameter_names) - self.assertEqual({ParameterDeclaration(name) for name in {'foo', 'bar', 'hugo'}}, template.parameter_declarations) def test_defined_channels(self) -> None: if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) @@ -185,6 +184,7 @@ def setUp(self) -> None: self.if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}, measurement_names={'if_mease'}) self.else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}, measurement_names={'else_meas'}) self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) + self.constraints = [''] def test_get_serialization_data(self) -> None: expected_data = dict( diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index be3ea4fcb..51c88ced0 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -3,54 +3,95 @@ from qctoolkit.pulses.function_pulse_template import FunctionPulseTemplate,\ FunctionWaveform -from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException +from qctoolkit.serialization import Serializer +from qctoolkit.pulses.parameters import ParameterNotProvidedException from qctoolkit.expressions import Expression from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform +from qctoolkit.pulses.parameters import ParameterConstraintViolation import numpy as np -from tests.serialization_dummies import DummySerializer +from tests.serialization_dummies import DummySerializer, DummyStorageBackend from tests.pulses.sequencing_dummies import DummyParameter +from tests.pulses.measurement_tests import MeasurementDefinerTest, ParameterConstrainerTest +from tests.property_test_helper import assert_all_properties_tested class FunctionPulseTest(unittest.TestCase): - def setUp(self) -> None: self.maxDiff = None self.s = 'a + b * t' self.s2 = 'c' self.meas_list = [('mw', 1, 1), ('mw', 'x', 'z'), ('drup', 'j', 'u')] - self.meas_dict = {'mw': [(1, 1), ('x', 'z')], 'drup': [('j', 'u')]} + + self.constraints = ['a < b', 'c > 1', 'd > c'] + + self.valid_par_vals = dict(a=1, + b=2, + c=14.5, + d=15, + x=0.1, + z=0.2, + j=0.3, + u=0.4) + + self.invalid_par_vals = dict(a=1, + b=2, + c=14.5, + d=14, + x=0.1, + z=0.2, + j=0.3, + u=0.4) self.fpt = FunctionPulseTemplate(self.s, self.s2, channel='A', - measurements=self.meas_list) + measurements=self.meas_list, + parameter_constraints=self.constraints) self.pars = dict(a=DummyParameter(1), b=DummyParameter(2), c=DummyParameter(136.78)) + +@assert_all_properties_tested(to_test=FunctionPulseTemplate) +class FunctionPulsePropertyTest(FunctionPulseTest): + def test_expression(self): + self.assertEqual(self.fpt.expression, self.s) + + def test_function_parameters(self): + self.assertEqual(self.fpt.function_parameters, {'a', 'b', 'c'}) + def test_is_interruptable(self) -> None: self.assertFalse(self.fpt.is_interruptable) def test_defined_channels(self) -> None: self.assertEqual({'A'}, self.fpt.defined_channels) + def test_parameter_names(self): + self.assertEqual(self.fpt.parameter_names, {'a', 'b', 'c', 'd', 'x', 'z', 'j', 'u'}) + + def test_duration(self): + self.assertEqual(self.fpt.duration, self.s2) + + def test_measurement_names(self): + self.assertEqual(self.fpt.measurement_names, {'mw', 'drup'}) + def test_parameter_names_and_declarations_expression_input(self) -> None: template = FunctionPulseTemplate(Expression("3 * foo + bar * t"), Expression("5 * hugo")) expected_parameter_names = {'foo', 'bar', 'hugo'} self.assertEqual(expected_parameter_names, template.parameter_names) - self.assertEqual({ParameterDeclaration(name) for name in expected_parameter_names}, template.parameter_declarations) def test_parameter_names_and_declarations_string_input(self) -> None: template = FunctionPulseTemplate("3 * foo + bar * t", "5 * hugo",channel='A') expected_parameter_names = {'foo', 'bar', 'hugo'} self.assertEqual(expected_parameter_names, template.parameter_names) - self.assertEqual({ParameterDeclaration(name) for name in expected_parameter_names}, - template.parameter_declarations) - def test_serialization_data(self) -> None: + +class FunctionPulseSerializationTest(FunctionPulseTest): + def test_get_serialization_data(self) -> None: expected_data = dict(duration_expression=str(self.s2), expression=str(self.s), channel='A', - measurement_declarations=self.meas_list) + measurement_declarations=self.meas_list, + parameter_constraints=self.constraints) self.assertEqual(expected_data, self.fpt.get_serialization_data( DummySerializer(serialize_callback=lambda x: x.original_expression))) @@ -59,47 +100,93 @@ def test_deserialize(self) -> None: expression=self.s, channel='A', identifier='hugo', - measurement_declarations=self.meas_list) + measurement_declarations=self.meas_list, + parameter_constraints=self.constraints) serializer = DummySerializer(serialize_callback=lambda x: x.original_expression) serializer.subelements[self.s2] = Expression(self.s2) serializer.subelements[self.s] = Expression(self.s) template = FunctionPulseTemplate.deserialize(serializer, **basic_data) self.assertEqual('hugo', template.identifier) - self.assertEqual({'a', 'b', 'c', 'x', 'z', 'j', 'u'}, template.parameter_names) - self.assertEqual({ParameterDeclaration(name) for name in template.parameter_names}, - template.parameter_declarations) + self.assertEqual({'a', 'b', 'c', 'x', 'z', 'j', 'u', 'd'}, template.parameter_names) self.assertEqual(template.measurement_declarations, self.meas_list) serialized_data = template.get_serialization_data(serializer) del basic_data['identifier'] self.assertEqual(basic_data, serialized_data) + def test_serializer_integration(self): + before = FunctionPulseTemplate(expression=self.s, + duration_expression=self.s2, + channel='A', + measurements=self.meas_list, + parameter_constraints=self.constraints, + identifier='my_tpt') + serializer = Serializer(DummyStorageBackend()) + serializer.serialize(before) + after = serializer.deserialize('my_tpt') -class FunctionPulseSequencingTest(unittest.TestCase): + self.assertIsInstance(after, FunctionPulseTemplate) + self.assertEqual(before.expression, after.expression) + self.assertEqual(before.duration, after.duration) + self.assertEqual(before.defined_channels, after.defined_channels) - def setUp(self) -> None: - unittest.TestCase.setUp(self) - self.f = "a * t" - self.duration = "y" - self.args = dict(a=DummyParameter(3),y=DummyParameter(1)) - self.fpt = FunctionPulseTemplate(self.f, self.duration) + self.assertEqual(before.measurement_declarations, after.measurement_declarations) + self.assertEqual(before.parameter_constraints, after.parameter_constraints) - @unittest.skip + +class FunctionPulseSequencingTest(FunctionPulseTest): def test_build_waveform(self) -> None: - wf = self.fpt.build_waveform(self.args, {}, channel_mapping={'default': 'default'}) + with self.assertRaises(ParameterConstraintViolation): + self.fpt.build_waveform(self.invalid_par_vals, measurement_mapping={'mw': 'mw', + 'drup': 'jupp'}, channel_mapping={'A': 'B'}) + + wf = self.fpt.build_waveform(self.valid_par_vals, measurement_mapping={'mw': 'mw', + 'drup': 'jupp'}, channel_mapping={'A': 'B'}) self.assertIsNotNone(wf) - self.assertIsInstance(wf, MultiChannelWaveform) - expected_waveform = MultiChannelWaveform({'default': FunctionWaveform(Expression(self.f), - Expression(self.duration))}) + self.assertIsInstance(wf, FunctionWaveform) + + expression = Expression(self.s).evaluate_symbolic(self.valid_par_vals) + duration = Expression(self.s2).evaluate_numeric(c=self.valid_par_vals['c']) + + expected_waveform = FunctionWaveform(expression, duration=duration, channel='B', + measurement_windows=[('mw', 1, 1), + ('mw', + self.valid_par_vals['x'], + self.valid_par_vals['z']), + ('jupp', + self.valid_par_vals['j'], + self.valid_par_vals['u'])]) self.assertEqual(expected_waveform, wf) def test_requires_stop(self) -> None: - parameters = dict(a=DummyParameter(36.126), y=DummyParameter(247.9543)) + parameters = dict(a=DummyParameter(36.126), z=DummyParameter(247.9543)) self.assertFalse(self.fpt.requires_stop(parameters, dict())) - parameters = dict(a=DummyParameter(36.126), y=DummyParameter(247.9543, requires_stop=True)) + parameters = dict(a=DummyParameter(36.126), z=DummyParameter(247.9543, requires_stop=True)) self.assertTrue(self.fpt.requires_stop(parameters, dict())) +class TablePulseTemplateConstraintTest(ParameterConstrainerTest): + def __init__(self, *args, **kwargs): + + def tpt_constructor(parameter_constraints=None): + return FunctionPulseTemplate('a*t', 'duration', + parameter_constraints=parameter_constraints, measurements=[('M', 'n', 1)]) + + super().__init__(*args, + to_test_constructor=tpt_constructor, **kwargs) + + +class TablePulseTemplateMeasurementTest(MeasurementDefinerTest): + def __init__(self, *args, **kwargs): + + def tpt_constructor(measurements=None): + return FunctionPulseTemplate('a*t', 'duration', + parameter_constraints=['a < b'], measurements=measurements) + + super().__init__(*args, + to_test_constructor=tpt_constructor, **kwargs) + + class FunctionWaveformTest(unittest.TestCase): def test_equality(self) -> None: @@ -136,17 +223,15 @@ def test_unsafe_sample(self): np.testing.assert_equal(result, expected_result) self.assertIs(result, out_array) + def test_unsafe_get_subset_for_channels(self): + fw = FunctionWaveform(Expression('sin(2*pi*t) + 3'), 5, channel='A', + measurement_windows=[('asd', 0, 3), ('om', 1, 2)]) + self.assertIs(fw.unsafe_get_subset_for_channels({'A'}), fw) - @unittest.skip - def test_sample(self) -> None: - f = Expression("(t+1)**b") - length = Expression("c**b") - par = {"b":2,"c":10} - fw = FunctionWaveform(f, length, measurement_windows=[], channel='A') - a = np.arange(4) - expected_result = [[1, 4, 9, 16]] - result = fw.sample(a) - self.assertTrue(np.all(result == expected_result)) + def test_get_measurement_windows(self): + fw = FunctionWaveform(Expression('sin(2*pi*t) + 3'), 5, channel='A', + measurement_windows=[('asd', 0, 3), ('om', 1, 2)]) + self.assertEqual(fw.get_measurement_windows(), [('asd', 0, 3), ('om', 1, 2)]) class FunctionPulseMeasurementTest(unittest.TestCase): diff --git a/tests/pulses/loop_pulse_template_tests.py b/tests/pulses/loop_pulse_template_tests.py index a0171155e..8d6172618 100644 --- a/tests/pulses/loop_pulse_template_tests.py +++ b/tests/pulses/loop_pulse_template_tests.py @@ -92,11 +92,9 @@ def test_parameter_names_and_declarations(self) -> None: body = DummyPulseTemplate() t = WhileLoopPulseTemplate(condition, body) self.assertEqual(body.parameter_names, t.parameter_names) - self.assertEqual(body.parameter_declarations, t.parameter_declarations) body.parameter_names_ = {'foo', 't', 'bar'} self.assertEqual(body.parameter_names, t.parameter_names) - self.assertEqual(body.parameter_declarations, t.parameter_declarations) @unittest.skip def test_is_interruptable(self) -> None: @@ -177,8 +175,7 @@ def test_get_serialization_data(self) -> None: serializer = DummySerializer() expected_data = dict(type=serializer.get_type_identifier(t), body=str(id(body)), - condition=condition_name, - atomicity=False) + condition=condition_name) data = t.get_serialization_data(serializer) self.assertEqual(expected_data, data) @@ -187,8 +184,7 @@ def test_deserialize(self) -> None: data = dict( identifier='foo_loop', condition='foo_cond', - body='bodyDummyPulse', - atomicity=False + body='bodyDummyPulse' ) # prepare dependencies for deserialization diff --git a/tests/pulses/measurement_tests.py b/tests/pulses/measurement_tests.py new file mode 100644 index 000000000..bdccfb633 --- /dev/null +++ b/tests/pulses/measurement_tests.py @@ -0,0 +1,110 @@ +import unittest + +from qctoolkit.pulses.parameters import ParameterConstraint, ParameterConstraintViolation,\ + ParameterNotProvidedException, ParameterConstrainer +from qctoolkit.pulses.measurement import MeasurementDefiner + + +class MeasurementDefinerTest(unittest.TestCase): + def __init__(self, *args, to_test_constructor=None, **kwargs): + super().__init__(*args, **kwargs) + + if to_test_constructor is None: + self.to_test_constructor = lambda measurements=None: MeasurementDefiner(measurements=measurements) + else: + self.to_test_constructor = to_test_constructor + + def test_measurement_windows(self) -> None: + pulse = self.to_test_constructor(measurements=[('mw', 0, 5)]) + with self.assertRaises(KeyError): + pulse.get_measurement_windows(parameters=dict(), measurement_mapping=dict()) + windows = pulse.get_measurement_windows(parameters=dict(), measurement_mapping={'mw': 'asd'}) + self.assertEqual([('asd', 0, 5)], windows) + self.assertEqual(pulse.measurement_declarations, [('mw', 0, 5)]) + + def test_multiple_windows(self): + pulse = self.to_test_constructor(measurements=[('mw', 0, 5), ('H', 'a', 'b')]) + with self.assertRaises(KeyError): + pulse.get_measurement_windows(parameters=dict(), measurement_mapping=dict()) + windows = pulse.get_measurement_windows(parameters=dict(a=0.5, b=1), measurement_mapping={'mw': 'asd', 'H': 'H'}) + self.assertEqual([('asd', 0, 5), ('H', 0.5, 1)], windows) + self.assertEqual(pulse.measurement_declarations, [('mw', 0, 5), ('H', 'a', 'b')]) + + def test_no_measurement_windows(self) -> None: + pulse = self.to_test_constructor() + windows = pulse.get_measurement_windows(dict(), {'mw': 'asd'}) + self.assertEqual([], windows) + self.assertEqual([], pulse.measurement_declarations) + + def test_measurement_windows_with_parameters(self) -> None: + pulse = self.to_test_constructor(measurements=[('mw', 1, '(1+length)/2')]) + parameters = dict(length=100) + windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) + self.assertEqual(windows, [('asd', 1, 101 / 2)]) + self.assertEqual(pulse.measurement_declarations, [('mw', 1, '(1+length)/2')]) + + def test_measurement_windows_invalid(self) -> None: + pulse = self.to_test_constructor(measurements=[('mw', 'a', 'd')]) + measurement_mapping = {'mw': 'mw'} + + with self.assertRaises(ValueError): + pulse.get_measurement_windows(measurement_mapping=measurement_mapping, + parameters=dict(length=10, a=-1, d=3)) + with self.assertRaises(ValueError): + pulse.get_measurement_windows(measurement_mapping=measurement_mapping, + parameters=dict(length=10, a=3, d=-1)) + + +class ParameterConstrainerTest(unittest.TestCase): + def __init__(self, *args, to_test_constructor=None, **kwargs): + super().__init__(*args, **kwargs) + + if to_test_constructor is None: + self.to_test_constructor = lambda parameter_constraints=None:\ + ParameterConstrainer(parameter_constraints=parameter_constraints) + else: + self.to_test_constructor = to_test_constructor + + def test_parameter_constraints(self): + to_test = self.to_test_constructor() + self.assertEqual(to_test.parameter_constraints, []) + + to_test = self.to_test_constructor(['a < b']) + self.assertEqual(to_test.parameter_constraints, [ParameterConstraint('a < b')]) + + to_test = self.to_test_constructor(['a < b', 'c < 1']) + self.assertEqual(to_test.parameter_constraints, [ParameterConstraint('a < b'), ParameterConstraint('c < 1')]) + + def test_validate_parameter_constraints(self): + to_test = self.to_test_constructor() + to_test.validate_parameter_constraints(dict()) + to_test.validate_parameter_constraints(dict(a=1)) + + to_test = self.to_test_constructor(['a < b']) + with self.assertRaises(ParameterNotProvidedException): + to_test.validate_parameter_constraints(dict()) + with self.assertRaises(ParameterConstraintViolation): + to_test.validate_parameter_constraints(dict(a=1, b=0.8)) + to_test.validate_parameter_constraints(dict(a=1, b=2)) + + to_test = self.to_test_constructor(['a < b', 'c < 1']) + with self.assertRaises(ParameterNotProvidedException): + to_test.validate_parameter_constraints(dict(a=1, b=2)) + with self.assertRaises(ParameterNotProvidedException): + to_test.validate_parameter_constraints(dict(c=0.5)) + + with self.assertRaises(ParameterConstraintViolation): + to_test.validate_parameter_constraints(dict(a=1, b=0.8, c=0.5)) + with self.assertRaises(ParameterConstraintViolation): + to_test.validate_parameter_constraints(dict(a=0.5, b=0.8, c=1)) + to_test.validate_parameter_constraints(dict(a=0.5, b=0.8, c=0.1)) + + def test_constrained_parameters(self): + to_test = self.to_test_constructor() + self.assertEqual(to_test.constrained_parameters, set()) + + to_test = self.to_test_constructor(['a < b']) + self.assertEqual(to_test.constrained_parameters, {'a', 'b'}) + + to_test = self.to_test_constructor(['a < b', 'c < 1']) + self.assertEqual(to_test.constrained_parameters, {'a', 'b', 'c'}) \ No newline at end of file diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index 0588fb54a..b19e501e1 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -4,14 +4,14 @@ from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ MissingParameterDeclarationException, UnnecessaryMappingException -from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, MappedParameter, ConstantParameter -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelPulseTemplate, MultiChannelWaveform, MappingTemplate, ChannelMappingException +from qctoolkit.pulses.parameters import ParameterNotProvidedException, MappedParameter, ConstantParameter +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelPulseTemplate, MultiChannelWaveform, MappingTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate from qctoolkit.expressions import Expression from qctoolkit.pulses.instructions import CHANInstruction, EXECInstruction from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform from tests.serialization_dummies import DummySerializer - +from tests.pulses.pulse_template_tests import PulseTemplateStub class MultiChannelWaveformTest(unittest.TestCase): @@ -129,6 +129,128 @@ def test_unsafe_get_subset_for_channels(self): self.assertIs(sub_ab.unsafe_get_subset_for_channels({'A'}), dwf_a) self.assertIs(sub_ab.unsafe_get_subset_for_channels({'B'}), dwf_b) + +class AtomicMultiChannelPulseTemplateTest(unittest.TestCase): + def __init__(self,*args,**kwargs): + super().__init__(*args,**kwargs) + + self.subtemplates = [DummyPulseTemplate(parameter_names={'p1'}, + measurement_names={'m1'}, + defined_channels={'c1'}), + DummyPulseTemplate(parameter_names={'p2'}, + measurement_names={'m2'}, + defined_channels={'c2'}), + DummyPulseTemplate(parameter_names={'p3'}, + measurement_names={'m3'}, + defined_channels={'c3'})] + self.no_param_maps = [{'p1': '1'}, {'p2': '2'}, {'p3': '3'}] + self.param_maps = [{'p1': 'pp1'}, {'p2': 'pp2'}, {'p3': 'pp3'}] + self.chan_maps = [{'c1': 'cc1'}, {'c2': 'cc2'}, {'c3': 'cc3'}] + + def test_init_empty(self) -> None: + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate() + + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate(identifier='foo') + + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate(external_parameters=set()) + + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate(identifier='foo', external_parameters=set()) + + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate(identifier='foo', external_parameters=set(), parameter_constraints=[]) + + def test_non_atomic_subtemplates(self): + non_atomic_pt = PulseTemplateStub(duration='t1', defined_channels={'A'}, parameter_names=set()) + atomic_pt = DummyPulseTemplate(defined_channels={'B'}, duration='t1') + + with self.assertRaises(TypeError): + AtomicMultiChannelPulseTemplate(non_atomic_pt) + + with self.assertRaises(TypeError): + AtomicMultiChannelPulseTemplate(non_atomic_pt, atomic_pt) + + with self.assertRaises(TypeError): + AtomicMultiChannelPulseTemplate(MappingTemplate(non_atomic_pt), atomic_pt) + + with self.assertRaises(TypeError): + AtomicMultiChannelPulseTemplate((non_atomic_pt, {'B': 'C'}), atomic_pt) + + def test_duration(self): + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}), + DummyPulseTemplate(duration='t1', defined_channels={'B'}), + DummyPulseTemplate(duration='t2', defined_channels={'C'})] + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate(*sts) + + with self.assertRaises(ValueError): + AtomicMultiChannelPulseTemplate(sts[0], sts[2]) + template = AtomicMultiChannelPulseTemplate(*sts[:1]) + + self.assertEqual(template.duration, 't1') + + def test_external_parameters(self): + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'})] + constraints = ['a < d'] + template = AtomicMultiChannelPulseTemplate(*sts, + parameter_constraints=constraints, + external_parameters={'a', 'b', 'c', 'd'}) + + with self.assertRaises(MissingParameterDeclarationException): + AtomicMultiChannelPulseTemplate(*sts, + external_parameters={'a', 'c', 'd'}, + parameter_constraints=constraints) + with self.assertRaises(MissingParameterDeclarationException): + AtomicMultiChannelPulseTemplate(*sts, external_parameters={'a', 'b', 'd'}, + parameter_constraints=constraints) + with self.assertRaises(MissingParameterDeclarationException): + AtomicMultiChannelPulseTemplate(*sts, external_parameters={'b', 'c', 'd'}, + parameter_constraints=constraints) + with self.assertRaises(MissingParameterDeclarationException): + AtomicMultiChannelPulseTemplate(*sts, external_parameters={'a', 'c', 'b'}, + parameter_constraints=constraints) + + with self.assertRaises(MissingMappingException): + AtomicMultiChannelPulseTemplate(*sts, external_parameters={'a', 'b', 'c', 'd', 'e'}, + parameter_constraints=constraints) + + self.assertEqual(template.parameter_names, {'a', 'b', 'c', 'd'}) + + def test_mapping_template_pure_conversion(self): + template = AtomicMultiChannelPulseTemplate(*zip(self.subtemplates, self.param_maps, self.chan_maps)) + + for st, pm, cm in zip(template.subtemplates, self.param_maps, self.chan_maps): + self.assertEqual(st.parameter_names, set(pm.values())) + self.assertEqual(st.defined_channels, set(cm.values())) + + def test_mapping_template_mixed_conversion(self): + subtemp_args = [ + (self.subtemplates[0], self.param_maps[0], self.chan_maps[0]), + MappingTemplate(self.subtemplates[1], parameter_mapping=self.param_maps[1], channel_mapping=self.chan_maps[1]), + (self.subtemplates[2], self.param_maps[2], self.chan_maps[2]) + ] + template = AtomicMultiChannelPulseTemplate(*subtemp_args) + + for st, pm, cm in zip(template.subtemplates, self.param_maps, self.chan_maps): + self.assertEqual(st.parameter_names, set(pm.values())) + self.assertEqual(st.defined_channels, set(cm.values())) + + def test_channel_intersection(self): + chan_maps = self.chan_maps.copy() + chan_maps[-1]['c3'] = 'cc1' + with self.assertRaises(ChannelMappingException): + AtomicMultiChannelPulseTemplate(*zip(self.subtemplates, self.param_maps, chan_maps)) + + def test_defined_channels(self): + subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] + template = AtomicMultiChannelPulseTemplate(*subtemp_args) + self.assertEqual(template.defined_channels, {'cc1', 'cc2', 'cc3'}) + + class MultiChannelPulseTemplateTest(unittest.TestCase): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) @@ -143,15 +265,9 @@ def __init__(self,*args,**kwargs): self.param_maps = [{'p1': 'pp1'}, {'p2': 'pp2'}, {'p3': 'pp3'}] self.chan_maps = [{'c1': 'cc1'}, {'c2': 'cc2'}, {'c3': 'cc3'}] - @unittest.skip('Consider forbidding empty multi channel templates') def test_init_empty(self) -> None: - template = MultiChannelPulseTemplate([], {}, identifier='foo') - self.assertEqual('foo', template.identifier) - self.assertFalse(template.parameter_names) - self.assertFalse(template.parameter_declarations) - self.assertTrue(template.is_interruptable) - self.assertFalse(template.requires_stop(dict(), dict())) - self.assertEqual(0, template.num_channels) + with self.assertRaises(ValueError): + MultiChannelPulseTemplate([], {}, identifier='foo') def test_mapping_template_pure_conversion(self): subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] @@ -164,7 +280,7 @@ def test_mapping_template_pure_conversion(self): def test_mapping_template_mixed_conversion(self): subtemp_args = [ (self.subtemplates[0], self.param_maps[0], self.chan_maps[0]), - MappingTemplate(self.subtemplates[1], self.param_maps[1], channel_mapping=self.chan_maps[1]), + MappingTemplate(self.subtemplates[1], parameter_mapping=self.param_maps[1], channel_mapping=self.chan_maps[1]), (self.subtemplates[2], self.param_maps[2], self.chan_maps[2]) ] template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) @@ -209,8 +325,6 @@ def test_requires_stop_false_mapped_parameters(self) -> None: (dummy, dict(foo='rab-5'), {'default': 'B'})], {'bar', 'rab'}) self.assertEqual({'bar', 'rab'}, pulse.parameter_names) - self.assertEqual({ParameterDeclaration('bar'), ParameterDeclaration('rab')}, - pulse.parameter_declarations) parameters = dict(bar=ConstantParameter(-3.6), rab=ConstantParameter(35.26)) self.assertFalse(pulse.requires_stop(parameters, dict())) @@ -221,16 +335,14 @@ def test_requires_stop_true_mapped_parameters(self) -> None: (dummy, dict(foo='rab-5'), {'default': 'B'})], {'bar', 'rab'}) self.assertEqual({'bar', 'rab'}, pulse.parameter_names) - self.assertEqual({ParameterDeclaration('bar'), ParameterDeclaration('rab')}, - pulse.parameter_declarations) parameters = dict(bar=ConstantParameter(-3.6), rab=ConstantParameter(35.26)) self.assertTrue(pulse.requires_stop(parameters, dict())) - def test_build_sequence(self) -> None: - dummy_wf1 = DummyWaveform(duration=2.3) - dummy_wf2 = DummyWaveform(duration=2.3) - dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1) - dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2) + def test_build_sequence_different_duration(self) -> None: + dummy_wf1 = DummyWaveform(duration=2.4, defined_channels={'A'}) + dummy_wf2 = DummyWaveform(duration=2.3, defined_channels={'B'}) + dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1, duration=2.4) + dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2, duration=2.3) sequencer = DummySequencer() pulse = MultiChannelPulseTemplate([dummy1, dummy2], {'bar'}) @@ -259,43 +371,32 @@ def test_build_sequence(self) -> None: self.assertEqual(sequencer.sequencing_stacks[sub_block_ptr.block], [(dummy2, parameters, conditions, measurement_mapping, channel_mapping)]) + def test_build_sequence_same_duration(self) -> None: + dummy_wf1 = DummyWaveform(duration=2.3, defined_channels={'A'}) + dummy_wf2 = DummyWaveform(duration=2.3, defined_channels={'B'}) + dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1, duration=2.3) + dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2, duration=2.3) - @unittest.skip("Replace when/if there is an AtomicPulseTemplate detection.") - def test_integration_table_and_function_template(self) -> None: - from qctoolkit.pulses import TablePulseTemplate, FunctionPulseTemplate, Sequencer, EXECInstruction, STOPInstruction + sequencer = DummySequencer() + pulse = MultiChannelPulseTemplate([dummy1, dummy2], {'bar'}) - table_template = TablePulseTemplate(channels=2) - table_template.add_entry(1, 4, channel=0) - table_template.add_entry('foo', 'bar', channel=0) - table_template.add_entry(10, 0, channel=0) - table_template.add_entry('foo', 2.7, interpolation='linear', channel=1) - table_template.add_entry(9, 'bar', interpolation='linear', channel=1) + parameters = {'bar': ConstantParameter(3)} + measurement_mapping = {} + channel_mapping = {'A': 'A', 'B': 'B'} + instruction_block = DummyInstructionBlock() + conditions = {} - function_template = FunctionPulseTemplate('sin(t)', '10') + pulse.build_sequence(sequencer, parameters=parameters, + conditions=conditions, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping, + instruction_block=instruction_block) - template = MultiChannelPulseTemplate( - [(function_template, dict(), [1]), - (table_template, dict(foo='5', bar='2 * hugo'), [2, 0])], - {'hugo'} - ) + self.assertEqual(len(instruction_block), 2) + self.assertIsInstance(instruction_block[0], EXECInstruction) + self.assertIsInstance(instruction_block[0].waveform, MultiChannelWaveform) - sample_times = numpy.linspace(98.5, 103.5, num=11) - function_template_samples = function_template.build_waveform(dict()).sample(sample_times) - table_template_samples = table_template.build_waveform(dict(foo=ConstantParameter(5), bar=ConstantParameter(2*(-1.3)))).sample(sample_times) - - template_waveform = template.build_waveform(dict(hugo=ConstantParameter(-1.3))) - template_samples = template_waveform.sample(sample_times) - - self.assertTrue(numpy.all(table_template_samples[0] == template_samples[2])) - self.assertTrue(numpy.all(table_template_samples[1] == template_samples[0])) - self.assertTrue(numpy.all(function_template_samples[0] == template_samples[1])) - - sequencer = Sequencer() - sequencer.push(template, parameters=dict(hugo=-1.3), conditions=dict()) - instructions = sequencer.build() - self.assertEqual(2, len(instructions)) - self.assertIsInstance(instructions[0], EXECInstruction) - self.assertIsInstance(instructions[1], STOPInstruction) + self.assertEqual(instruction_block[0].waveform.compare_key, (dummy_wf1.compare_key, dummy_wf2.compare_key)) class MultiChannelPulseTemplateSerializationTests(unittest.TestCase): diff --git a/tests/pulses/parameters_tests.py b/tests/pulses/parameters_tests.py index 7e08688aa..d62e9f17b 100644 --- a/tests/pulses/parameters_tests.py +++ b/tests/pulses/parameters_tests.py @@ -2,7 +2,8 @@ from typing import Union from qctoolkit.expressions import Expression -from qctoolkit.pulses.parameters import ConstantParameter, MappedParameter, ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException, ParameterConstraint, ParameterConstraintViolation +from qctoolkit.pulses.parameters import ConstantParameter, MappedParameter, ParameterNotProvidedException,\ + ParameterConstraint, ParameterConstraintViolation from tests.serialization_dummies import DummySerializer from tests.pulses.sequencing_dummies import DummyParameter @@ -125,417 +126,13 @@ def test_no_relation(self): ParameterConstraint('1 < 2') - - -class ParameterDeclarationTest(unittest.TestCase): - - def __test_valid_values(self, **kwargs): - expected_values = {'min': float('-inf'), 'max': float('+inf'), 'default': None} - for k in kwargs: - expected_values[k] = kwargs[k] - - expected_values['absolute_min'] = expected_values['min'] - expected_values['absolute_max'] = expected_values['max'] - - if isinstance(expected_values['absolute_min'], ParameterDeclaration): - expected_values['absolute_min'] = expected_values['absolute_min'].absolute_min_value - if isinstance(expected_values['absolute_max'], ParameterDeclaration): - expected_values['absolute_max'] = expected_values['absolute_max'].absolute_max_value - - decl = ParameterDeclaration('test', **kwargs) - - self.assertEqual('test', decl.name) - self.assertEqual(expected_values['min'], decl.min_value) - self.assertEqual(expected_values['max'], decl.max_value) - self.assertEqual(expected_values['default'], decl.default_value) - self.assertEqual(expected_values['absolute_min'], decl.absolute_min_value) - self.assertEqual(expected_values['absolute_max'], decl.absolute_max_value) - - decl = ParameterDeclaration('test', default=expected_values['default']) - if 'min' in kwargs: - decl.min_value = kwargs['min'] - if 'max' in kwargs: - decl.max_value = kwargs['max'] - - self.assertEqual(expected_values['min'], decl.min_value) - self.assertEqual(expected_values['max'], decl.max_value) - self.assertEqual(expected_values['default'], decl.default_value) - self.assertEqual(expected_values['absolute_min'], decl.absolute_min_value) - self.assertEqual(expected_values['absolute_max'], decl.absolute_max_value) - - - def __max_assignment(self, decl: ParameterDeclaration, value: Union[ParameterDeclaration, float]) -> None: - decl.max_value = value - - def __min_assignment(self, decl: ParameterDeclaration, value: Union[ParameterDeclaration, float]) -> None: - decl.min_value = value - - def test_init_constant_values(self) -> None: - self.__test_valid_values() - self.__test_valid_values(min=-0.1) - self.__test_valid_values(max=7.5) - self.__test_valid_values(default=2.1) - self.__test_valid_values(min=-0.1, max=3.5) - self.__test_valid_values(min=-0.1, max=-0.1) - self.__test_valid_values(min=-0.1, default=3.5) - self.__test_valid_values(min=-0.1, default=-0.1) - self.__test_valid_values(max=3.5, default=3.5) - self.__test_valid_values(max=3.5, default=-0.1) - self.__test_valid_values(min=-0.1, default=2.4, max=3.5) - self.__test_valid_values(min=-0.1, default=-0.1, max=3.5) - self.__test_valid_values(min=-0.1, default=3.5, max=3.5) - self.__test_valid_values(min=1.7, default=1.7, max=1.7) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=-0.1, max=-0.2) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=-0.1, default=-0.2) - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=-0.2, default=-0.1) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=-0.2, default=-0.3, max=0.1) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=-0.2, default=-0.3, max=-0.4) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=-0.2, default=-0.1, max=-0.4) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=-0.2, default=0.5, max=0.1) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=1.7, default=0.5, max=0.1) - decl = ParameterDeclaration('test', min=-0.1) - with self.assertRaises(ValueError): - self.__max_assignment(decl, -0.2) - decl = ParameterDeclaration('test', min=-0.2, default=-0.1) - with self.assertRaises(ValueError): - self.__max_assignment(decl, -0.4) - decl = ParameterDeclaration('test', min=-0.2, default=0.5) - with self.assertRaises(ValueError): - self.__max_assignment(decl, 0.1) - decl = ParameterDeclaration('test', max=-0.1) - with self.assertRaises(ValueError): - self.__min_assignment(decl, 0.2) - decl = ParameterDeclaration('test', max=0.2, default=-0.1) - with self.assertRaises(ValueError): - self.__min_assignment(decl, 0.4) - decl = ParameterDeclaration('test', max=1.1, default=0.5) - with self.assertRaises(ValueError): - self.__min_assignment(decl, 0.7) - - def test_init_min_reference(self) -> None: - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1)) - - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), max=3.5) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), max=-0.1) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=3.5) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.1) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-17.3) - - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=3.5, max=3.5) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.1, max=3.5) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.1, max=-0.1) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2, max=3.5) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2, max=-0.1) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-17.3, max=3.5) - self.__test_valid_values(min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-17.3, max=-0.1) - - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('foo', min=-17.3, max=-0.1), max=-0.2) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-20.3) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-20.3, max=0.1) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2, max=-0.4) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-20.3, max=-0.4) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=0.3, max=0.2) - - def test_init_max_reference(self) -> None: - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1)) - - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), min=-22.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), min=-17.3) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-22.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-17.3) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.1) - - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-22.2, min=-22.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-17.3, min=-22.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-17.3, min=-17.3) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2, min=-22.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2, min=-17.3) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.1, min=-22.2) - self.__test_valid_values(max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.1, min=-17.3) - - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=ParameterDeclaration('foo', min=-17.3, max=-0.1), min=-0.2) - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=0.01) - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-0.01, min=-20.9) - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=-4.2, min=-5.2) - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=1.2, min=-0.4) - with self.assertRaises(ValueError): - ParameterDeclaration('test', max=ParameterDeclaration('foo', min=-17.3, max=-0.1), default=1.2, min=3.9) - - def test_init_min_max_reference(self) -> None: - self.__test_valid_values(min=ParameterDeclaration('fooi', max=3.5), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', max=5.7), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', max=13.2), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', max=3.5), max=ParameterDeclaration('fooa', max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', max=3.5), max=ParameterDeclaration('fooa', max=3.5)) - - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', min=4.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=5.7), max=ParameterDeclaration('fooa', min=4.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3), max=ParameterDeclaration('fooa', min=4.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', min=0.3)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3), max=ParameterDeclaration('fooa', min=0.3)) - - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=5.7), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=13.2), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', min=0.3, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', min=0.3, max=3.5)) - - - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=0.3, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=2.1, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=3.5, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=4.1, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=4.2, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=5.7, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=13.2, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', min=0.2, max=13.2)) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('fooi', min=0.3, max=13.5), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3), max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3, max=3.5), max=ParameterDeclaration('fooa', max=13.2)) - self.__test_valid_values(min=ParameterDeclaration('fooi', min=0.3), max=ParameterDeclaration('fooa', max=13.2)) - - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=-0.2, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - with self.assertRaises(ValueError): - ParameterDeclaration('test', min=ParameterDeclaration('fooi', min=0.3, max=3.5), default=22.1, max=ParameterDeclaration('fooa', min=4.2, max=13.2)) - - def test_get_value(self) -> None: - decl = ParameterDeclaration('foo') - foo_param = ConstantParameter(2.1) - self.assertEqual(foo_param.get_value(), decl.get_value({'foo': foo_param})) - with self.assertRaises(ParameterNotProvidedException): - decl.get_value(dict()) - - decl = ParameterDeclaration('foo', default=2.7) - self.assertEqual(decl.default_value, decl.get_value({})) - - decl = ParameterDeclaration('foo', min=1.3) - self.assertEqual(1.4, decl.get_value({'foo': ConstantParameter(1.4)})) - self.assertEqual(1.3, decl.get_value({'foo': ConstantParameter(1.3)})) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'foo': ConstantParameter(1.1)}) - - decl = ParameterDeclaration('foo', max=2.3) - self.assertTrue(1.4, decl.get_value({'foo': ConstantParameter(1.4)})) - self.assertTrue(2.3, decl.get_value({'foo': ConstantParameter(2.3)})) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'foo': ConstantParameter(3.1)}) - - decl = ParameterDeclaration('foo', min=1.3, max=2.3) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'foo': ConstantParameter(0.9)}) - self.assertEqual(1.3, decl.get_value({'foo': ConstantParameter(1.3)})) - self.assertEqual(1.4, decl.get_value({'foo': ConstantParameter(1.4)})) - self.assertEqual(2.3, decl.get_value({'foo': ConstantParameter(2.3)})) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'foo': ConstantParameter(3.1)}) - - min_decl = ParameterDeclaration('min', min=1.2, max=2.3) - max_decl = ParameterDeclaration('max', min=1.2, max=5.1) - - min_param = ConstantParameter(1.3) - max_param = ConstantParameter(2.3) - - decl = ParameterDeclaration('foo', min=min_decl, max=max_decl) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(0.9)}) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.2)}) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.25)}) - self.assertEqual(1.3, decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.3)})) - self.assertEqual(1.7, decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(1.7)})) - self.assertEqual(2.3, decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(2.3)})) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(3.5)}) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(5.1)}) - with self.assertRaises(ParameterValueIllegalException): - decl.get_value({'min': min_param, 'max': max_param, 'foo': ConstantParameter(17.2)}) - - def __assign_min_value(self, left_value: ParameterDeclaration, right_value: ParameterDeclaration) -> None: - left_value.min_value = right_value - - def test_internal_set_value_exception_branches(self) -> None: - foo = ParameterDeclaration('foo', min=2.1, max=2.6) - bar = ParameterDeclaration('bar', min=foo, max=foo) - with self.assertRaises(ValueError): - ParameterDeclaration('foobar', min=3.1, max=bar) - - bar = ParameterDeclaration('bar', min=foo, max=foo) - foobar = ParameterDeclaration('foobar', max=1.1) - with self.assertRaises(ValueError): - self.__assign_min_value(foobar, bar) - - def test_is_parameter_valid_no_bounds(self) -> None: - decl = ParameterDeclaration('foo') - param = ConstantParameter(2.4) - self.assertTrue(decl.is_parameter_valid(param)) - - def test_is_parameter_valid_min_bound(self) -> None: - decl = ParameterDeclaration('foobar', min=-0.1) - params = [(False, -0.5), (True, -0.1), (True, 0), (True, 17.3)] - for expected, param in params: - self.assertEqual(expected, decl.is_parameter_valid(param)) - - def test_is_parameter_valid_max_bound(self) -> None: - decl = ParameterDeclaration('foobar', max=-0.1) - params = [(True, -0.5), (True, -0.1), (False, 0), (False, 17.3)] - for expected, param in params: - self.assertEqual(expected, decl.is_parameter_valid(param)) - - def test_is_parameter_valid_min_max_bound(self) -> None: - decl = ParameterDeclaration('foobar', min=-0.1, max=13.2) - params = [(False, -0.5), (True, -0.1), (True, 0), (True, 7.9), (True, 13.2), (False, 17.3)] - for expected, param in params: - self.assertEqual(expected, decl.is_parameter_valid(param)) - - def test_str_and_repr(self) -> None: - decl = ParameterDeclaration('foo', min=0.1) - self.assertEqual("{} 'foo', range (0.1, inf), default None".format(type(decl)), str(decl)) - self.assertEqual("<{} 'foo', range (0.1, inf), default None>".format(type(decl)), repr(decl)) - min_decl = ParameterDeclaration('minifoo', min=0.2) - max_decl = ParameterDeclaration('maxifoo', max=1.1) - decl = ParameterDeclaration('foo', min=min_decl) - self.assertEqual("{} 'foo', range (Parameter 'minifoo' (min 0.2), inf), default None".format(type(decl)), str(decl)) - self.assertEqual("<{} 'foo', range (Parameter 'minifoo' (min 0.2), inf), default None>".format(type(decl)), repr(decl)) - decl = ParameterDeclaration('foo', max=max_decl) - self.assertEqual("{} 'foo', range (-inf, Parameter 'maxifoo' (max 1.1)), default None".format(type(decl)), str(decl)) - self.assertEqual("<{} 'foo', range (-inf, Parameter 'maxifoo' (max 1.1)), default None>".format(type(decl)), repr(decl)) - decl = ParameterDeclaration('foo', min=min_decl, max=max_decl) - self.assertEqual("{} 'foo', range (Parameter 'minifoo' (min 0.2), Parameter 'maxifoo' (max 1.1)), default None".format(type(decl)), str(decl)) - self.assertEqual("<{} 'foo', range (Parameter 'minifoo' (min 0.2), Parameter 'maxifoo' (max 1.1)), default None>".format(type(decl)), repr(decl)) - - -class ParameterDeclarationSerializationTests(unittest.TestCase): - - def setUp(self) -> None: - self.serializer = DummySerializer() - self.declaration = ParameterDeclaration('foo') - self.expected_data = dict(name='foo', type=self.serializer.get_type_identifier(self.declaration)) - - def test_get_serialization_data_all_default(self) -> None: - self.expected_data['min_value'] = float('-inf') - self.expected_data['max_value'] = float('+inf') - self.expected_data['default_value'] = None - self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer)) - - def test_get_serialization_data_all_floats(self) -> None: - self.declaration = ParameterDeclaration('foo', min=-3.1, max=4.3, default=0.2) - self.expected_data['min_value'] = -3.1 - self.expected_data['max_value'] = 4.3 - self.expected_data['default_value'] = 0.2 - self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer)) - - def test_get_serialization_data_min_max_references(self) -> None: - bar_min = ParameterDeclaration('bar_min') - bar_max = ParameterDeclaration('bar_max') - self.declaration.min_value = bar_min - self.declaration.max_value = bar_max - self.expected_data['min_value'] = 'bar_min' - self.expected_data['max_value'] = 'bar_max' - self.expected_data['default_value'] = None - self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer)) - - def test_deserialize_all_default(self) -> None: - data = dict(min_value=float('-inf'), max_value=float('+inf'), default_value=None, name='foo') - declaration = ParameterDeclaration.deserialize(self.serializer, **data) - self.assertEqual(data['min_value'], declaration.min_value) - self.assertEqual(data['max_value'], declaration.max_value) - self.assertEqual(data['default_value'], declaration.default_value) - self.assertEqual(data['name'], declaration.name) - self.assertIsNone(declaration.identifier) - - def test_deserialize_all_floats(self) -> None: - data = dict(min_value=33.3, max_value=44, default_value=41.1, name='foo') - declaration = ParameterDeclaration.deserialize(self.serializer, **data) - self.assertEqual(data['min_value'], declaration.min_value) - self.assertEqual(data['max_value'], declaration.max_value) - self.assertEqual(data['default_value'], declaration.default_value) - self.assertEqual(data['name'], declaration.name) - self.assertIsNone(declaration.identifier) - - def test_deserialize_min_max_references(self) -> None: - data = dict(min_value='bar_min', max_value='bar_max', default_value=-23.5, name='foo') - declaration = ParameterDeclaration.deserialize(self.serializer, **data) - self.assertEqual(float('-inf'), declaration.min_value) - self.assertEqual(float('+inf'), declaration.max_value) - self.assertEqual(data['default_value'], declaration.default_value) - self.assertEqual(data['name'], declaration.name) - self.assertIsNone(declaration.identifier) - - class ParameterNotProvidedExceptionTests(unittest.TestCase): def test(self) -> None: exc = ParameterNotProvidedException('foo') self.assertEqual("No value was provided for parameter 'foo' and no default value was specified.", str(exc)) - -# class ImmutableParameterDeclarationTest(unittest.TestCase): -# -# def test_init(self) -> None: -# param_decl = ParameterDeclaration('hugo', min=13.2, max=15.3, default=None) -# immutable = ImmutableParameterDeclaration(param_decl) -# self.assertEqual(param_decl, immutable) -# self.assertEqual(param_decl.name, immutable.name) -# self.assertEqual(param_decl.min_value, immutable.min_value) -# self.assertEqual(param_decl.absolute_min_value, immutable.absolute_min_value) -# self.assertEqual(param_decl.max_value, immutable.max_value) -# self.assertEqual(param_decl.absolute_max_value, immutable.absolute_max_value) -# self.assertEqual(param_decl.default_value, immutable.default_value) -# -# def test_reference_values(self) -> None: -# min_decl = ParameterDeclaration('min', min=-0.1, max=34.7) -# max_decl = ParameterDeclaration('max', min=23.1) -# param_decl = ParameterDeclaration('foo', min=min_decl, max=max_decl, default=2.4) -# immutable = ImmutableParameterDeclaration(param_decl) -# -# self.assertEqual(param_decl, immutable) -# self.assertEqual(param_decl.name, immutable.name) -# self.assertEqual(param_decl.min_value, immutable.min_value) -# self.assertEqual(param_decl.absolute_min_value, immutable.absolute_min_value) -# self.assertEqual(param_decl.max_value, immutable.max_value) -# self.assertEqual(param_decl.absolute_max_value, immutable.absolute_max_value) -# self.assertEqual(param_decl.default_value, immutable.default_value) -# self.assertIsInstance(immutable.min_value, ImmutableParameterDeclaration) -# self.assertIsInstance(immutable.max_value, ImmutableParameterDeclaration) -# -# def test_immutability(self) -> None: -# param_decl = ParameterDeclaration('hugo', min=13.2, max=15.3, default=None) -# immutable = ImmutableParameterDeclaration(param_decl) -# -# self.assertRaises(Exception, immutable.min_value, 2.1) -# self.assertEqual(13.2, immutable.min_value) -# self.assertEqual(13.2, param_decl.min_value) -# -# self.assertRaises(Exception, immutable.max_value, 14.1) -# self.assertEqual(15.3, immutable.max_value) -# self.assertEqual(15.3, param_decl.max_value) + if __name__ == "__main__": unittest.main(verbosity=2) - \ No newline at end of file + diff --git a/tests/pulses/pulse_template_parameter_mapping_tests.py b/tests/pulses/pulse_template_parameter_mapping_tests.py index c2e6d2071..254151cb0 100644 --- a/tests/pulses/pulse_template_parameter_mapping_tests.py +++ b/tests/pulses/pulse_template_parameter_mapping_tests.py @@ -1,10 +1,12 @@ import unittest +import itertools from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ - UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate + UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate,\ + AmbiguousMappingException, MappingCollisionException from qctoolkit.expressions import Expression from qctoolkit.pulses.parameters import ParameterNotProvidedException -from qctoolkit.pulses.parameters import ConstantParameter +from qctoolkit.pulses.parameters import ConstantParameter, ParameterConstraintViolation from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummySequencer, DummyInstructionBlock @@ -13,8 +15,8 @@ class MappingTemplateTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - def test_init(self): - template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + def test_init_exceptions(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, defined_channels={'A'}, measurement_names={'B'}) parameter_mapping = {'foo': 't*k', 'bar': 't*l'} with self.assertRaises(MissingMappingException): @@ -22,13 +24,63 @@ def test_init(self): with self.assertRaises(MissingMappingException): MappingTemplate(template, parameter_mapping={'bar': 'kneipe'}) with self.assertRaises(UnnecessaryMappingException): - MappingTemplate(template, dict(**parameter_mapping, foobar='asd')) - MappingTemplate(template, parameter_mapping=parameter_mapping) + MappingTemplate(template, parameter_mapping=dict(**parameter_mapping, foobar='asd')) with self.assertRaises(UnnecessaryMappingException): - MappingTemplate(template, parameter_mapping, measurement_mapping=dict(a='b')) + MappingTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=dict(a='b')) with self.assertRaises(UnnecessaryMappingException): - MappingTemplate(template, parameter_mapping, channel_mapping=dict(a='b')) + MappingTemplate(template, parameter_mapping=parameter_mapping, channel_mapping=dict(a='b')) + + with self.assertRaises(TypeError): + MappingTemplate(template, parameter_mapping) + + MappingTemplate(template, parameter_mapping=parameter_mapping) + + def test_from_tuple_exceptions(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, + measurement_names={'foo', 'foobar'}, + defined_channels={'bar', 'foobar'}) + with self.assertRaises(AmbiguousMappingException): + MappingTemplate.from_tuple((template, {'foo': 'foo'})) + with self.assertRaises(AmbiguousMappingException): + MappingTemplate.from_tuple((template, {'bar': 'bar'})) + with self.assertRaises(AmbiguousMappingException): + MappingTemplate.from_tuple((template, {'foobar': 'foobar'})) + + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + with self.assertRaises(MappingCollisionException): + MappingTemplate.from_tuple((template, {'foo': '1', 'bar': 2}, {'foo': '1', 'bar': 4})) + + def test_from_tuple(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, + measurement_names={'m1', 'm2'}, + defined_channels={'c1', 'c2'}) + + def test_mapping_permutations(template: DummyPulseTemplate, + pmap, mmap, cmap): + direct = MappingTemplate(template, + parameter_mapping=pmap, + measurement_mapping=mmap, + channel_mapping=cmap) + + mappings = [m for m in [pmap, mmap, cmap] if m is not None] + + for current_mapping_order in itertools.permutations(mappings): + mapper = MappingTemplate.from_tuple((template, *current_mapping_order)) + self.assertEqual(mapper.measurement_mapping, direct.measurement_mapping) + self.assertEqual(mapper.channel_mapping, direct.channel_mapping) + self.assertEqual(mapper.parameter_mapping, direct.parameter_mapping) + + test_mapping_permutations(template, {'foo': 1, 'bar': 2}, {'m1': 'n1', 'm2': 'n2'}, {'c1': 'd1', 'c2': 'd2'}) + test_mapping_permutations(template, {'foo': 1, 'bar': 2}, {'m1': 'n1'}, {'c1': 'd1', 'c2': 'd2'}) + test_mapping_permutations(template, {'foo': 1, 'bar': 2}, None, {'c1': 'd1', 'c2': 'd2'}) + test_mapping_permutations(template, {'foo': 1, 'bar': 2}, {'m1': 'n1', 'm2': 'n2'}, {'c1': 'd1'}) + test_mapping_permutations(template, {'foo': 1, 'bar': 2}, {'m1': 'n1', 'm2': 'n2'}, None) + test_mapping_permutations(template, None, {'m1': 'n1', 'm2': 'n2'}, {'c1': 'd1', 'c2': 'd2'}) + test_mapping_permutations(template, None, {'m1': 'n1'}, {'c1': 'd1', 'c2': 'd2'}) + test_mapping_permutations(template, None, None, {'c1': 'd1', 'c2': 'd2'}) + test_mapping_permutations(template, None, {'m1': 'n1', 'm2': 'n2'}, {'c1': 'd1'}) + test_mapping_permutations(template, None, {'m1': 'n1', 'm2': 'n2'}, None) def test_external_params(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) @@ -36,6 +88,15 @@ def test_external_params(self): external_params = {'t', 'l', 'k'} self.assertEqual(st.parameter_names, external_params) + def test_constrained(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}, parameter_constraints=['t < m']) + external_params = {'t', 'l', 'k', 'm'} + self.assertEqual(st.parameter_names, external_params) + + with self.assertRaises(ParameterConstraintViolation): + st.map_parameters(dict(t=1, l=2, k=3, m=0)) + def test_map_parameters(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) @@ -50,7 +111,7 @@ def test_map_parameters(self): def test_get_updated_channel_mapping(self): template = DummyPulseTemplate(defined_channels={'foo', 'bar'}) - st = MappingTemplate(template, {}, channel_mapping={'bar': 'kneipe'}) + st = MappingTemplate(template, channel_mapping={'bar': 'kneipe'}) with self.assertRaises(KeyError): st.get_updated_channel_mapping(dict()) self.assertEqual(st.get_updated_channel_mapping({'kneipe': 'meas1', 'foo': 'meas2', 'troet': 'meas3'}), @@ -58,18 +119,18 @@ def test_get_updated_channel_mapping(self): def test_measurement_names(self): template = DummyPulseTemplate(measurement_names={'foo', 'bar'}) - st = MappingTemplate(template, {}, measurement_mapping={'foo': 'froop', 'bar': 'kneipe'}) + st = MappingTemplate(template, measurement_mapping={'foo': 'froop', 'bar': 'kneipe'}) self.assertEqual( st.measurement_names, {'froop','kneipe'} ) def test_defined_channels(self): mapping = {'asd': 'A', 'fgh': 'B'} template = DummyPulseTemplate(defined_channels=set(mapping.keys())) - st = MappingTemplate(template, {}, channel_mapping=mapping) + st = MappingTemplate(template, channel_mapping=mapping) self.assertEqual(st.defined_channels, set(mapping.values())) def test_get_updated_measurement_mapping(self): template = DummyPulseTemplate(measurement_names={'foo', 'bar'}) - st = MappingTemplate(template, {}, measurement_mapping={'bar': 'kneipe'}) + st = MappingTemplate(template, measurement_mapping={'bar': 'kneipe'}) with self.assertRaises(KeyError): st.get_updated_measurement_mapping(dict()) self.assertEqual(st.get_updated_measurement_mapping({'kneipe': 'meas1', 'foo': 'meas2', 'troet': 'meas3'}), @@ -81,7 +142,7 @@ def test_build_sequence(self): template = DummyPulseTemplate(measurement_names=set(measurement_mapping.keys()), parameter_names=set(parameter_mapping.keys())) - st = MappingTemplate(template, parameter_mapping, measurement_mapping=measurement_mapping) + st = MappingTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=measurement_mapping) sequencer = DummySequencer() block = DummyInstructionBlock() pre_parameters = {'k': ConstantParameter(5)} diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index 11d46646d..6ea15ecd3 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -5,23 +5,86 @@ from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.expressions import Expression -from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration, PulseTemplate from qctoolkit.pulses.instructions import Waveform, EXECInstruction -from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration +from qctoolkit.pulses.parameters import Parameter from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from tests.pulses.sequencing_dummies import DummyWaveform, DummySequencer, DummyInstructionBlock -class AtomicPulseTemplateStub(AtomicPulseTemplate): +class PulseTemplateStub(PulseTemplate): + def __init__(self, identifier=None, + defined_channels=None, + duration=None, + parameter_names=None): + super().__init__(identifier=identifier) + + self._defined_channels = defined_channels + self._duration = duration + self._parameter_names = parameter_names + + @property + def defined_channels(self) -> Set['ChannelID']: + if self._defined_channels: + return self._defined_channels + else: + raise NotImplementedError() + + @property + def parameter_names(self) -> Set[str]: + if self._parameter_names is None: + raise NotImplementedError() + return self._parameter_names + + def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: + raise NotImplementedError() + + @staticmethod + def deserialize(serializer: 'Serializer', **kwargs) -> 'AtomicPulseTemplateStub': + raise NotImplementedError() + + @property + def duration(self) -> Expression: + if self._duration is None: + raise NotImplementedError() + return self._duration + + def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: + raise NotImplementedError() + @staticmethod + def deserialize(serializer: 'Serializer', **kwargs) -> 'AtomicPulseTemplateStub': + raise NotImplementedError() + + def build_sequence(self, + sequencer: "Sequencer", + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition'], + measurement_mapping: Dict[str, str], + channel_mapping: Dict['ChannelID', 'ChannelID'], + instruction_block: 'InstructionBlock'): + raise NotImplementedError() + + def is_interruptable(self): + raise NotImplementedError() + + def measurement_names(self): + raise NotImplementedError() + + def requires_stop(self, + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition']): + raise NotImplementedError() + + +class AtomicPulseTemplateStub(AtomicPulseTemplate): def is_interruptable(self) -> bool: return super().is_interruptable() def __init__(self, *, waveform: Waveform=None, duration: Expression=None, - measurements: List[MeasurementDeclaration] = [], identifier: Optional[str]=None) -> None: - super().__init__(identifier=identifier, measurements=measurements) + super().__init__(identifier=identifier) self.waveform = waveform self._duration = duration @@ -37,10 +100,6 @@ def requires_stop(self, def defined_channels(self) -> Set['ChannelID']: raise NotImplementedError() - @property - def parameter_declarations(self) -> Set[ParameterDeclaration]: - raise NotImplementedError() - @property def parameter_names(self) -> Set[str]: raise NotImplementedError() @@ -48,6 +107,10 @@ def parameter_names(self) -> Set[str]: def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: raise NotImplementedError() + @property + def measurement_names(self): + raise NotImplementedError() + @staticmethod def deserialize(serializer: 'Serializer', **kwargs) -> 'AtomicPulseTemplateStub': raise NotImplementedError() @@ -82,70 +145,10 @@ def test_build_sequence(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() - template = AtomicPulseTemplateStub(waveform=wf, measurements=measurement_windows) + template = AtomicPulseTemplateStub(waveform=wf) template.build_sequence(sequencer, {}, {}, measurement_mapping={}, channel_mapping={}, instruction_block=block) self.assertEqual(len(block.instructions), 1) self.assertIsInstance(block.instructions[0], EXECInstruction) self.assertEqual(block.instructions[0].waveform.defined_channels, {'A'}) self.assertEqual(list(block.instructions[0].waveform.get_measurement_windows()), [('M', 0, 5)]) - def test_measurement_windows(self) -> None: - pulse = AtomicPulseTemplateStub(duration=Expression(5), - measurements=[('mw', 0, 5)]) - with self.assertRaises(KeyError): - pulse.get_measurement_windows(parameters=dict(), measurement_mapping=dict()) - windows = pulse.get_measurement_windows(parameters=dict(), measurement_mapping={'mw': 'asd'}) - self.assertEqual([('asd', 0, 5)], windows) - self.assertEqual(pulse.measurement_declarations, [('mw', 0, 5)]) - - def test_no_measurement_windows(self) -> None: - pulse = AtomicPulseTemplateStub(duration=Expression(4)) - windows = pulse.get_measurement_windows(dict(), {'mw': 'asd'}) - self.assertEqual([], windows) - self.assertEqual([], pulse.measurement_declarations) - - def test_measurement_windows_with_parameters(self) -> None: - pulse = AtomicPulseTemplateStub(duration=Expression('length'), - measurements=[('mw', 1, '(1+length)/2')]) - parameters = dict(length=100) - windows = pulse.get_measurement_windows(parameters, measurement_mapping={'mw': 'asd'}) - self.assertEqual(windows, [('asd', 1, 101 / 2)]) - self.assertEqual(pulse.measurement_declarations, [('mw', 1, '(1+length)/2')]) - - @unittest.skip('Move to AtomicPulseTemplate test') - def test_multiple_measurement_windows(self) -> None: - pulse = AtomicPulseTemplateStub(duration=Expression('length'), - measurements=[('A', 0, '(1+length)/2'), - ('A', 1, 3), - ('B', 'begin', 2)]) - - parameters = dict(length=5, begin=1) - measurement_mapping = dict(A='A', B='C') - windows = pulse.get_measurement_windows(parameters=parameters, - measurement_mapping=measurement_mapping) - expected = [('A', 0, 3), ('A', 1, 3), ('C', 1, 2)] - self.assertEqual(sorted(windows), sorted(expected)) - - expected = [('A', 0, '(1+length)/2'), - ('A', 1, 3), - ('B', 'begin', 2)] - self.assertEqual(pulse.measurement_declarations, - expected) - - def test_measurement_windows_multi_out_of_pulse(self) -> None: - pulse = AtomicPulseTemplateStub(duration=Expression('length'), - measurements=[('mw', 'a', 'd')]) - measurement_mapping = {'mw': 'mw'} - - with self.assertRaises(ValueError): - pulse.get_measurement_windows(measurement_mapping=measurement_mapping, - parameters=dict(length=10, a=-1, d=3)) - with self.assertRaises(ValueError): - pulse.get_measurement_windows(measurement_mapping=measurement_mapping, - parameters=dict(length=10, a=5, d=30)) - with self.assertRaises(ValueError): - pulse.get_measurement_windows(measurement_mapping=measurement_mapping, - parameters=dict(length=10, a=11, d=3)) - with self.assertRaises(ValueError): - pulse.get_measurement_windows(measurement_mapping=measurement_mapping, - parameters=dict(length=10, a=3, d=-1)) diff --git a/tests/pulses/repetition_pulse_template_tests.py b/tests/pulses/repetition_pulse_template_tests.py index 6e67438c0..baf7d2707 100644 --- a/tests/pulses/repetition_pulse_template_tests.py +++ b/tests/pulses/repetition_pulse_template_tests.py @@ -3,7 +3,8 @@ import numpy as np from qctoolkit.pulses.repetition_pulse_template import RepetitionPulseTemplate,ParameterNotIntegerException, RepetitionWaveform -from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException +from qctoolkit.pulses.parameters import ParameterNotProvidedException, ParameterConstraintViolation, ConstantParameter, \ + ParameterConstraint from qctoolkit.pulses.instructions import REPJInstruction, InstructionPointer from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummySequencer, DummyInstructionBlock, DummyParameter,\ @@ -49,7 +50,7 @@ def test_init(self) -> None: self.assertEqual(repetition_count, t.repetition_count) self.assertEqual(body, t.body) - repetition_count = ParameterDeclaration('foo') + repetition_count = 'foo' t = RepetitionPulseTemplate(body, repetition_count) self.assertEqual(repetition_count, t.repetition_count) self.assertEqual(body, t.body) @@ -58,11 +59,9 @@ def test_parameter_names_and_declarations(self) -> None: body = DummyPulseTemplate() t = RepetitionPulseTemplate(body, 5) self.assertEqual(body.parameter_names, t.parameter_names) - self.assertEqual(body.parameter_declarations, t.parameter_declarations) body.parameter_names_ = {'foo', 't', 'bar'} self.assertEqual(body.parameter_names, t.parameter_names) - self.assertEqual(body.parameter_declarations, t.parameter_declarations) @unittest.skip('is interruptable not implemented for loops') def test_is_interruptable(self) -> None: @@ -77,7 +76,7 @@ def test_str(self) -> None: body = DummyPulseTemplate() t = RepetitionPulseTemplate(body, 9) self.assertIsInstance(str(t), str) - t = RepetitionPulseTemplate(body, ParameterDeclaration('foo')) + t = RepetitionPulseTemplate(body, 'foo') self.assertIsInstance(str(t), str) @@ -92,7 +91,7 @@ def test_requires_stop_constant(self) -> None: def test_requires_stop_declaration(self) -> None: body = DummyPulseTemplate(requires_stop=False) - t = RepetitionPulseTemplate(body, ParameterDeclaration('foo')) + t = RepetitionPulseTemplate(body, 'foo') parameter = DummyParameter() parameters = dict(foo=parameter) @@ -109,8 +108,8 @@ def test_requires_stop_declaration(self) -> None: def setUp(self) -> None: self.body = DummyPulseTemplate() - self.repetitions = ParameterDeclaration('foo', max=5) - self.template = RepetitionPulseTemplate(self.body, self.repetitions) + self.repetitions = 'foo' + self.template = RepetitionPulseTemplate(self.body, self.repetitions, parameter_constraints=['foo<9']) self.sequencer = DummySequencer() self.block = DummyInstructionBlock() @@ -118,7 +117,7 @@ def test_build_sequence_constant(self) -> None: repetitions = 3 t = RepetitionPulseTemplate(self.body, repetitions) parameters = {} - measurement_mapping = {'my' : 'thy'} + measurement_mapping = {'my': 'thy'} conditions = dict(foo=DummyCondition(requires_stop=True)) channel_mapping = {} t.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, channel_mapping, self.block) @@ -130,7 +129,7 @@ def test_build_sequence_constant(self) -> None: self.assertEqual([REPJInstruction(repetitions, InstructionPointer(body_block, 0))], self.block.instructions) def test_build_sequence_declaration_success(self) -> None: - parameters = dict(foo=3) + parameters = dict(foo=ConstantParameter(3)) conditions = dict(foo=DummyCondition(requires_stop=True)) measurement_mapping = dict(moth='fire') channel_mapping = dict(asd='f') @@ -141,13 +140,12 @@ def test_build_sequence_declaration_success(self) -> None: self.assertEqual({body_block}, set(self.sequencer.sequencing_stacks.keys())) self.assertEqual([(self.body, parameters, conditions, measurement_mapping, channel_mapping)], self.sequencer.sequencing_stacks[body_block]) - self.assertEqual([REPJInstruction(parameters['foo'], InstructionPointer(body_block, 0))], self.block.instructions) - + self.assertEqual([REPJInstruction(3, InstructionPointer(body_block, 0))], self.block.instructions) def test_build_sequence_declaration_exceeds_bounds(self) -> None: - parameters = dict(foo=9) + parameters = dict(foo=ConstantParameter(9)) conditions = dict(foo=DummyCondition(requires_stop=True)) - with self.assertRaises(ParameterValueIllegalException): + with self.assertRaises(ParameterConstraintViolation): self.template.build_sequence(self.sequencer, parameters, conditions, {}, {}, self.block) self.assertFalse(self.sequencer.sequencing_stacks) @@ -159,7 +157,7 @@ def test_build_sequence_declaration_parameter_missing(self) -> None: self.assertFalse(self.sequencer.sequencing_stacks) def test_build_sequence_declaration_parameter_value_not_whole(self) -> None: - parameters = dict(foo=3.3) + parameters = dict(foo=ConstantParameter(3.3)) conditions = dict(foo=DummyCondition(requires_stop=True)) with self.assertRaises(ParameterNotIntegerException): self.template.build_sequence(self.sequencer, parameters, conditions, {}, {}, self.block) @@ -176,23 +174,19 @@ def test_get_serialization_data_constant(self) -> None: repetition_count = 3 template = RepetitionPulseTemplate(self.body, repetition_count) expected_data = dict( - type=self.serializer.get_type_identifier(template), body=str(id(self.body)), repetition_count=repetition_count, - atomicity=False + parameter_constraints=[] ) data = template.get_serialization_data(self.serializer) self.assertEqual(expected_data, data) def test_get_serialization_data_declaration(self) -> None: - repetition_count = ParameterDeclaration('foo') - template = RepetitionPulseTemplate(self.body, repetition_count) - template.atomicity = True + template = RepetitionPulseTemplate(self.body, 'foo', parameter_constraints=['foo<3']) expected_data = dict( - type=self.serializer.get_type_identifier(template), body=str(id(self.body)), - repetition_count=str(id(repetition_count)), - atomicity=True + repetition_count='foo', + parameter_constraints=[ParameterConstraint('foo<3')] ) data = template.get_serialization_data(self.serializer) self.assertEqual(expected_data, data) @@ -203,7 +197,7 @@ def test_deserialize_constant(self) -> None: repetition_count=repetition_count, body=dict(name=str(id(self.body))), identifier='foo', - atomicity=True + parameter_constraints=['bar<3'] ) # prepare dependencies for deserialization self.serializer.subelements[str(id(self.body))] = self.body @@ -212,25 +206,25 @@ def test_deserialize_constant(self) -> None: # compare! self.assertEqual(self.body, template.body) self.assertEqual(repetition_count, template.repetition_count) - self.assertTrue(template.atomicity) + self.assertEqual([str(c) for c in template.parameter_constraints], ['bar < 3']) def test_deserialize_declaration(self) -> None: - repetition_count = ParameterDeclaration('foo') data = dict( - repetition_count=dict(name='foo'), + repetition_count='foo', body=dict(name=str(id(self.body))), identifier='foo', - atomicity=False + parameter_constraints=['foo<3'] ) # prepare dependencies for deserialization self.serializer.subelements[str(id(self.body))] = self.body - self.serializer.subelements['foo'] = repetition_count + # deserialize template = RepetitionPulseTemplate.deserialize(self.serializer, **data) + # compare! self.assertEqual(self.body, template.body) - self.assertEqual(repetition_count, template.repetition_count) - self.assertFalse(template.atomicity) + self.assertEqual('foo', template.repetition_count) + self.assertEqual(template.parameter_constraints, [ParameterConstraint('foo < 3')]) class ParameterNotIntegerExceptionTests(unittest.TestCase): diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index e544e74dd..1e6a66c76 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -8,7 +8,7 @@ from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate, SequenceWaveform from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate -from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ConstantParameter +from qctoolkit.pulses.parameters import ParameterNotProvidedException, ConstantParameter, ParameterConstraint from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate,\ DummyNoValueParameter, DummyWaveform @@ -90,45 +90,31 @@ def __init__(self, *args, **kwargs) -> None: self.parameters['pulse_length'] = ConstantParameter(100) self.parameters['voltage'] = ConstantParameter(10) - self.sequence = SequencePulseTemplate([MappingTemplate(self.square, self.mapping1, measurement_mapping=self.window_name_mapping)], self.outer_parameters) - - def test_missing_mapping(self) -> None: - mapping = self.mapping1 - mapping.pop('v') - - subtemplates = [(self.square, mapping, {})] - with self.assertRaises(MissingMappingException): - SequencePulseTemplate(subtemplates, self.outer_parameters) - - def test_unnecessary_mapping(self) -> None: - mapping = self.mapping1 - mapping['unnecessary'] = 'voltage' - - subtemplates = [(self.square, mapping, {})] - with self.assertRaises(UnnecessaryMappingException): - SequencePulseTemplate(subtemplates, self.outer_parameters) + self.sequence = SequencePulseTemplate(MappingTemplate(self.square, + parameter_mapping=self.mapping1, + measurement_mapping=self.window_name_mapping), + external_parameters=self.outer_parameters) def test_identifier(self) -> None: identifier = 'some name' - pulse = SequencePulseTemplate([DummyPulseTemplate()], {}, identifier=identifier) + pulse = SequencePulseTemplate(DummyPulseTemplate(), external_parameters=set(), identifier=identifier) self.assertEqual(identifier, pulse.identifier) def test_multiple_channels(self) -> None: dummy = DummyPulseTemplate(parameter_names={'hugo'}, defined_channels={'A', 'B'}) subtemplates = [(dummy, {'hugo': 'foo'}, {}), (dummy, {'hugo': '3'}, {})] - sequence = SequencePulseTemplate(subtemplates, {'foo'}) + sequence = SequencePulseTemplate(*subtemplates, external_parameters={'foo'}) self.assertEqual({'A', 'B'}, sequence.defined_channels) def test_multiple_channels_mismatch(self) -> None: with self.assertRaises(ValueError): - SequencePulseTemplate( - [DummyPulseTemplate(defined_channels={'A'}), DummyPulseTemplate(defined_channels={'B'})] - , set()) + SequencePulseTemplate(DummyPulseTemplate(defined_channels={'A'}), + DummyPulseTemplate(defined_channels={'B'})) with self.assertRaises(ValueError): SequencePulseTemplate( - [DummyPulseTemplate(defined_channels={'A'}), DummyPulseTemplate(defined_channels={'A', 'B'})] - , set()) + DummyPulseTemplate(defined_channels={'A'}), DummyPulseTemplate(defined_channels={'A', 'B'}) + , external_parameters=set()) class SequencePulseTemplateSerializationTests(unittest.TestCase): @@ -149,12 +135,12 @@ def test_get_serialization_data(self) -> None: dummy1 = DummyPulseTemplate() dummy2 = DummyPulseTemplate() - sequence = SequencePulseTemplate([dummy1, dummy2], []) + sequence = SequencePulseTemplate(dummy1, dummy2, parameter_constraints=['a None: serializer = DummySerializer(serialize_callback=lambda x: str(id(x))) data = dict( - subtemplates = [serializer.dictify(dummy1), serializer.dictify(dummy2)], - identifier='foo' + subtemplates=[serializer.dictify(dummy1), serializer.dictify(dummy2)], + identifier='foo', + parameter_constraints=['a None: sequencer = DummySequencer() block = DummyInstructionBlock() - seq = SequencePulseTemplate([(sub1, {}, {}), (sub2, {'foo': 'foo'}, {})], {'foo'}) + seq = SequencePulseTemplate(sub1, (sub2, {'foo': 'foo'}), external_parameters={'foo'}) seq.build_sequence(sequencer, parameters, {}, {}, {}, block) self.assertEqual(2, len(sequencer.sequencing_stacks[block])) sequencer = DummySequencer() block = DummyInstructionBlock() - seq = SequencePulseTemplate([(sub2, {'foo': 'foo'}, {}), (sub1, {}, {})], {'foo'}) + seq = SequencePulseTemplate((sub2, {'foo': 'foo'}), sub1, external_parameters={'foo'}) seq.build_sequence(sequencer, parameters, {}, {}, {}, block) self.assertEqual(2, len(sequencer.sequencing_stacks[block])) @@ -198,16 +186,16 @@ def test_requires_stop(self) -> None: sub2 = (DummyPulseTemplate(requires_stop=True, parameter_names={'foo'}), {'foo': 'foo'}, {}) parameters = {'foo': DummyNoValueParameter()} - seq = SequencePulseTemplate([sub1], {}) + seq = SequencePulseTemplate(sub1) self.assertFalse(seq.requires_stop(parameters, {})) - seq = SequencePulseTemplate([sub2], {'foo'}) + seq = SequencePulseTemplate(sub2, external_parameters={'foo'}) self.assertFalse(seq.requires_stop(parameters, {})) - seq = SequencePulseTemplate([sub1, sub2], {'foo'}) + seq = SequencePulseTemplate(sub1, sub2, external_parameters={'foo'}) self.assertFalse(seq.requires_stop(parameters, {})) - seq = SequencePulseTemplate([sub2, sub1], {'foo'}) + seq = SequencePulseTemplate(sub2, sub1, external_parameters={'foo'}) self.assertFalse(seq.requires_stop(parameters, {})) def test_missing_parameter_declaration_exception(self): @@ -216,7 +204,7 @@ def test_missing_parameter_declaration_exception(self): subtemplates = [(self.square, mapping,{})] with self.assertRaises(MissingParameterDeclarationException): - SequencePulseTemplate(subtemplates, self.outer_parameters) + SequencePulseTemplate(*subtemplates, external_parameters=self.outer_parameters) def test_crash(self) -> None: table = TablePulseTemplate({'default': [('ta', 'va', 'hold'), @@ -238,7 +226,8 @@ def test_crash(self) -> None: 'vb': 'va + vb', 'tend': '2 * tend' } - sequence = SequencePulseTemplate([(table, first_mapping, {}), (table, second_mapping, {})], external_parameters) + sequence = SequencePulseTemplate((table, first_mapping, {}), (table, second_mapping, {}), + external_parameters=external_parameters) parameters = { 'ta': ConstantParameter(2), @@ -252,7 +241,12 @@ def test_crash(self) -> None: sequencer = DummySequencer() block = DummyInstructionBlock() self.assertFalse(sequence.requires_stop(parameters, {})) - sequence.build_sequence(sequencer, parameters, {}, {}, {'default', 'default'}, block) + sequence.build_sequence(sequencer, + parameters=parameters, + conditions={}, + measurement_mapping={}, + channel_mapping={'default': 'default'}, + instruction_block=block) from qctoolkit.pulses.sequencing import Sequencer s = Sequencer() s.push(sequence, parameters, channel_mapping={'default': 'EXAMPLE_A'}) @@ -264,25 +258,22 @@ def test_missing_parameter_declaration_exception(self) -> None: subtemplates = [(self.square, mapping)] with self.assertRaises(MissingParameterDeclarationException): - SequencePulseTemplate(subtemplates, self.outer_parameters) + SequencePulseTemplate(*subtemplates, external_parameters=self.outer_parameters) class SequencePulseTemplateTestProperties(SequencePulseTemplateTest): def test_is_interruptable(self): self.assertTrue( - SequencePulseTemplate([DummyPulseTemplate(is_interruptable=True), - DummyPulseTemplate(is_interruptable=True)], []).is_interruptable) + SequencePulseTemplate(DummyPulseTemplate(is_interruptable=True), + DummyPulseTemplate(is_interruptable=True)).is_interruptable) self.assertTrue( - SequencePulseTemplate([DummyPulseTemplate(is_interruptable=True), - DummyPulseTemplate(is_interruptable=False)], []).is_interruptable) + SequencePulseTemplate(DummyPulseTemplate(is_interruptable=True), + DummyPulseTemplate(is_interruptable=False)).is_interruptable) self.assertFalse( - SequencePulseTemplate([DummyPulseTemplate(is_interruptable=False), - DummyPulseTemplate(is_interruptable=False)], []).is_interruptable) - - def test_parameter_declarations(self): - decl = self.sequence.parameter_declarations - self.assertEqual(decl, set([ParameterDeclaration(i) for i in self.outer_parameters])) + SequencePulseTemplate(DummyPulseTemplate(is_interruptable=False), + DummyPulseTemplate(is_interruptable=False)).is_interruptable) + class PulseTemplateConcatenationTest(unittest.TestCase): @@ -322,8 +313,8 @@ def test_concatenation_sequence_table_pulse(self): c = DummyPulseTemplate(parameter_names={'snu'}, defined_channels={'A'}) d = DummyPulseTemplate(parameter_names={'snu'}, defined_channels={'A'}) - seq1 = SequencePulseTemplate([a, b], ['foo', 'bar']) - seq2 = SequencePulseTemplate([c, d], ['snu']) + seq1 = SequencePulseTemplate(a, b, external_parameters=['foo', 'bar']) + seq2 = SequencePulseTemplate(c, d, external_parameters=['snu']) seq = seq1 @ c self.assertTrue(len(seq.subtemplates) == 3) diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index a82c5128c..177da4a0f 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -9,10 +9,11 @@ from qctoolkit.serialization import Serializer from qctoolkit.pulses.instructions import Waveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock, SequencingElement -from qctoolkit.pulses.parameters import Parameter, ParameterDeclaration +from qctoolkit.pulses.parameters import Parameter from qctoolkit.pulses.pulse_template import AtomicPulseTemplate from qctoolkit.pulses.interpolation import InterpolationStrategy from qctoolkit.pulses.conditions import Condition +from qctoolkit.expressions import Expression class DummyParameter(Parameter): @@ -279,10 +280,11 @@ def __init__(self, is_interruptable: bool=False, parameter_names: Set[str]={}, defined_channels: Set[ChannelID]={'default'}, - duration: float=0, + duration: Any=0, waveform: Waveform=None, - measurement_names: Set[str] = set()) -> None: - super().__init__() + measurement_names: Set[str] = set(), + identifier=None) -> None: + super().__init__(identifier=identifier) self.requires_stop_ = requires_stop self.requires_stop_arguments = [] @@ -290,18 +292,18 @@ def __init__(self, self.parameter_names_ = parameter_names self.build_sequence_arguments = [] self.defined_channels_ = defined_channels - self.duration = duration + self._duration = Expression(duration) self.waveform = waveform self.build_waveform_calls = [] self.measurement_names_ = measurement_names @property - def parameter_names(self) -> Set[str]: - return set(self.parameter_names_) + def duration(self): + return self._duration @property - def parameter_declarations(self) -> Set[str]: - return {ParameterDeclaration(name) for name in self.parameter_names} + def parameter_names(self) -> Set[str]: + return set(self.parameter_names_) def get_measurement_windows(self, parameters: Dict[str, Parameter] = None) -> List[MeasurementWindow]: """Return all measurement windows defined in this PulseTemplate.""" diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index fbabeaf97..072bf6261 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -1,17 +1,19 @@ import unittest import warnings +import functools import numpy from qctoolkit.expressions import Expression -from qctoolkit.pulses.instructions import EXECInstruction +from qctoolkit.serialization import Serializer from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry, ZeroDurationTablePulseTemplate -from qctoolkit.pulses.parameters import ParameterDeclaration, ParameterNotProvidedException, ParameterValueIllegalException, ParameterConstraintViolation +from qctoolkit.pulses.parameters import ParameterNotProvidedException, ParameterConstraintViolation from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyInterpolationStrategy, DummyParameter, DummyCondition -from tests.serialization_dummies import DummySerializer +from tests.serialization_dummies import DummySerializer, DummyStorageBackend +from tests.pulses.measurement_tests import ParameterConstrainerTest, MeasurementDefinerTest class TableEntryTest(unittest.TestCase): @@ -33,6 +35,8 @@ def test_unknown_interpolation_strategy(self): class TablePulseTemplateTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) def test_time_is_negative(self) -> None: with self.assertRaises(ValueError): @@ -325,7 +329,6 @@ def test_from_array_multi_one_voltage(self) -> None: i: [TableEntry(time, voltage, HoldInterpolationStrategy()) for (time, voltage) in zip(times[i, :], voltages)] for i in range(2)} - self.assertEqual(entries, pulse.entries) def test_add_entry_multi_same_time_param(self) -> None: @@ -367,6 +370,32 @@ def test_get_instantiated_entries_multi_same_time_param(self) -> None: } self.assertEqual(expected, entries) + def test_measurement_names(self): + tpt = TablePulseTemplate({0: [(10, 1)]}, measurements=[('A', 2, 3), ('AB', 0, 1)]) + self.assertEqual(tpt.measurement_names, {'A', 'AB'}) + + +class TablePulseTemplateConstraintTest(ParameterConstrainerTest): + def __init__(self, *args, **kwargs): + + def tpt_constructor(parameter_constraints=None): + return TablePulseTemplate({0: [('a', 'b')]}, + parameter_constraints=parameter_constraints, measurements=[('M', 'n', 1)]) + + super().__init__(*args, + to_test_constructor=tpt_constructor, **kwargs) + + +class TablePulseTemplateMeasurementTest(MeasurementDefinerTest): + def __init__(self, *args, **kwargs): + + def tpt_constructor(measurements=None): + return TablePulseTemplate({0: [('a', 'b')]}, + parameter_constraints=['a < b'], measurements=measurements) + + super().__init__(*args, + to_test_constructor=tpt_constructor, **kwargs) + class TablePulseTemplateSerializationTests(unittest.TestCase): @@ -400,6 +429,17 @@ def test_deserialize(self) -> None: self.assertEqual(template.entries, self.template.entries) self.assertEqual(template.measurement_declarations, self.template.measurement_declarations) + self.assertEqual(template.parameter_constraints, self.template.parameter_constraints) + + def test_serializer_integration(self): + serializer = Serializer(DummyStorageBackend()) + serializer.serialize(self.template) + template = serializer.deserialize('foo') + + self.assertIsInstance(template, TablePulseTemplate) + self.assertEqual(template.entries, self.template.entries) + self.assertEqual(template.measurement_declarations, self.template.measurement_declarations) + self.assertEqual(template.parameter_constraints, self.template.parameter_constraints) class TablePulseTemplateSequencingTests(unittest.TestCase): @@ -517,19 +557,12 @@ def test_identifier(self) -> None: self.assertEqual(pulse.identifier, identifier) - -class TableWaveformDataTests(unittest.TestCase): - +class TableWaveformTests(unittest.TestCase): def test_duration(self) -> None: entries = [WaveformTableEntry(0, 0, HoldInterpolationStrategy()), WaveformTableEntry(5, 1, HoldInterpolationStrategy())] waveform = TableWaveform('A', entries, []) self.assertEqual(5, waveform.duration) - @unittest.skip("What is the point of empty waveforms?") - def test_duration_no_entries(self) -> None: - waveform = TableWaveform([]) - self.assertEqual(0, waveform.duration) - def test_duration_no_entries_exception(self) -> None: with self.assertRaises(ValueError): waveform = TableWaveform('A', [], []) @@ -541,6 +574,24 @@ def test_few_entries(self) -> None: with self.assertRaises(ValueError): TableWaveform('A', [WaveformTableEntry(0, 0, HoldInterpolationStrategy())], []) + def test_get_measurement_windows(self): + interp = DummyInterpolationStrategy() + entries = [WaveformTableEntry(0, 0, interp), + WaveformTableEntry(2.1, -33.2, interp), + WaveformTableEntry(5.7, 123.4, interp)] + waveform = TableWaveform('A', entries, + measurement_windows=[('a', 1, 2), ('b', 3, 4)]) + self.assertEqual(waveform.get_measurement_windows(), (('a', 1, 2), ('b', 3, 4))) + + def test_unsafe_get_subset_for_channels(self): + interp = DummyInterpolationStrategy() + entries = [WaveformTableEntry(0, 0, interp), + WaveformTableEntry(2.1, -33.2, interp), + WaveformTableEntry(5.7, 123.4, interp)] + waveform = TableWaveform('A', entries, + measurement_windows=[('a', 1, 2), ('b', 3, 4)]) + self.assertIs(waveform.unsafe_get_subset_for_channels({'A'}), waveform) + def test_unsafe_sample(self) -> None: interp = DummyInterpolationStrategy() entries = [WaveformTableEntry(0, 0, interp), @@ -577,12 +628,5 @@ def test_simple_properties(self): self.assertIs(waveform.unsafe_get_subset_for_channels('A'), waveform) -class ParameterValueIllegalExceptionTest(unittest.TestCase): - - def test(self) -> None: - decl = ParameterDeclaration('foo', max=8) - exception = ParameterValueIllegalException(decl, 8.1) - self.assertEqual("The value 8.1 provided for parameter foo is illegal (min = -inf, max = 8)", str(exception)) - if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/tests/pulses/table_sequence_sequencer_intergration_tests.py b/tests/pulses/table_sequence_sequencer_intergration_tests.py index 8805849f2..61f34f7b1 100644 --- a/tests/pulses/table_sequence_sequencer_intergration_tests.py +++ b/tests/pulses/table_sequence_sequencer_intergration_tests.py @@ -22,8 +22,8 @@ def test_table_sequence_sequencer_integration(self) -> None: (5, 0)]}, measurements=[('foo', 4, 1)]) - seqt = SequencePulseTemplate([MappingTemplate(t1, {'foo': 'foo'}, measurement_mapping={'foo': 'bar'}), - MappingTemplate(t2, {'bar': '2 * hugo'})], {'foo', 'hugo'}) + seqt = SequencePulseTemplate(MappingTemplate(t1, measurement_mapping={'foo': 'bar'}), + MappingTemplate(t2, parameter_mapping={'bar': '2 * hugo'})) with self.assertRaises(ParameterNotProvidedException): t1.requires_stop(dict(), dict()) diff --git a/tests/serialization_tests.py b/tests/serialization_tests.py index 16c29828c..3c86f3bac 100644 --- a/tests/serialization_tests.py +++ b/tests/serialization_tests.py @@ -1,13 +1,13 @@ import unittest import os.path import json +import abc from tempfile import TemporaryDirectory from typing import Optional, Dict, Any -from qctoolkit.serialization import FilesystemBackend, Serializer, CachingBackend, Serializable +from qctoolkit.serialization import FilesystemBackend, Serializer, CachingBackend, Serializable, ExtendedJSONEncoder from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate -from qctoolkit.pulses.parameters import ParameterDeclaration from tests.serialization_dummies import DummyStorageBackend @@ -41,7 +41,6 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: class SerializableTests(unittest.TestCase): - def test_identifier(self) -> None: serializable = DummySerializable() self.assertEqual(None, serializable.identifier) @@ -330,9 +329,9 @@ def test_serialization_and_deserialization_combined(self) -> None: table = TablePulseTemplate({'default': [('t', 0)]}) foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') - sequence = SequencePulseTemplate([(table_foo, foo_mappings, dict()), - (table, dict(t=0), dict())], - ['ilse', 'albert', 'voltage'], + sequence = SequencePulseTemplate((table_foo, foo_mappings, dict()), + (table, dict(t=0), dict()), + external_parameters=['ilse', 'albert', 'voltage'], identifier=None) storage = DummyStorageBackend() @@ -350,5 +349,29 @@ def test_serialization_and_deserialization_combined(self) -> None: self.assertEqual(serialized_sequence, storage.stored_items['main']) +class TriviallyRepresentableEncoderTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_encoding(self): + class A: + def __str__(self): + return 'aaa' + ExtendedJSONEncoder.str_constructable_types.add(A) + + class B: + pass + + encoder = ExtendedJSONEncoder() + + a = A() + self.assertEqual(encoder.default(a), 'aaa') + + with self.assertRaises(TypeError): + encoder.default(B()) + + self.assertEqual(encoder.default({'a', 1}), list({'a', 1})) + + if __name__ == "__main__": unittest.main(verbosity=2) From 19f14244d8dc8762c1d10e5972acf2fdc7a82e64 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Fri, 19 May 2017 11:49:01 +0200 Subject: [PATCH 060/116] Remove sample generator from tests --- tests/pulses/sample_pulse_generator.py | 88 -------------------------- 1 file changed, 88 deletions(-) delete mode 100644 tests/pulses/sample_pulse_generator.py diff --git a/tests/pulses/sample_pulse_generator.py b/tests/pulses/sample_pulse_generator.py deleted file mode 100644 index 70514b569..000000000 --- a/tests/pulses/sample_pulse_generator.py +++ /dev/null @@ -1,88 +0,0 @@ -import random - -from qctoolkit.pulses.parameters import ConstantParameter -from qctoolkit.pulses.parameters import ParameterDeclaration -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate - -RANGE = 100 -INTERPOLATION_STRATEGIES = list(TablePulseTemplate()._TablePulseTemplate__interpolation_strategies.keys()) -#random.seed(2) - -def getOrdered(length, min_=-RANGE, max_=RANGE,max_bound=None): - a = [random.uniform(min_,max_) for _ in range(length)] - a.sort() - if max_bound and a[2] < max_bound: - a[2] = random.uniform (max_bound,max_) - return a - -class SampleGenerator(object): - def __init__(self, *args, **kwargs): - object.__init__(self, *args, **kwargs) - self.__ParameterDeclarationNameOffset = 0 - self.__TablePulseTemplateOffset = 0 - - def generate_ConstantParameter(self, min_=-RANGE, max_=RANGE): - while 1: yield ConstantParameter(random.uniform(min_,max_)) - - def generate_ParameterDeclaration(self, min_=-RANGE, max_=RANGE, name_prefix="",max_bound=None): - while 1: - bounds = getOrdered(3,min_,max_,max_bound) - self.__ParameterDeclarationNameOffset+= 1 - yield ParameterDeclaration(name="ParameterDeclaration_{}".format(self.__ParameterDeclarationNameOffset),min=bounds[0],max=bounds[2],default=bounds[1]) - - def generate_TablePulseTemplates(self,number_of_entries,max_dist=RANGE): - while 1: - x = TablePulseTemplate() - name = "TablePulseTemplate_{}".format(self.__TablePulseTemplateOffset) - self.__TablePulseTemplateOffset +=1 - if bool(random.getrandbits(1)): - x.identifier = name - previous_min = 0 - previous_max = 0 - parameter_names = [] - - for i in range(number_of_entries): - dict_ = {} - for j in ["time","voltage"]: - a = random.choice(["float","str","ParameterDeclaration"]) - if a == "float": - if j == "time": - dict_[j] = random.uniform(previous_max,previous_max+max_dist) - previous_min = dict_[j] - previous_max = dict_[j] - else: - dict_[j] = random.uniform(-RANGE,RANGE) - elif a == "str": - dict_[j] = "_".join([name,"str",str(i),str(j)]) - parameter_names.append(dict_[j]) - elif a == "ParameterDeclaration": - if j == "time": - dict_[j] = self.generate_ParameterDeclaration(previous_min, previous_min+max_dist, name, previous_max).__next__() - previous_min = dict_[j].min_value - previous_max = dict_[j].max_value - else: - dict_[j] = self.generate_ParameterDeclaration().__next__() - parameter_names.append(dict_[j].name) - x.add_entry(time=dict_["time"],voltage=dict_["voltage"],interpolation=random.choice(INTERPOLATION_STRATEGIES)) - yield x - - def generate_SequencePulseTemplate(self): - pass - - -class CounterExampleGenerator(object): - def __init__(self, *args, **kwargs): - object.__init__(self, *args, **kwargs) - self.__ParameterDeclarationNameOffset = 0 - - def generate_ConstantParameter(self): - yield ConstantParameter(float('inf')) - yield ConstantParameter(float('-inf')) - - def generate_ParameterDeclaration(self, max_=RANGE, min_=(-RANGE)): - while 1: - bounds = [random.uniform(min_,max_) for _ in range(3)] - while bounds == sorted(bounds):bounds = random.shuffle(bounds) - self.__ParameterDeclarationNameOffset+= 1 - yield ParameterDeclaration(name="var_{}".format(self.__ParameterDeclarationNameOffset),min=bounds[0],max=bounds[2],default=bounds[1]) - \ No newline at end of file From 555aae6b96b7cc3f6647cbe0094fe5dd5c8b87b6 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 12 Jul 2017 10:27:25 +0200 Subject: [PATCH 061/116] Update examples 01 to 04 --- doc/source/examples/00SimpleTablePulse.ipynb | 215 +++-- doc/source/examples/01SequencePulse.ipynb | 82 +- doc/source/examples/02FunctionPulse.ipynb | 126 +-- doc/source/examples/03Serialization.ipynb | 874 +++++++++++++++++- doc/source/examples/04Sequencing.ipynb | 803 +--------------- .../examples/FreeInductionDecayExample.ipynb | 522 +++++++++++ .../sequence_referenced.json | 37 +- .../serialized_pulses/table_template.json | 55 +- 8 files changed, 1628 insertions(+), 1086 deletions(-) create mode 100644 doc/source/examples/FreeInductionDecayExample.ipynb diff --git a/doc/source/examples/00SimpleTablePulse.ipynb b/doc/source/examples/00SimpleTablePulse.ipynb index b2c8d1600..e9a3f5347 100644 --- a/doc/source/examples/00SimpleTablePulse.ipynb +++ b/doc/source/examples/00SimpleTablePulse.ipynb @@ -10,7 +10,11 @@ "\n", "![The pulse we want to model using the qctoolkit](img/example_pulse.png)\n", "\n", - "Assume we want to model a pulse as depicted by the figure above. Since the structure is relatively simple and relies only on a few critical points between which the values are interpolated (indicated by the crosses in the figure), we will do so by using a `TablePulseTemplate` and setting values at appropriate times. First, let us instantiate a `TablePulseTemplate` object:" + "Assume we want to model a pulse as depicted by the figure above. Since the structure is relatively simple and relies only on a few critical points between which the values are interpolated (indicated by the crosses in the figure), we will do so by using a `TablePulseTemplate` and setting values at appropriate times.\n", + "\n", + "For our first try, let's just fix some values for the parameters. Let us set $t_a$ = 2, $v_a$ = 2, $t_b$ = 4, $v_b$ = 3, $t_{end}$ = 6. Our pulse then holds a value of 0 for 2 units of time, then jumps to 2 and subsequently ramps to 3 over the next 2 units of time. Finally, it returns to holding 0 for another 2 units of time. The supporting points for the table are thus (0,0), (2,2), (4,3), (6,0).\n", + "\n", + "Let us first put these entries into a list with the correct interpolation strategies:" ] }, { @@ -21,16 +25,19 @@ }, "outputs": [], "source": [ - "from qctoolkit.pulses import TablePulseTemplate\n", - "\n", - "template = TablePulseTemplate(identifier='foo')" + "entry_list = [(0, 0),\n", + " (2, 2, 'hold'),\n", + " (4, 3, 'linear'),\n", + " (6, 0, 'jump')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "For our first try, let's just fix some values for the parameters. Let us set $t_a$ = 2, $v_a$ = 2, $t_b$ = 4, $v_b$ = 3, $t_{end}$ = 6. Our pulse then holds a value of 0 for 2 units of time, then jumps to 2 and subsequently ramps to 3 over the next 2 units of time. Finally, it returns to holding 0 for another 2 units of time. The supporting points for the table are thus (0,0), (2,2), (4,3), (6,0). We add these to our `TablePulseTemplate` object `template` with the correct interpolation strategies as follows:" + "The interpolation set for an entry always applies to the range from the previous entry to the new entry. Thus, the value for the first entry is always ignored. The default value for interpolation is 'hold'. 'hold' and 'jump' differ in that 'hold' will hold the previous value until the new entry is reached while 'jump' will immediately assume the value of the new entry.\n", + "\n", + "Now, we must decide on which channel the `TablePulseTemplate` is defined on. As a `TablePulseTemplate` can contain multiple channels it takes a python `dict` whose keys are the channel identifiers and whose values are lists of entries like the one above. Channel identifiers can be either strings like 'channel_A' or integers. For this example we choose the channel ID '0' for our pulse and create the `TablePulseTemplate` `template`:" ] }, { @@ -41,18 +48,17 @@ }, "outputs": [], "source": [ - "template.add_entry(0, 0)\n", - "template.add_entry(2, 2, interpolation='hold')\n", - "template.add_entry(4, 3, interpolation='linear')\n", - "template.add_entry(6, 0, interpolation='jump')" + "from qctoolkit.pulses import TablePulseTemplate\n", + "\n", + "entries = {0: entry_list}\n", + "\n", + "template = TablePulseTemplate(entries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Note that we could omit the first instruction: If the time value of the first call to `add_entry` is greater than zero, a starting entry (0,0) is automatically set. Note further that the interpolation set for an entry always applies to the range from the previous entry to the new entry. Thus, the value for the first entry is always ignored. The default value for interpolation is 'hold'. 'hold' and 'jump' differ in that 'hold' will hold the previous value until the new entry is reached while 'jump' will immediately assume the value of the new entry.\n", - "\n", "We plot `template` to see if everything is correct (ignore the matplotlib warning here):" ] }, @@ -69,6 +75,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -127,6 +134,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -196,6 +206,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -252,8 +271,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -386,10 +406,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -545,8 +565,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -608,7 +628,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -667,6 +687,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -675,7 +696,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -686,8 +707,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -776,12 +798,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -830,7 +849,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -844,10 +863,7 @@ "%matplotlib notebook\n", "from qctoolkit.pulses import plot\n", "\n", - "try:\n", - " plot(template, sample_rate=100)\n", - "except:\n", - " pass" + "_ = plot(template, sample_rate=100)" ] }, { @@ -866,21 +882,23 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": true + "collapsed": false }, "outputs": [], "source": [ - "param_template = TablePulseTemplate()\n", - "param_template.add_entry('ta', 'va', interpolation='hold')\n", - "param_template.add_entry('tb', 'vb', interpolation='linear')\n", - "param_template.add_entry('tend', 0, interpolation='jump')" + "param_entries = {0: [(0, 0),\n", + " ('ta', 'va', 'hold'),\n", + " ('tb', 'vb', 'linear'),\n", + " ('tend', 0, 'jump')]}\n", + "\n", + "param_template = TablePulseTemplate(param_entries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Instead of using numerical values, we simply insert parameter names in our calls to `add_entry`. You can use any combination of numerical values or parameters names here. Note that we also gave our object the optional identifier 'foo'. Our `param_template` thus now defines a set of parameters." + "Instead of using numerical values, we simply insert parameter names in our entry list. You can use any combination of numerical values or parameters names here. Note that we also gave our object the optional identifier 'foo'. Our `param_template` thus now defines a set of parameters." ] }, { @@ -894,7 +912,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'va', 'tb', 'vb', 'tend', 'ta'}\n" + "{'va', 'tb', 'tend', 'ta', 'vb'}\n" ] } ], @@ -922,6 +940,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -980,6 +999,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -1049,6 +1071,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1105,8 +1136,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -1239,10 +1271,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -1398,8 +1430,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -1461,7 +1493,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -1520,6 +1552,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -1528,7 +1561,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -1539,8 +1572,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -1629,12 +1663,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -1683,7 +1714,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1700,7 +1731,7 @@ " 'tb': 4,\n", " 'vb': 3,\n", " 'tend': 6}\n", - "plot(param_template, parameters, sample_rate=100)" + "_ = plot(param_template, parameters, sample_rate=100)" ] }, { @@ -1723,6 +1754,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -1781,6 +1813,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -1850,6 +1885,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1906,8 +1950,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -2040,10 +2085,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -2199,8 +2244,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -2262,7 +2307,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -2321,6 +2366,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -2329,7 +2375,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -2340,8 +2386,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -2430,12 +2477,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -2484,7 +2528,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -2501,24 +2545,15 @@ " 'tb': 6,\n", " 'vb': 3,\n", " 'tend': 8}\n", - "plot(param_template, parameters, sample_rate=100)" + "_ = plot(param_template, parameters, sample_rate=100)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "lab_master", "language": "python", - "name": "python3" + "name": "lab_master" }, "language_info": { "codemirror_mode": { @@ -2530,7 +2565,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.3" } }, "nbformat": 4, diff --git a/doc/source/examples/01SequencePulse.ipynb b/doc/source/examples/01SequencePulse.ipynb index d7a0e6540..a2c70d9f1 100644 --- a/doc/source/examples/01SequencePulse.ipynb +++ b/doc/source/examples/01SequencePulse.ipynb @@ -21,10 +21,10 @@ "source": [ "from qctoolkit.pulses import TablePulseTemplate\n", "\n", - "template = TablePulseTemplate(identifier='foo')\n", - "template.add_entry('ta', 'va', interpolation='hold')\n", - "template.add_entry('tb', 'vb', interpolation='linear')\n", - "template.add_entry('tend', 0, interpolation='jump')" + "template = TablePulseTemplate(entries={0: [(0, 0),\n", + " ('ta', 'va', 'hold'),\n", + " ('tb', 'vb', 'linear'),\n", + " ('tend', 0, 'jump')]},identifier='foo')" ] }, { @@ -59,9 +59,9 @@ " 'vb': 'va + vb',\n", " 'tend': '2 * tend'\n", "}\n", - "sequence = SequencePulseTemplate([(template, first_mapping),\n", - " (template, second_mapping)],\n", - " external_parameters)" + "sequence = SequencePulseTemplate((template, first_mapping),\n", + " (template, second_mapping),\n", + " external_parameters=external_parameters)" ] }, { @@ -82,7 +82,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'va', 'tend', 'tc', 'td', 'tb', 'vb', 'ta'}\n" + "{'vb', 'ta', 'va', 'tb', 'tend', 'tc', 'td'}\n" ] } ], @@ -113,6 +113,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -171,6 +172,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -240,6 +244,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -296,8 +309,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -430,10 +444,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -589,8 +603,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -652,7 +666,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -711,6 +725,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -719,7 +734,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -730,8 +745,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -820,12 +836,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -874,7 +887,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -895,24 +908,15 @@ " 'tc': 5,\n", " 'td': 11,\n", " 'tend': 6}\n", - "plot(sequence, parameters, sample_rate=100)" + "_ = plot(sequence, parameters, sample_rate=100)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "lab_master", "language": "python", - "name": "python3" + "name": "lab_master" }, "language_info": { "codemirror_mode": { @@ -924,7 +928,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.3" } }, "nbformat": 4, diff --git a/doc/source/examples/02FunctionPulse.ipynb b/doc/source/examples/02FunctionPulse.ipynb index f75c70ad1..497eb62a4 100644 --- a/doc/source/examples/02FunctionPulse.ipynb +++ b/doc/source/examples/02FunctionPulse.ipynb @@ -6,7 +6,7 @@ "source": [ "# Modelling Pulses Using Functions And Expressions\n", "\n", - "Assume we want to model a pulse that represents a damped sine function. While we could, in theory, do this using `TablePulseTemplate`s by piecewise linear approximation (cf. [Modelling a Simple TablePulseTemplate](00SimpleTablePulse.ipynb)), this would be a tedious endeavor. A much simpler approach presents itself in the form of the `FunctionPulseTemplate` class of the qctoolkit. Like the `TablePulseTemplate`, a `FunctionPulseTemplate` represents an atomic pulse which will be converted into a waveform for execution. The difference between both is that `FunctionPulseTemplate` accepts a mathematical expression which is parsed and evaluated using `py_expression_eval` to sample the waveform instead of the linear interpolation between specified supporting points as it is done in `TablePulseTemplate`.\n", + "Assume we want to model a pulse that represents a damped sine function. While we could, in theory, do this using `TablePulseTemplate`s by piecewise linear approximation (cf. [Modelling a Simple TablePulseTemplate](00SimpleTablePulse.ipynb)), this would be a tedious endeavor. A much simpler approach presents itself in the form of the `FunctionPulseTemplate` class of the qctoolkit. Like the `TablePulseTemplate`, a `FunctionPulseTemplate` represents an atomic pulse which will be converted into a waveform for execution. The difference between both is that `FunctionPulseTemplate` accepts a mathematical expression which is parsed and evaluated using `sympy` to sample the waveform instead of the linear interpolation between specified supporting points as it is done in `TablePulseTemplate`.\n", "\n", "To define the sine function pulse template, we can thus do the following:" ] @@ -24,6 +24,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -82,6 +83,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -151,6 +155,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -207,8 +220,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -341,10 +355,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -500,8 +514,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -563,7 +577,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -622,6 +636,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -630,7 +645,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -641,8 +656,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -731,12 +747,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -785,7 +798,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -803,14 +816,14 @@ "%matplotlib notebook\n", "from qctoolkit.pulses import plot\n", "\n", - "plot(template, sample_rate=100)" + "_ = plot(template, sample_rate=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The first argument to `FunctionPulseTemplate`'s constructor is the string representation of the formula that the pulse represents. The second argument is used to compute the length of the pulse. In this case, this is simply a constant expression. Refer to [py-expression-eval's documentation](https://github.com/AxiaCore/py-expression-eval#available-operators-constants-and-functions) to read about the usable operators and functions in the expressions.\n", + "The first argument to `FunctionPulseTemplate`'s constructor is the string representation of the formula that the pulse represents. The second argument is used to compute the length of the pulse. In this case, this is simply a constant expression. Refer to [sympy's documentation](http://docs.sympy.org/latest/index.html) to read about the usable operators and functions in the expressions.\n", "\n", "The `t` is reserved as the free variable of the time domain in the first argument and must be present. Other variables can be used at will and corresponding values have to be passed in as a parameter when instantiating the `FunctionPulseTemplate`:" ] @@ -829,6 +842,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -887,6 +901,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -956,6 +973,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1012,8 +1038,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -1146,10 +1173,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -1305,8 +1332,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -1368,7 +1395,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -1427,6 +1454,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -1435,7 +1463,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -1446,8 +1474,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -1536,12 +1565,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -1590,7 +1616,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1601,29 +1627,17 @@ } ], "source": [ - "param_template = FunctionPulseTemplate('exp(-t/lambda)*sin(phi*t)', 'duration')\n", + "param_template = FunctionPulseTemplate('exp(-t/tau)*sin(phi*t)', 'duration')\n", "\n", - "%matplotlib notebook\n", - "from qctoolkit.pulses import plot\n", - "\n", - "plot(param_template, {'lambda': 4, 'phi': 8, 'duration': 4*3.1415}, sample_rate=100)" + "_ = plot(param_template, {'tau': 4, 'phi': 8, 'duration': 4*3.1415}, sample_rate=100)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "lab_master", "language": "python", - "name": "python3" + "name": "lab_master" }, "language_info": { "codemirror_mode": { @@ -1635,7 +1649,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.3" } }, "nbformat": 4, diff --git a/doc/source/examples/03Serialization.ipynb b/doc/source/examples/03Serialization.ipynb index 030d4fa30..3933eb21a 100644 --- a/doc/source/examples/03Serialization.ipynb +++ b/doc/source/examples/03Serialization.ipynb @@ -23,10 +23,9 @@ "from qctoolkit.pulses import TablePulseTemplate\n", "from qctoolkit.serialization import Serializer, FilesystemBackend\n", "\n", - "anonymous_table = TablePulseTemplate()\n", - "anonymous_table.add_entry('ta', 'va', interpolation='hold')\n", - "anonymous_table.add_entry('tb', 'vb', interpolation='linear')\n", - "anonymous_table.add_entry('tend', 0, interpolation='jump')\n", + "anonymous_table = TablePulseTemplate({'A': [('ta', 'va', 'hold'),\n", + " ('tb', 'vb', 'linear'),\n", + " ('tend', 0, 'jump')]})\n", "\n", "backend = FilesystemBackend(\"./serialized_pulses\")\n", "serializer = Serializer(backend)\n", @@ -51,10 +50,10 @@ }, "outputs": [], "source": [ - "identified_table = TablePulseTemplate(identifier='table_template')\n", - "identified_table.add_entry('ta', 'va', interpolation='hold')\n", - "identified_table.add_entry('tb', 'vb', interpolation='linear')\n", - "identified_table.add_entry('tend', 0, interpolation='jump')\n", + "identified_table = TablePulseTemplate({'A': [('ta', 'va', 'hold'),\n", + " ('tb', 'vb', 'linear'),\n", + " ('tend', 0, 'jump')]},\n", + " identifier='table_template')\n", "\n", "serializer.serialize(identified_table, overwrite=True)" ] @@ -83,6 +82,796 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('');\n", - " button.click(method_name, toolbar_event);\n", - " button.mouseover(tooltip, toolbar_mouse_event);\n", - " nav_element.append(button);\n", - " }\n", - "\n", - " // Add the status bar.\n", - " var status_bar = $('');\n", - " nav_element.append(status_bar);\n", - " this.message = status_bar[0];\n", - "\n", - " // Add the close button to the window.\n", - " var buttongrp = $('
');\n", - " var button = $('');\n", - " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", - " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", - " buttongrp.append(button);\n", - " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", - " titlebar.prepend(buttongrp);\n", - "}\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(el){\n", - " var fig = this\n", - " el.on(\"remove\", function(){\n", - "\tfig.close_ws(fig, {});\n", - " });\n", - "}\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(el){\n", - " // this is important to make the div 'focusable\n", - " el.attr('tabindex', 0)\n", - " // reach out to IPython and tell the keyboard manager to turn it's self\n", - " // off when our div gets focus\n", - "\n", - " // location in version 3\n", - " if (IPython.notebook.keyboard_manager) {\n", - " IPython.notebook.keyboard_manager.register_events(el);\n", - " }\n", - " else {\n", - " // location in version 2\n", - " IPython.keyboard_manager.register_events(el);\n", - " }\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._key_event_extra = function(event, name) {\n", - " var manager = IPython.notebook.keyboard_manager;\n", - " if (!manager)\n", - " manager = IPython.keyboard_manager;\n", - "\n", - " // Check for shift+enter\n", - " if (event.shiftKey && event.which == 13) {\n", - " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", - " }\n", - "}\n", - "\n", - "mpl.figure.prototype.handle_save = function(fig, msg) {\n", - " fig.ondownload(fig, null);\n", - "}\n", - "\n", - "\n", - "mpl.find_output_cell = function(html_output) {\n", - " // Return the cell and output element which can be found *uniquely* in the notebook.\n", - " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", - " // IPython event is triggered only after the cells have been serialised, which for\n", - " // our purposes (turning an active figure into a static one), is too late.\n", - " var cells = IPython.notebook.get_cells();\n", - " var ncells = cells.length;\n", - " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", - " data = data.data;\n", - " }\n", - " if (data['text/html'] == html_output) {\n", - " return [cell, data, j];\n", - " }\n", - " }\n", - " }\n", - " }\n", - "}\n", - "\n", - "// Register the function which deals with the matplotlib target/channel.\n", - "// The kernel may be null if the page has been refreshed.\n", - "if (IPython.notebook.kernel != null) {\n", - " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", - "}\n" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" + "ename": "TypeError", + "evalue": "__init__() missing 1 required positional argument: 'entries'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mqctoolkit\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpulses\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mTablePulseTemplate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mFunctionPulseTemplate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSequencePulseTemplate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mplot\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mtable_template\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTablePulseTemplate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0mtable_template\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_entry\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ta'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'va'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minterpolation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'hold'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mtable_template\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_entry\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'tb'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'vb'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minterpolation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'linear'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: __init__() missing 1 required positional argument: 'entries'" + ] } ], "source": [ @@ -836,19 +71,11 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[, , ]\n" - ] - } - ], + "outputs": [], "source": [ "from qctoolkit.pulses import Sequencer\n", "\n", @@ -898,9 +125,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "lab_master", "language": "python", - "name": "python3" + "name": "lab_master" }, "language_info": { "codemirror_mode": { @@ -912,7 +139,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.3" } }, "nbformat": 4, diff --git a/doc/source/examples/FreeInductionDecayExample.ipynb b/doc/source/examples/FreeInductionDecayExample.ipynb new file mode 100644 index 000000000..39f769ea6 --- /dev/null +++ b/doc/source/examples/FreeInductionDecayExample.ipynb @@ -0,0 +1,522 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from qctoolkit.pulses import TablePulseTemplate\n", + "import qctoolkit.pulses.plotting\n", + "import numpy as np\n", + "import sympy as sp\n", + "from sympy import sympify as spy\n", + "\n", + "#measurement point\n", + "M = sp.IndexedBase('M', shape=(2, ))\n", + "\n", + "#singlet initialization point\n", + "S_init = sp.IndexedBase('S_init', shape=(2, ))\n", + "\n", + "#operation point\n", + "O = sp.IndexedBase('O', shape=(2, ))\n", + "\n", + "#singlet triplet transition\n", + "ST = sp.IndexedBase('ST', shape=(2, ))\n", + "ST_jump = sp.IndexedBase('ST_jump', shape=(2, ))\n", + "\n", + "#init duration\n", + "init_dur = ('t_init')\n", + "\n", + "\n", + "def from_entry_list(tuple_list, channel_names=None, **kwargs):\n", + " max_len = max(len(t) for t in tuple_list)\n", + " min_len = min(len(t) for t in tuple_list)\n", + " \n", + " if max_len-min_len > 1:\n", + " raise Exception()\n", + " elif max_len-min_len == 1:\n", + " num_chan = min_len-1\n", + " else:\n", + " if all(t[-1] in TablePulseTemplate.interpolation_strategies.keys() or t[-1] in TablePulseTemplate.interpolation_strategies.values()\n", + " for t in tuple_list):\n", + " num_chan = min_len - 2\n", + " elif any(t[-1] in TablePulseTemplate.interpolation_strategies.keys() or t[-1] in TablePulseTemplate.interpolation_strategies.values()\n", + " for t in tuple_list):\n", + " raise Exception()\n", + " else:\n", + " num_chan = min_len - 1\n", + " \n", + " if channel_names is None:\n", + " channel_names = list(range(num_chan))\n", + " elif len(channel_names) != num_chan:\n", + " raise Exception()\n", + " \n", + " parsed = {channel_name: [] for channel_name in channel_names}\n", + " \n", + " \n", + " for time, *data in tuple_list:\n", + " if len(data) == num_chan:\n", + " volts = data\n", + " interp = 'hold'\n", + " else:\n", + " *volts, interp = data\n", + " \n", + " for channel_name, volt in zip(channel_names, volts):\n", + " parsed[channel_name].append((time, volt, interp))\n", + " \n", + " return TablePulseTemplate(parsed, **kwargs)\n", + " \n", + "\n", + "\n", + "time_diffs = [ 't_init', 't_ramp_up', '0', 't_ramp_low']\n", + "voltages = ['S_init', 'M', 'ST_up', 'ST_low', 'O']\n", + "interpol = ['hold', 'jump', 'linear', 'jump', 'linear']\n", + "\n", + "times = [0] + ['+'.join(time_diffs[:i+1]) for i in range(len(time_diffs))]\n", + "v_X = [v + '_X' for v in voltages]\n", + "v_Y = [v + '_Y' for v in voltages]\n", + "\n", + "substitutions = {'t_ramp_up': 'ST_up-M'}\n", + "\n", + "times = [sp.sympify(t) for t in times]\n", + "\n", + "init = TablePulseTemplate({'RFX': [(t, v, interp) for t, v, interp in zip(times, v_X, interpol)],\n", + " 'RFY': [(t, v, interp) for t, v, interp in zip(times, v_Y, interpol)]},\n", + " identifier='init')\n", + "\n", + "\n", + "#qctoolkit.pulses.plotting.plot(init, dict(t_init=2, t_ramp_up=3, t_ramp_low=2, S_init))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\lablocal\\Anaconda3\\envs\\lab_master\\lib\\site-packages\\matplotlib\\figure.py:402: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure\n", + " \"matplotlib is currently using a non-GUI backend, \"\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEWCAYAAABmE+CbAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHRhJREFUeJzt3XuUXHWZ7vHvkxAMt8BAwCAhJDIgNCQ0WFwCekDuKhMY\nTwABReJBmBkiMDookuUoHBhQnEEuyoDGOI4JFwOccBQUEDjAAIEOBGI6SkST2DFICEcwYIDgO3/U\nrk6l6Ut112XvXfV81urVddm96011ut569u9Xv62IwMzMbFjaBZiZWTa4IZiZGeCGYGZmCTcEMzMD\n3BDMzCzhhmBmZoAbgtmgSHqf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frek+294ziq/8zV593r948WIuvfRS\nHn30UUaPHs3LL7/cfd+qVat45JFH+OUvf8mUKVOYOnUqI0eO5I477mDUqFG89NJLHHTQQUyZMgWA\npUuXctNNN/Gd73yHk046idtuu41PfOITAKxfv54nnniCu+66i4svvpj77ruPmTNnsvXWW/Pkk0/y\nxhtvcMghh3D00UdXNMC7bt065s+fz9VXX91928MPP0x7ezsAJ554IjNmzGDHHXfk/PPPZ/LkyVxz\nzTVsu+22Q3oeB+KGYLVR72TQU+kxSuMUbggt7f777+fEE09k9OjRABu9YJ5wwgkMGzaMtrY2/vCH\nPwDFOfsXXXQRDz30EMOGDWPlypXd902YMKH7Bfn9738/y5Yt697Xxz72sXfcfs899/Dss892p49X\nXnmFpUuXsvvuu/dZ7/PPP097eztLly5l6tSpTJo0qfu+3g4ZAZxzzjlceOGFnHHGGYN8dirnhmDV\naVQy6MlJIbP6eyefhne9613dl0sDs7Nnz2b16tUsWLCAESNGMH78+O75++XbDx8+vPuQUfl9w4cP\nZ/369d37vPbaaznmmGM2etzyRtJTaQzhhRde4NBDD+XOO+/sTih9GTZsWN2n9noMwarT6GTQk8cU\nDDj88MP50Y9+xJo1awA2OmTUm1deeYUddtiBESNG8MADD7B8eUWrQ/fqmGOO4frrr++eKfTcc8/x\n2muvVfSzY8aM4YorruDyyy8f8uPXkhuCDU3HrA3vzkvJII1354VpxccuNYVZHy3WZi1lr732YsaM\nGRx66KHss88+fO5zn+t3+9NOO42Ojg4KhQKzZ89mjz32GPJjn3nmmbS1tbHffvux9957c/bZZ3en\nh0qccMIJvP76692D1GlSKULlQaFQCJ8gJyPKm0EWDtWkdejKAFiyZAl77rln2mUYvf8uJC2IiMJA\nP+uEYIOTlWTQk5OCWdU8qGyDk/aYwUA8+8hsyJwQrDJZTQY9OSmYDZkTglUm68mgJycFs0FzQrD+\n5SUZ9OSkYDZoTgjWv7wlg56cFMwq5oRgfeuYBcsfyVcy6MlJoaU1w/LXAOeeey6XXHJJ9z4uu+wy\nzjnn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RKwb4n7fpve/ubfK1p/p3iYi/k7SgRRPXLRQUntErBnE\nY5sNig8ZmVXuHjYcPkJSez0fTNKuETE/Iv6Z4gqWOw/0M2bVcEMwq9y5QCE5GXwn8Hd1frwrk0Ht\nX1AcW8jqWbusSXjaqZmZAU4IZmaWcEMwMzPADcHMzBJuCGZmBrghmJlZwg3BzMwANwQzM0v8N2sY\nRMIfsbiSAAAAAElFTkSuQmCC\n", + "text/plain": [ + "" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fid_direct = TablePulseTemplate({'RFX': [(0, 'S_init_X'),\n", + " ('t_init', 'M_X', 'hold'),\n", + " ('t_ST_fall', 'ST_up_X', 'linear'),\n", + " ('t_ST_fall', 'ST_low_X'),\n", + " ('t_op', 'O_X', 'linear'),\n", + " ('t_op + t_fid', 'O_X', 'hold'),\n", + " ('t_ST_jump', 'ST_low_X', 'linear'),\n", + " ('t_ST_jump', 'ST_up_X'),\n", + " ('t_meas_start', 'M_X', 'linear'),\n", + " ('t_meas_end', 'M_X', 'hold'),\n", + " ('total_duration', 'M_X', 'hold')],\n", + " 'RFY': [(0, 'S_init_Y'),\n", + " ('t_init', 'M_Y', 'hold'),\n", + " ('t_ST_fall', 'ST_up_Y', 'linear'),\n", + " ('t_ST_fall', 'ST_low_Y'),\n", + " ('t_op', 'O_Y', 'linear'),\n", + " ('t_op + t_fid', 'O_Y', 'hold'),\n", + " ('t_ST_jump', 'ST_low_Y', 'linear'),\n", + " ('t_ST_jump', 'ST_up_Y'),\n", + " ('t_meas_start', 'M_Y', 'linear'),\n", + " ('t_meas_end', 'M_Y', 'hold'),\n", + " ('total_duration', 'M_Y', 'hold')]},\n", + " identifier='fid',\n", + " measurements=[('MEAS', 't_meas_start', 't_meas_end - t_meas_start')])\n", + "\n", + "example_values = dict(t_init=1, t_ST_fall=3, t_op=5, t_fid=1, t_ST_jump=8, t_meas_start=10, t_meas_end=14, total_duration=16,\n", + " S_init_X=-1, M_X=0, ST_up_X=2, ST_low_X=3, O_X=5,\n", + " S_init_Y=-1, M_Y=0, ST_up_Y=-2, ST_low_Y=-3, O_Y=-5)\n", + "\n", + "from qctoolkit.pulses.plotting import plot\n", + "\n", + "plot(fid_direct, example_values, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "S_init_X, S_init_Y = sp.symbols('S_init_X S_init_Y')\n", + "M_X, M_Y = sp.symbols('M_X M_Y')\n", + "\n", + "ST_plus_X, ST_plus_Y = sp.symbols('ST_plus_X ST_plus_Y')\n", + "ST_plus_width_X, ST_plus_width_Y = sp.symbols('ST_plus_width_X, ST_plus_width_Y')\n", + "\n", + "ST_plus_begin_X = ST_plus_X - ST_plus_width_X/2\n", + "ST_plus_begin_Y = ST_plus_Y + ST_plus_width_Y/2\n", + "ST_plus_end_X = ST_plus_X - ST_plus_width_X/2\n", + "ST_plus_end_Y = ST_plus_Y + ST_plus_width_Y/2\n", + "\n", + "O_X, O_Y = sp.symbols('O_X, O_Y')\n", + "\n", + "ramp_speed_X = sp.symbols('ramp_speed_X')\n", + "\n", + "t_init = sp.symbols('t_init')\n", + "\n", + "t_ST_jump = t_init + (ST_plus_begin_X - M_X)/ramp_speed_X\n", + "t_op = t_ST_jump + (O_X - ST_plus_end_X)/ramp_speed_X\n", + "\n", + "conditions = [ST_plus_begin_X >= M_X,\n", + " ST_plus_end_X <= O_X,\n", + " ST_plus_begin_Y >= M_Y,\n", + " ST_plus_end_Y <= O_Y]\n", + "\n", + "init = from_entry_list([(0, 'S_init_X', 'S_init_Y'),\n", + " ('t_init', 'M_X', 'M_Y', 'hold'),\n", + " (t_ST_jump, ST_plus_begin_X, ST_plus_begin_Y, 'linear'),\n", + " (t_ST_jump, ST_plus_end_X, ST_plus_end_Y),\n", + " (t_op, O_X, O_Y, 'linear')],\n", + " identifier='init_up_down',\n", + " parameter_constraints=conditions)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "t_ST_jump = (O_X - ST_plus_end_X)/ramp_speed_X\n", + "t_meas_start = t_ST_jump + (ST_plus_begin_X - M_X)/ramp_speed_X\n", + "t_meas_end = t_meas_start + sp.symbols('t_meas')\n", + "\n", + "meas = from_entry_list([(0, 'O_X', 'O_Y'),\n", + " (t_ST_jump, ST_plus_end_X, ST_plus_end_Y, 'linear'),\n", + " (t_ST_jump, ST_plus_begin_X, ST_plus_begin_Y),\n", + " (t_meas_start, 'M_X', 'M_Y', 'linear'),\n", + " (t_meas_end, 'M_X', 'M_Y', 'hold')],\n", + " measurements=[('M', t_meas_start, 't_meas')],\n", + " identifier='meas')" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "total_duration" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from qctoolkit.pulses import SequencePulseTemplate\n", + "\n", + "fid_wait = from_entry_list([('t_fid', 'O_X', 'O_Y')])\n", + "\n", + "one_run = SequencePulseTemplate(init, fid_wait, meas)\n", + "\n", + "fill = from_entry_list([(sp.symbols('total_duration')-one_run.duration.sympified_expression, 'M_X', 'M_Y')])\n", + "\n", + "filled_run = SequencePulseTemplate(one_run, fill)\n", + "\n", + "filled_run.duration.sympified_expression.simplify()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'M_X',\n", + " 'M_Y',\n", + " 'N',\n", + " 'O_X',\n", + " 'O_Y',\n", + " 'ST_plus_X',\n", + " 'ST_plus_Y',\n", + " 'ST_plus_width_X',\n", + " 'ST_plus_width_Y',\n", + " 'S_init_X',\n", + " 'S_init_Y',\n", + " 'fid_step',\n", + " 'ramp_speed_X',\n", + " 't_init',\n", + " 't_meas',\n", + " 'total_duration'}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from qctoolkit.pulses.loop_pulse_template import ForLoopPulseTemplate\n", + "from qctoolkit.pulses import MappingTemplate\n", + "\n", + "mapped_run = MappingTemplate(filled_run, parameter_mapping={'t_fid': 'n*fid_step'}, allow_partial_parameter_mapping=True)\n", + "\n", + "fid_ramp = ForLoopPulseTemplate(mapped_run, loop_index='n', loop_range=(sp.symbols('N'),))\n", + "fid_ramp.parameter_names" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\lablocal\\Anaconda3\\envs\\lab_master\\lib\\site-packages\\matplotlib\\figure.py:402: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure\n", + " \"matplotlib is currently using a non-GUI backend, \"\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEKCAYAAAA8QgPpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXm8ZFV177+rpjt2Q0+A0kAjIJMgQos4owTBATBOzzEx\nqHwy+GJe4hg/aszgJ9HExCTGBCVx4mmMxpjniCiKRkEBFUFBEUGbqZuGbrrq9q1xvz/OUKfqnmGf\numfvfUvO7/O5n1u3qk6ddXeds9davzWJUooSJUqUKFEiLyquBShRokSJEtOJUoGUKFGiRImJUCqQ\nEiVKlCgxEUoFUqJEiRIlJkKpQEqUKFGixEQoFUiJEiVKlJgIpQIpUaJEiRIToVQgJUqUKFFiIpQK\npESJEiVKTISaawHyYPPmzWrbtm2uxShRokSJqcK11157r1JqS9GfO1UKZNu2bVxzzTWuxShRokSJ\nqYKI3G7ic0sKq0SJEiVKTIRSgZQoUaJEiYlQKpASJUqUKDERpioGEodut8uOHTtYXl52LcqvBGZn\nZ9m6dSv1et21KCVKlFjjmHoFsmPHDtatW8e2bdsQEdfiTDWUUuzevZsdO3Zw5JFHuhanRIkS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iKUCqREiRIlSkyEshK9xK80RCRIcQQ4BOgDu/y/l5RSjyv4fNuB31BK/X6Rn1uixFpEmcZb\n4kEDEfkToKmU+mvXspQo8auAksIq8aCFiDT932eKyNdF5BMi8hMR+UsReYk/k+OHInKU/74tIvIp\nEfmu//P4mM88MzKz4k/Em/nyNRG5VURivRIRaYrIX4g3/+MqETnYf/75InKD//yV5laiRInJUCqQ\nEiU8PBJ4DXAS8DLg4Uqp0/FaiP9v/z3vAf5WKfVo4Ln+a1k4Dq999unA2/x+X+NYAK5SSj0SuBJ4\nlf/8W4Fz/OfPn+i/KlHCIMoYSIkSHr6r/JbYIvIz4DL/+R8CT/Ef/xpwQqRD8HoRWVTD2R1x+JxS\nqg20RWQnXqvtHWPv6QCf9R9fC5ztP/4f4IMi8gngPylRYo2hVCAlSnhoRx4PIn8PGN4nFeAMpdTy\nhJ/bJ/6e66phMDJ8j1Lqt0XkMXhN8b4vIqcopXbnOHeJEkZRUlglSujjMoZ0FiJyismTichRSqmr\nlVJvBe5ltAV3iRLOUSqQEiX08fvAdhG5XkR+BPy24fO9yw/i34AXG/mB4fOVKJELZRpviRIlSpSY\nCKUHUqJEiRIlJkKpQEqUKFGixEQoFUiJEiVKlJgIpQIpUaJEiRIToVQgJUqUKFFiIpQKpESJEiVK\nTIRSgZQoUaJEiYlQKpASJUqUKDER/j8j1z09Cze3OwAAAABJRU5ErkJggg==\n", + "text/plain": [ + "" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "example_parameters = dict(M_X=0, M_Y=0,\n", + " O_X=4, O_Y=-4,\n", + " S_init_X=-3, S_init_Y=-0.3,\n", + " ramp_speed_X=1/180,\n", + " ST_plus_X=0.4, ST_plus_Y=-0.4,\n", + " ST_plus_width_X=0.2, ST_plus_width_Y=0.2,\n", + " t_meas=1000,\n", + " t_init=200,\n", + " total_duration=5000,\n", + " N=10, fid_step=100\n", + " )\n", + " \n", + "plot(fid_ramp, example_parameters)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "'@start','@fill','@wait','@reload','@adprep','@wait','@adread','@latch_readout'\n", + "\n", + "\n", + "\n", + "\n", + "t_ST_jump = (ST_plus_begin_X - M_X)/ramp_speed_X\n", + "t_op = t_ST_jump + (O_X - ST_plus_end_X)/ramp_speed_X\n", + "adprep = from_entry_list([(0, M_X, M_Y),\n", + " (t_ST_jump, ST_plus_begin_X, ST_plus_begin_Y, 'linear'),\n", + " (t_ST_jump, ST_plus_end_X, ST_plus_end_Y),\n", + " (t_op, O_X, O_Y, 'linear')],\n", + " \n", + " parameter_constraints=[M_X < ST_plus_begin_X, ST_plus_end_X < O_X],\n", + " identifier='adprep')\n", + "\n", + "t_ST_jump = (O_X - ST_plus_end_X)/ramp_speed_X\n", + "t_meas_start = t_ST_jump + (ST_plus_begin_X - M_X)/ramp_speed_X\n", + "adread = from_entry_list([(0, O_X, O_Y),\n", + " (t_ST_jump, ST_plus_end_X, ST_plus_end_Y, 'linear'),\n", + " (t_ST_jump, ST_plus_begin_X, ST_plus_begin_Y),\n", + " (t_meas_start, M_X, M_Y, 'linear')],\n", + " parameter_constraints=[M_X < ST_plus_begin_X, ST_plus_end_X < O_X],\n", + " identifier='adread')\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from qctoolkit.expressions import Expression\n", + "\n", + "class PointPulseTemplate(TablePulseTemplate):\n", + " def __init__(self, time_point_tuple_list, channel_names, **kwargs):\n", + " \n", + " self._time_point_tuple_list = []\n", + " self._ordered_channels = channel_names\n", + " entries = {channel_name: [] for channel_name in channel_names}\n", + " \n", + " for time_point_tuple in time_point_tuple_list:\n", + " if len(time_point_tuple) == 2:\n", + " time, point, interpolation = time_point_tuple, 'hold'\n", + " else:\n", + " time, point, interpolation = time_point_tuple\n", + " \n", + " if isinstance(point, str):\n", + " point = sp.IndexedBase(point, shape=(len(channel_names), ))\n", + " for ch_i, channel_name in enumerate(channel_names):\n", + " entries[channel_name].append((time, Expression(point[ch_i]), interpolation))\n", + " self._time_point_tuple_list.append((time, point, interpolation))\n", + " \n", + " super().__init__(entries=entries, **kwargs)\n", + " \n", + " @property\n", + " def time_point_tuple_list(self):\n", + " return self._time_point_tuple_list\n", + " \n", + " def get_serialization_data(self, serializer):\n", + " super_data = super().get_serialization_data(serializer)\n", + " super_data.pop('entries')\n", + " super_data['time_point_tuple_list'] = [(time, str(point), str(interp))\n", + " for time, point, interp in self.time_point_tuple_list]\n", + " super_data['channel_names'] = self._ordered_channels\n", + " return super_data\n", + " \n", + " @staticmethod\n", + " def deserialize(serializer, time_point_tuple_list, channel_names, **kwargs):\n", + " PointPulseTemplate(time_point_tuple_list=time_point_tuple_list,\n", + " channel_names=channel_names, **kwargs)\n", + " \n", + " \n", + "\n", + "ppt = PointPulseTemplate([(0, 'S_init', 'hold'),\n", + " ('t_init', 'M', 'jump'),\n", + " ('t_2', 'O', 'linear')],\n", + " ['RFX', 'RFY'])\n", + "\n", + "class MemoryBackend(qctoolkit.serialization.StorageBackend):\n", + " def __init__(self):\n", + " self.storage = dict()\n", + " \n", + " def put(self, identifier, data, overwrite):\n", + " self.storage[identifier] = data\n", + "\n", + " def get(self, identifier):\n", + " return self.storage[identifier]\n", + " \n", + " def exists(self, identifier):\n", + " return identifier in self.storage\n", + "\n", + "import qctoolkit.serialization\n", + "be = MemoryBackend()\n", + "s = qctoolkit.serialization.Serializer(be)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "d = ppt.get_serialization_data(None)\n", + "\n", + "s.serialize(ppt)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "ppt2 = s.deserialize('main')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def debug(f, *args, **kwargs):\n", + " import traceback, sys, code\n", + " try:\n", + " f(*args, **kwargs)\n", + " except:\n", + " type, value, tb = sys.exc_info()\n", + " traceback.print_exc()\n", + " last_frame = lambda tb=tb: last_frame(tb.tb_next) if tb.tb_next else tb\n", + " frame = last_frame().tb_frame\n", + " ns = dict(frame.f_globals)\n", + " ns.update(frame.f_locals)\n", + " code.interact(local=ns)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "lab_master", + "language": "python", + "name": "lab_master" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/doc/source/examples/serialized_pulses/sequence_referenced.json b/doc/source/examples/serialized_pulses/sequence_referenced.json index 4114958fe..02cfe904f 100644 --- a/doc/source/examples/serialized_pulses/sequence_referenced.json +++ b/doc/source/examples/serialized_pulses/sequence_referenced.json @@ -1,31 +1,20 @@ { - "external_parameters": [], - "is_interruptable": true, + "parameter_constraints": [], "subtemplates": [ { - "mappings": { - "ta": { - "expression": "1", - "type": "qctoolkit.expressions.Expression" - }, - "tb": { - "expression": "2", - "type": "qctoolkit.expressions.Expression" - }, - "tend": { - "expression": "5", - "type": "qctoolkit.expressions.Expression" - }, - "va": { - "expression": "5", - "type": "qctoolkit.expressions.Expression" - }, - "vb": { - "expression": "0", - "type": "qctoolkit.expressions.Expression" - } + "channel_mapping": { + "A": "A" }, - "template": "table_template" + "measurement_mapping": {}, + "parameter_mapping": { + "ta": "1", + "tb": "2", + "tend": "5", + "va": "5", + "vb": "0" + }, + "template": "table_template", + "type": "qctoolkit.pulses.pulse_template_parameter_mapping.MappingTemplate" } ], "type": "qctoolkit.pulses.sequence_pulse_template.SequencePulseTemplate" diff --git a/doc/source/examples/serialized_pulses/table_template.json b/doc/source/examples/serialized_pulses/table_template.json index 3962d03e1..8c56b3550 100644 --- a/doc/source/examples/serialized_pulses/table_template.json +++ b/doc/source/examples/serialized_pulses/table_template.json @@ -1,11 +1,6 @@ { - "entries": [ - [ - [ - 0, - 0, - "hold" - ], + "entries": { + "A": [ [ "ta", "va", @@ -22,46 +17,8 @@ "jump" ] ] - ], - "is_measurement_pulse": false, - "time_parameter_declarations": [ - { - "default_value": null, - "max_value": "tb", - "min_value": 0, - "name": "ta", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": "tend", - "min_value": "ta", - "name": "tb", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": Infinity, - "min_value": "tb", - "name": "tend", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ], - "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate", - "voltage_parameter_declarations": [ - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "va", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "vb", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" } \ No newline at end of file From e414a3503d6e89543136023f3a7d7d0ac9509200 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 12 Jul 2017 10:30:57 +0200 Subject: [PATCH 062/116] Bug fixes: Mainly not updated function signatures --- qctoolkit/pulses/conditions.py | 28 ++++++++++---- qctoolkit/pulses/loop_pulse_template.py | 4 +- qctoolkit/pulses/sequence_pulse_template.py | 8 +++- tests/pulses/sequencing_dummies.py | 5 ++- tests/pulses/sequencing_tests.py | 42 ++++++++++----------- 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/qctoolkit/pulses/conditions.py b/qctoolkit/pulses/conditions.py index fbdf1e969..c2ba8e9d6 100644 --- a/qctoolkit/pulses/conditions.py +++ b/qctoolkit/pulses/conditions.py @@ -134,7 +134,9 @@ def build_sequence_loop(self, len(instruction_block.instructions)) instruction_block.add_instruction_cjmp(self.__trigger, body_block) - sequencer.push(body, parameters, conditions, measurement_mapping, channel_mapping, body_block) + sequencer.push(body, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=body_block) def build_sequence_branch(self, delegator: SequencingElement, @@ -150,10 +152,14 @@ def build_sequence_branch(self, else_block = InstructionBlock() instruction_block.add_instruction_cjmp(self.__trigger, if_block) - sequencer.push(if_branch, parameters, conditions, measurement_mapping, channel_mapping, if_block) + sequencer.push(if_branch, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=if_block) instruction_block.add_instruction_goto(else_block) - sequencer.push(else_branch, parameters, conditions, measurement_mapping, channel_mapping, else_block) + sequencer.push(else_branch, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=else_block) if_block.return_ip = InstructionPointer(instruction_block, len(instruction_block.instructions)) @@ -208,8 +214,12 @@ def build_sequence_loop(self, if evaluation_result is None: raise ConditionEvaluationException() if evaluation_result is True: - sequencer.push(delegator, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) - sequencer.push(body, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) + sequencer.push(delegator, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=instruction_block) + sequencer.push(body, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=instruction_block) self.__loop_iteration += 1 # next time, evaluate for next iteration def build_sequence_branch(self, @@ -227,9 +237,13 @@ def build_sequence_branch(self, if evaluation_result is None: raise ConditionEvaluationException() if evaluation_result is True: - sequencer.push(if_branch, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) + sequencer.push(if_branch, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=instruction_block) else: - sequencer.push(else_branch, parameters, conditions, measurement_mapping, channel_mapping, instruction_block) + sequencer.push(else_branch, parameters, conditions, window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=instruction_block) class ConditionEvaluationException(Exception): diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index f44a08a18..db7070f2f 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -160,9 +160,9 @@ def build_sequence(self, sequencer.push(self.body, parameters=local_parameters, conditions=conditions, - measurement_mapping=measurement_mapping, + window_mapping=measurement_mapping, channel_mapping=channel_mapping, - instruction_block=instruction_block) + target_block=instruction_block) def build_waveform(self, parameters: Dict[str, Parameter]) -> ForLoopWaveform: return ForLoopWaveform([self.body.build_waveform(local_parameters) diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index b1b1a1f9c..f2c48df21 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -206,8 +206,12 @@ def build_sequence(self, channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: for subtemplate in reversed(self.subtemplates): - sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, - instruction_block) + sequencer.push(subtemplate, + parameters=parameters, + conditions=conditions, + window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=instruction_block) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index 177da4a0f..a8d4ac02b 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -88,7 +88,10 @@ def build_sequence(self, self.channel_mapping = channel_mapping if self.push_elements is not None: for element in self.push_elements[1]: - sequencer.push(element, parameters, conditions, measurement_mapping, channel_mapping, self.push_elements[0]) + sequencer.push(element, parameters, conditions, + window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=self.push_elements[0]) def requires_stop(self, parameters: Dict[str, Parameter], conditions: Dict[str, 'Conditions']) -> bool: self.requires_stop_call_counter += 1 diff --git a/tests/pulses/sequencing_tests.py b/tests/pulses/sequencing_tests.py index 18e8b0f43..6f98bc067 100644 --- a/tests/pulses/sequencing_tests.py +++ b/tests/pulses/sequencing_tests.py @@ -21,7 +21,7 @@ def test_push(self) -> None: wm = {'foo' : 'bar'} elem = DummySequencingElement() - sequencer.push(elem, ps, cs, wm) + sequencer.push(elem, ps, cs, window_mapping=wm) self.assertFalse(sequencer.has_finished()) sequencer.build() self.assertEqual(ps, elem.parameters) @@ -80,7 +80,7 @@ def test_build_path_o1_m2_i1_f_i0_one_element_custom_block_requires_stop(self) - cs = {'foo': DummyCondition()} wm = {} target_block = InstructionBlock() - sequencer.push(elem, ps, cs, wm, target_block) + sequencer.push(elem, ps, cs, window_mapping=wm, target_block=target_block) sequencer.build() self.assertFalse(sequencer.has_finished()) @@ -97,11 +97,11 @@ def test_build_path_o1_m2_i1_f_i1_f_one_element_custom_and_main_block_requires_s wm = {} cm = {} elem_main = DummySequencingElement(True) - sequencer.push(elem_main, ps, cs, cm) + sequencer.push(elem_main, ps, cs, channel_mapping=cm) elem_cstm = DummySequencingElement(True) target_block = InstructionBlock() - sequencer.push(elem_cstm, ps, cs, wm, cm, target_block) + sequencer.push(elem_cstm, ps, cs, window_mapping=wm, channel_mapping=cm, target_block=target_block) sequencer.build() @@ -256,7 +256,7 @@ def test_build_path_o2_m2_i0_i1_t_m2_i0_i0_one_element_custom_block(self) -> Non target_block = InstructionBlock() elem = DummySequencingElement(False) - sequencer.push(elem, ps, cs, wm, cm, target_block=target_block) + sequencer.push(elem, ps, cs, window_mapping=wm, channel_mapping=cm, target_block=target_block) sequencer.build() @@ -334,10 +334,10 @@ def test_build_path_o2_m2_i0_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_req target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) sequencer.build() @@ -361,10 +361,10 @@ def test_build_path_o2_m2_i0_i2_tt_m2_i0_i0_two_elements_custom_block(self) -> N target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) sequencer.build() @@ -389,10 +389,10 @@ def test_build_path_o2_m2_i1_f_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_last target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main = DummySequencingElement(True) sequencer.push(elem_main, ps, cs) @@ -423,10 +423,10 @@ def test_build_path_o2_m2_i1_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_last_r target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) @@ -460,10 +460,10 @@ def test_build_path_o2_m2_i1_t_i2_tt_m2_i0_i0_two_elements_custom_block_one_elem target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main = DummySequencingElement(False) sequencer.push(elem_main, ps, cs) @@ -498,10 +498,10 @@ def test_build_path_o2_m2_i2_tf_t_i2_tf_m2_i1_f_i1_f_two_elements_custom_block_l target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main2 = DummySequencingElement(True) sequencer.push(elem_main2, ps, cs) @@ -543,10 +543,10 @@ def test_build_path_o2_m2_i2_tt_t_i2_tf_m2_i0_i1_f_two_elements_custom_block_las target_block = InstructionBlock() elem2 = DummySequencingElement(True) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) @@ -588,10 +588,10 @@ def test_build_path_o2_m2_i2_tt_t_i2_tt_m2_i0_i0_two_elements_custom_block_two_e target_block = InstructionBlock() elem2 = DummySequencingElement(False) - sequencer.push(elem2, ps, cs, wm, target_block=target_block) + sequencer.push(elem2, ps, cs, window_mapping=wm, target_block=target_block) elem1 = DummySequencingElement(False) - sequencer.push(elem1, ps, cs, wm, target_block=target_block) + sequencer.push(elem1, ps, cs, window_mapping=wm, target_block=target_block) elem_main2 = DummySequencingElement(False) sequencer.push(elem_main2, ps, cs) From 6d1ffb37a5c4f238065f81c839682410a1f684d5 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Tue, 1 Aug 2017 16:28:15 +0200 Subject: [PATCH 063/116] save all: PointPulseTemplate other stuff --- doc/source/examples/04Sequencing.ipynb | 1627 ++++++++++++++++- doc/source/examples/05Parameters.ipynb | 835 +-------- .../examples/06ConditionalExecution.ipynb | 25 +- doc/source/examples/08RealWorldCase.ipynb | 1088 +++++++++-- .../09DetailedSequencingWalkthrough.ipynb | 2 +- .../examples/10MultiChannelTemplates.ipynb | 195 +- .../examples/FreeInductionDecayExample.ipynb | 116 +- .../examples/serialized_pulses/main.json | 55 +- .../serialized_pulses/sequence_embedded.json | 92 +- .../serialized_pulses/stored_template.json | 29 +- qctoolkit/pulses/measurement.py | 6 +- .../pulse_template_parameter_mapping.py | 11 +- qctoolkit/pulses/sequence_pulse_template.py | 2 +- qctoolkit/pulses/sequencing.py | 12 +- qctoolkit/pulses/table_pulse_template.py | 110 +- tests/pulses/table_pulse_template_tests.py | 42 +- 16 files changed, 2914 insertions(+), 1333 deletions(-) diff --git a/doc/source/examples/04Sequencing.ipynb b/doc/source/examples/04Sequencing.ipynb index 06846a7ad..2d61f0d45 100644 --- a/doc/source/examples/04Sequencing.ipynb +++ b/doc/source/examples/04Sequencing.ipynb @@ -19,27 +19,1593 @@ }, "outputs": [ { - "ename": "TypeError", - "evalue": "__init__() missing 1 required positional argument: 'entries'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mqctoolkit\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpulses\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mTablePulseTemplate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mFunctionPulseTemplate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSequencePulseTemplate\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mplot\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mtable_template\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTablePulseTemplate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[0mtable_template\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_entry\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ta'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'va'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minterpolation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'hold'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mtable_template\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_entry\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'tb'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'vb'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minterpolation\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'linear'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mTypeError\u001b[0m: __init__() missing 1 required positional argument: 'entries'" - ] + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" } ], "source": [ "%matplotlib notebook\n", "from qctoolkit.pulses import TablePulseTemplate, FunctionPulseTemplate, SequencePulseTemplate, plot\n", "\n", - "table_template = TablePulseTemplate()\n", - "table_template.add_entry('ta', 'va', interpolation='hold')\n", - "table_template.add_entry('tb', 'vb', interpolation='linear')\n", - "table_template.add_entry('tend', 0, interpolation='jump')\n", + "table_template = TablePulseTemplate({'A': [('ta', 'va', 'hold'),\n", + " ('tb', 'vb', 'linear'),\n", + " ('tend', 0, 'jump')]})\n", "\n", - "function_template = FunctionPulseTemplate('exp(-t/lambda)*sin(phi*t)', 'duration')\n", + "function_template = FunctionPulseTemplate('exp(-t/tau)*sin(phi*t)', 'duration', channel='A')\n", "\n", "table_parameter_mapping = {\n", " 'ta': 'ta',\n", @@ -49,16 +1615,16 @@ " 'vb': '0'\n", "}\n", "function_parameter_mapping = {\n", - " 'lambda': 'lambda',\n", + " 'tau': 'tau',\n", " 'phi': 'phi',\n", " 'duration': 'duration'\n", "}\n", - "sequence_template = SequencePulseTemplate([(table_template, table_parameter_mapping),\n", - " (function_template, function_parameter_mapping)],\n", - " {'ta', 'duration', 'va', 'lambda', 'phi'})\n", + "sequence_template = SequencePulseTemplate((table_template, table_parameter_mapping),\n", + " (function_template, function_parameter_mapping),\n", + " external_parameters={'ta', 'duration', 'va', 'tau', 'phi'})\n", "\n", "\n", - "plot(sequence_template, {'lambda': 4, 'phi': 8, 'duration': 4*3.1415, 'ta': 1, 'va': 2}, sample_rate=100)" + "plot(sequence_template, {'tau': 4, 'phi': 8, 'duration': 4*3.1415, 'ta': 1, 'va': 2}, sample_rate=100)" ] }, { @@ -71,17 +1637,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[, , ]\n" + ] + } + ], "source": [ "from qctoolkit.pulses import Sequencer\n", "\n", "sequencer = Sequencer()\n", "\n", - "sequencer.push(sequence_template, {'lambda': 4, 'phi': 8, 'duration': 4*3.1415, 'ta': 1, 'va': 2})\n", + "sequencer.push(sequence_template, {'tau': 4, 'phi': 8, 'duration': 4*3.1415, 'ta': 1, 'va': 2})\n", "instruction_block = sequencer.build()\n", "print([instruction for instruction in instruction_block])" ] @@ -112,15 +1686,6 @@ "The latter two will only be generated when hardware-based conditional branching is used in `LoopPulseTemplate` and `BranchPulseTemplate`. This was the main motivator for the usage of such an instruction sequence rather than just compling one large waveform. The `REPJInstruction` results from the usage of the `RepetitionPulseTemplate`.\n", "Using only the `PulseTemplate`s discussed so far, only `EXECInstruction`s and a final `STOPInstruction` will be output by the `Sequencer`." ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/doc/source/examples/05Parameters.ipynb b/doc/source/examples/05Parameters.ipynb index f95e78a23..dcf68ca24 100644 --- a/doc/source/examples/05Parameters.ipynb +++ b/doc/source/examples/05Parameters.ipynb @@ -21,791 +21,25 @@ }, "outputs": [ { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -204,6 +991,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -273,6 +1063,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -329,8 +1128,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -463,10 +1263,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -622,8 +1422,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -685,7 +1485,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -744,6 +1544,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -752,7 +1553,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -763,8 +1564,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -853,12 +1655,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -907,7 +1706,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -922,6 +1721,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -980,6 +1780,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -1049,6 +1852,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1105,8 +1917,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -1239,10 +2052,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -1398,8 +2211,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -1461,7 +2274,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -1520,6 +2333,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -1528,7 +2342,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -1539,8 +2353,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -1629,12 +2444,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -1683,7 +2495,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1698,6 +2510,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -1756,6 +2569,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -1825,6 +2641,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1881,8 +2706,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -2015,10 +2841,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -2174,8 +3000,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -2237,7 +3063,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -2296,6 +3122,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -2304,7 +3131,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -2315,8 +3142,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -2405,12 +3233,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -2459,7 +3284,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -2491,25 +3316,20 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# stub for an initialization pulse of length 4\n", - "init = TablePulseTemplate()\n", - "init.add_entry(0, 5)\n", - "init.add_entry(4, 0, 'linear')\n", + "init = TablePulseTemplate({0: [(0, 5), (4, 0, 'linear')]})\n", "\n", "# stub for a measurement pulse of length 12\n", - "measure = TablePulseTemplate()\n", - "measure.add_entry(0, 0)\n", - "measure.add_entry(12, 5, 'linear')\n", + "measure = TablePulseTemplate({0: [(0, 0), (12, 5, 'linear')]})\n", "\n", "# a wating pulse\n", - "wait = TablePulseTemplate()\n", - "wait.add_entry('wait_duration', 0)" + "wait = TablePulseTemplate({0: [(0, 0), ('wait_duration', 0)]})" ] }, { @@ -2521,7 +3341,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": { "collapsed": false }, @@ -2557,7 +3377,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -2566,29 +3386,12 @@ "# an identity mapping for all epsilons\n", "all_epsilons_map = {param_name: param_name for param_name in all_epsilons}\n", "\n", - "final_sequences = []\n", - "for k in range(0, len(sequences)):\n", - " \n", - " #prepare the subtemplate of the sequence S_k'\n", - " subtemplates = []\n", - " \n", - " # include the wait pulse first. pass in the appropriate wait time.\n", - " # note that the wait time has to be cast to a string. In parameter mappings, some string containing a mathematical\n", - " # expression is expected. Here, we provide a mathematical expression consisting only of a constant value.\n", - " subtemplates.append((wait, {'wait_duration': str(wait_times[k])}))\n", - " \n", - " # append the init pulse\n", - " subtemplates.append((init, {}))\n", - " \n", - " # append the k-th sequence\n", - " subtemplates.append((sequences[k], all_epsilons_map))\n", - " \n", - " # append the measuring\n", - " subtemplates.append((measure, {}))\n", - " \n", - " # construct the object for S_k'\n", - " s_k_prime = SequencePulseTemplate(subtemplates, all_epsilons)\n", - " final_sequences.append(s_k_prime)" + "gates_with_init_and_readout = [SequencePulseTemplate((wait, {'wait_duration': wait_duration}),\n", + " init,\n", + " gate,\n", + " measure)\n", + " for gate, wait_duration in zip(sequences, wait_times)]\n", + "final_sequence = SequencePulseTemplate(*gates_with_init_and_readout)" ] }, { @@ -2600,7 +3403,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -2611,6 +3414,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -2669,6 +3473,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -2738,6 +3545,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -2794,8 +3610,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -2928,10 +3745,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -3087,8 +3904,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -3150,7 +3967,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -3209,6 +4026,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -3217,7 +4035,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -3228,8 +4046,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -3318,12 +4137,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -3372,7 +4188,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -3383,7 +4199,7 @@ } ], "source": [ - "plot(final_sequences[1], parameters)" + "pl = plot(final_sequence.subtemplates[1], parameters)" ] }, { @@ -3392,41 +4208,13 @@ "source": [ "Finally, we construct a single scanline which just repeats all three sequences over and over again. Since our $S_k'$ are short, we just build a scanline with a duration of 0.6 microseconds. With a duration for each $S_k'$ of 200 ns, we can fit 1'000 repetitions of $S_1' | S_2' | S_3'$ in our scanline. " ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "from qctoolkit.pulses import Sequencer\n", - "\n", - "subtemplates = []\n", - "for i in range(0, 1000):\n", - " subtemplates.append((final_sequences[0], all_epsilons_map))\n", - " subtemplates.append((final_sequences[1], all_epsilons_map))\n", - " subtemplates.append((final_sequences[2], all_epsilons_map))\n", - " \n", - "scanline = SequencePulseTemplate(subtemplates, all_epsilons)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "lab_master", "language": "python", - "name": "python3" + "name": "lab_master" }, "language_info": { "codemirror_mode": { @@ -3438,7 +4226,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.3" } }, "nbformat": 4, diff --git a/doc/source/examples/09DetailedSequencingWalkthrough.ipynb b/doc/source/examples/09DetailedSequencingWalkthrough.ipynb index 586d33101..a8ac0308f 100644 --- a/doc/source/examples/09DetailedSequencingWalkthrough.ipynb +++ b/doc/source/examples/09DetailedSequencingWalkthrough.ipynb @@ -869,7 +869,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.6.0" } }, "nbformat": 4, diff --git a/doc/source/examples/10MultiChannelTemplates.ipynb b/doc/source/examples/10MultiChannelTemplates.ipynb index 120bf18db..646b06115 100644 --- a/doc/source/examples/10MultiChannelTemplates.ipynb +++ b/doc/source/examples/10MultiChannelTemplates.ipynb @@ -25,6 +25,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -83,6 +84,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -152,6 +156,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -208,8 +221,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -342,10 +356,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -501,8 +515,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -564,7 +578,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -623,6 +637,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -631,7 +646,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -642,8 +657,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -732,12 +748,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -786,7 +799,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -806,14 +819,14 @@ "source": [ "from qctoolkit.pulses import TablePulseTemplate\n", "\n", - "table_template = TablePulseTemplate(channels=2, identifier='2-channel-table-template')\n", - "# set up entries for channel 0\n", - "table_template.add_entry(1, 4, channel=0)\n", - "table_template.add_entry('foo', 'bar', channel=0)\n", - "table_template.add_entry(10, 0, channel=0)\n", - "# set up entries for channel 1\n", - "table_template.add_entry('foo', 2.7, interpolation='linear', channel=1)\n", - "table_template.add_entry(9, 'bar', interpolation='linear', channel=1)\n", + "table_template = TablePulseTemplate(identifier='2-channel-table-template',\n", + " entries={0: [(0, 0),\n", + " (1, 4),\n", + " ('foo', 'bar'),\n", + " (10, 0)],\n", + " 1: [(0, 0),\n", + " ('foo', 2.7, 'linear'),\n", + " (9, 'bar', 'linear')]})\n", "\n", "# plot it\n", "%matplotlib notebook\n", @@ -822,7 +835,7 @@ " foo=7,\n", " bar=-1.3\n", ")\n", - "plot(table_template, parameters, sample_rate=100)\n", + "_ = plot(table_template, parameters, sample_rate=100)\n", "print(\"The number of channels in table_template is {}.\".format(table_template.num_channels))" ] }, @@ -850,6 +863,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -908,6 +922,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -977,6 +994,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1033,8 +1059,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -1167,10 +1194,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -1326,8 +1353,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -1389,7 +1416,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -1448,6 +1475,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -1456,7 +1484,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -1467,8 +1495,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -1557,12 +1586,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -1611,7 +1637,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1632,16 +1658,16 @@ "source": [ "from qctoolkit.pulses import FunctionPulseTemplate, MultiChannelPulseTemplate\n", "\n", - "function_template = FunctionPulseTemplate('-sin(t)**2', '10', identifier='function-template')\n", + "function_template = FunctionPulseTemplate('-sin(t)**2', '10', identifier='function-template', channel='chan_A')\n", "\n", "template = MultiChannelPulseTemplate(\n", - " [(function_template, dict(), [1]),\n", - " (table_template, dict(foo='5', bar='2 * hugo'), [2, 0])],\n", + " [(function_template, dict(), {'chan_A': 1}),\n", + " (table_template, dict(foo='5', bar='2 * hugo'), {0: 'rectangle', 1: 'triangle'})],\n", " {'hugo'},\n", " identifier='3-channel-combined-template'\n", ")\n", "\n", - "plot(template, dict(hugo=-1.3), sample_rate=100)\n", + "_ = plot(template, dict(hugo=-1.3), sample_rate=100)\n", "print(\"The number of channels in function_template is {}.\".format(function_template.num_channels))\n", "print(\"The number of channels in template is {}.\".format(template.num_channels))" ] @@ -1675,6 +1701,7 @@ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", + "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", @@ -1733,6 +1760,9 @@ " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", @@ -1802,6 +1832,15 @@ " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", @@ -1858,8 +1897,9 @@ " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", - " canvas.attr('width', width);\n", - " canvas.attr('height', height);\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", @@ -1992,10 +2032,10 @@ "}\n", "\n", "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n", - " var x0 = msg['x0'];\n", - " var y0 = fig.canvas.height - msg['y0'];\n", - " var x1 = msg['x1'];\n", - " var y1 = fig.canvas.height - msg['y1'];\n", + " var x0 = msg['x0'] / mpl.ratio;\n", + " var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n", + " var x1 = msg['x1'] / mpl.ratio;\n", + " var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n", " x0 = Math.floor(x0) + 0.5;\n", " y0 = Math.floor(y0) + 0.5;\n", " x1 = Math.floor(x1) + 0.5;\n", @@ -2151,8 +2191,8 @@ " this.canvas_div.focus();\n", " }\n", "\n", - " var x = canvas_pos.x;\n", - " var y = canvas_pos.y;\n", + " var x = canvas_pos.x * mpl.ratio;\n", + " var y = canvas_pos.y * mpl.ratio;\n", "\n", " this.send_message(name, {x: x, y: y, button: event.button,\n", " step: event.step,\n", @@ -2214,7 +2254,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -2273,6 +2313,7 @@ "};\n", "\n", "mpl.figure.prototype.handle_close = function(fig, msg) {\n", + " var width = fig.canvas.width/mpl.ratio\n", " fig.root.unbind('remove')\n", "\n", " // Update the output cell to use the data from the current canvas.\n", @@ -2281,7 +2322,7 @@ " // Re-enable the keyboard manager in IPython - without this line, in FF,\n", " // the notebook keyboard shortcuts fail.\n", " IPython.keyboard_manager.enable()\n", - " $(fig.parent_element).html('');\n", + " $(fig.parent_element).html('');\n", " fig.close_ws(fig, msg);\n", "}\n", "\n", @@ -2292,8 +2333,9 @@ "\n", "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n", " // Turn the data on the canvas into data in the output cell.\n", + " var width = this.canvas.width/mpl.ratio\n", " var dataURL = this.canvas.toDataURL();\n", - " this.cell_info[1]['text/html'] = '';\n", + " this.cell_info[1]['text/html'] = '';\n", "}\n", "\n", "mpl.figure.prototype.updated_canvas_event = function() {\n", @@ -2382,12 +2424,9 @@ " // Check for shift+enter\n", " if (event.shiftKey && event.which == 13) {\n", " this.canvas_div.blur();\n", - " event.shiftKey = false;\n", - " // Send a \"J\" for go to next cell\n", - " event.which = 74;\n", - " event.keyCode = 74;\n", - " manager.command_mode();\n", - " manager.handle_keydown(event);\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", " }\n", "}\n", "\n", @@ -2436,7 +2475,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -2455,11 +2494,10 @@ ], "source": [ "from qctoolkit.pulses import SequencePulseTemplate\n", + "from qctoolkit.pulses import MappingTemplate\n", "\n", - "channel_swapped_table_template = MultiChannelPulseTemplate(\n", - " [(table_template, dict(foo='foo', bar='bar'), [1, 0])],\n", - " {'foo', 'bar'}\n", - ")\n", + "channel_swapped_table_template = MappingTemplate(template=table_template,\n", + " channel_mapping={0: 1, 1: 0})\n", "\n", "sequence_template = SequencePulseTemplate(\n", " [(table_template, dict(foo='1.2 * hugo', bar='hugo ** 2')),\n", @@ -2471,22 +2509,13 @@ "plot(sequence_template, dict(hugo=2), sample_rate=100)\n", "print(\"The number of channels in sequence_template is {}.\".format(sequence_template.num_channels))" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "lab_master", "language": "python", - "name": "python3" + "name": "lab_master" }, "language_info": { "codemirror_mode": { @@ -2498,7 +2527,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.1" + "version": "3.5.3" } }, "nbformat": 4, diff --git a/doc/source/examples/FreeInductionDecayExample.ipynb b/doc/source/examples/FreeInductionDecayExample.ipynb index 39f769ea6..669790197 100644 --- a/doc/source/examples/FreeInductionDecayExample.ipynb +++ b/doc/source/examples/FreeInductionDecayExample.ipynb @@ -3,9 +3,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from qctoolkit.pulses import TablePulseTemplate\n", @@ -94,9 +92,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stderr", @@ -110,7 +106,7 @@ "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAEWCAYAAABmE+CbAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAHRhJREFUeJzt3XuUXHWZ7vHvkxAMt8BAwCAhJDIgNCQ0WFwCekDuKhMY\nTwABReJBmBkiMDookuUoHBhQnEEuyoDGOI4JFwOccBQUEDjAAIEOBGI6SkST2DFICEcwYIDgO3/U\nrk6l6Ut112XvXfV81urVddm96011ut569u9Xv62IwMzMbFjaBZiZWTa4IZiZGeCGYGZmCTcEMzMD\n3BDMzCzhhmBmZoAbgtmgSHqfpIWS/iTpL5K+3M+2IemvG1mfWTU2SbsAs5z5AvBARLSnXYhZrTkh\nmA3OLsDitIswqwc3BLMKSbof+BBwnaS1kuZIurTs/gskrZL0e0mfTq9Ss6FxQzCrUEQcDjwMTI+I\nLYE3S/dJOhb4J+AoYDfgyFSKNKuCG4JZbZwEzIqIX0TEa8BXU67HbNDcEMxq4z3A78quL0+rELOh\nckMwq41VwM5l18elVYjZULkhmNXGrcAZktokbQ58Je2CzAbLDcGsBiLibuCbwP3Ar5PvZrkinyDH\nzMzACcHMzBJuCGZmBrghmJlZwg3BzMyAnK12Onr06Bg/fnzaZZiZ5cqCBQteiojtB9ouVw1h/Pjx\ndHR0pF2GmVmuSKrok/M+ZGRmZoAbgpmZJdwQzMwMyNkYgpll01tvvUVXVxfr1q1Lu5SWNnLkSMaO\nHcuIESOG9PNuCGZWta6uLrbaaivGjx+PpLTLaUkRwZo1a+jq6mLChAlD2ocPGZlZ1datW8d2223n\nZpAiSWy33XZVpTQ3BDOrCTeD9FX7O/AhI6uPjlmwaG599j1xKhSm1WffZi3MCcHqY9FceGFR7ff7\nwqL6NRprOmeccQZz56bz/2XZsmXsvffevd6+2Wab0d7eTltbG6effjpvvfUWAA8++CBbb7017e3t\ntLe3c+SRR3LvvfcyefJkSqcqePvtt9l333159NFHa16zE4LVVikZvLAIxkyEaT+p7f5nfbS471kf\ndVKw3Np1111ZuHAhb7/9NkcddRS33norp512GgAf/OAH+fGPf7zR9jNnzmTmzJmceeaZXHvttRQK\nBQ4++OCa1+WEYLVV3gwmTq39/idOLe7bScF6+MEPfsCkSZPYZ599+OQnP9l9+0MPPcTBBx/Me9/7\n3u60sHbtWo444gj2228/Jk6cyLx584Diu/c999yTz3zmM+y1114cffTR/PnPfwbgsMMO44tf/CIH\nHHAAu+++Ow8//DBQfMd+wQUXsP/++zNp0iRuuOGGimsePnw4BxxwACtXrux3u6uuuorLL7+cxYsX\nc9111/G1r31tUM9NpZwQrDbqnQxKCtOKX04KmXXx/11M5+9frek+294ziq/8zV593r948WIuvfRS\nHn30UUaPHs3LL7/cfd+qVat45JFH+OUvf8mUKVOYOnUqI0eO5I477mDUqFG89NJLHHTQQUyZMgWA\npUuXctNNN/Gd73yHk046idtuu41PfOITAKxfv54nnniCu+66i4svvpj77ruPmTNnsvXWW/Pkk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cHMzBJuCGZmBrghmJlZwg3BzMwANwQzM0v8N2sY\nRMIfsbiSAAAAAElFTkSuQmCC\n", "text/plain": [ - "" + "" ] }, "execution_count": 2, @@ -155,10 +151,8 @@ }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "S_init_X, S_init_Y = sp.symbols('S_init_X S_init_Y')\n", @@ -199,9 +193,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "t_ST_jump = (O_X - ST_plus_end_X)/ramp_speed_X\n", @@ -219,10 +211,8 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { @@ -230,7 +220,7 @@ "total_duration" ] }, - "execution_count": 12, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -252,9 +242,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -294,29 +282,24 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Users\\lablocal\\Anaconda3\\envs\\lab_master\\lib\\site-packages\\matplotlib\\figure.py:402: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure\n", - " \"matplotlib is currently using a non-GUI backend, \"\n" + "ename": "ParameterConstraintViolation", + "evalue": "The constraint 'ST_plus_Y + ST_plus_width_Y/2 >= M_Y' is not fulfilled.\nParameters: {'ST_plus_width_X': 0.2, 'ST_plus_Y': -0.4, 'S_init_X': -3.0, 'M_X': 0.0, 'ST_plus_width_Y': 0.2, 'ST_plus_X': 0.4, 'M_Y': 0.0, 'O_Y': -4.0, 'ramp_speed_X': 0.005555555555555556, 't_init': 200.0, 'O_X': 4.0, 'S_init_Y': -0.3}", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mParameterConstraintViolation\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 11\u001b[0m )\n\u001b[0;32m 12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m \u001b[0mplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfid_ramp\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mexample_parameters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\pulses\\plotting.py\u001b[0m in \u001b[0;36mplot\u001b[1;34m(pulse, parameters, sample_rate)\u001b[0m\n\u001b[0;32m 121\u001b[0m \u001b[0mchannel_mapping\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[0mch\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mch\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mch\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mchannels\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 122\u001b[0m window_mapping={w: w for w in pulse.measurement_names})\n\u001b[1;32m--> 123\u001b[1;33m \u001b[0msequence\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msequencer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbuild\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 124\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0msequencer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhas_finished\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 125\u001b[0m \u001b[1;32mraise\u001b[0m \u001b[0mPlottingNotPossibleException\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpulse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\pulses\\sequencing.py\u001b[0m in \u001b[0;36mbuild\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 191\u001b[0m \u001b[0msequencing_stack\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 192\u001b[0m element.build_sequence(self, parameters, conditions, window_mapping,\n\u001b[1;32m--> 193\u001b[1;33m channel_mapping, target_block)\n\u001b[0m\u001b[0;32m 194\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;32mbreak\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 195\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\pulses\\pulse_template.py\u001b[0m in \u001b[0;36mbuild_sequence\u001b[1;34m(self, sequencer, parameters, conditions, measurement_mapping, channel_mapping, instruction_block)\u001b[0m\n\u001b[0;32m 115\u001b[0m waveform = self.build_waveform(parameters,\n\u001b[0;32m 116\u001b[0m \u001b[0mmeasurement_mapping\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmeasurement_mapping\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 117\u001b[1;33m channel_mapping=channel_mapping)\n\u001b[0m\u001b[0;32m 118\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mwaveform\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 119\u001b[0m \u001b[0minstruction_block\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_instruction_exec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwaveform\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\pulses\\table_pulse_template.py\u001b[0m in \u001b[0;36mbuild_waveform\u001b[1;34m(self, parameters, measurement_mapping, channel_mapping)\u001b[0m\n\u001b[0;32m 330\u001b[0m \u001b[0mmeasurement_mapping\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mDict\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 331\u001b[0m channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']:\n\u001b[1;32m--> 332\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalidate_parameter_constraints\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparameters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 333\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 334\u001b[0m instantiated = [(channel_mapping[channel], instantiated_channel)\n", + "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\pulses\\parameters.py\u001b[0m in \u001b[0;36mvalidate_parameter_constraints\u001b[1;34m(self, parameters)\u001b[0m\n\u001b[0;32m 231\u001b[0m \u001b[0mconstraint_parameters\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mv\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mParameter\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32melse\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mparameters\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 232\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mconstraint\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_fulfilled\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconstraint_parameters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 233\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mParameterConstraintViolation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconstraint\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconstraint_parameters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 234\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 235\u001b[0m \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mParameterConstraintViolation\u001b[0m: The constraint 'ST_plus_Y + ST_plus_width_Y/2 >= M_Y' is not fulfilled.\nParameters: {'ST_plus_width_X': 0.2, 'ST_plus_Y': -0.4, 'S_init_X': -3.0, 'M_X': 0.0, 'ST_plus_width_Y': 0.2, 'ST_plus_X': 0.4, 'M_Y': 0.0, 'O_Y': -4.0, 'ramp_speed_X': 0.005555555555555556, 't_init': 200.0, 'O_X': 4.0, 'S_init_Y': -0.3}" ] - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZAAAAEKCAYAAAA8QgPpAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJztnXm8ZFV177+rpjt2Q0+A0kAjIJMgQos4owTBATBOzzEx\nqHwy+GJe4hg/aszgJ9HExCTGBCVx4mmMxpjniCiKRkEBFUFBEUGbqZuGbrrq9q1xvz/OUKfqnmGf\numfvfUvO7/O5n1u3qk6ddXeds9davzWJUooSJUqUKFEiLyquBShRokSJEtOJUoGUKFGiRImJUCqQ\nEiVKlCgxEUoFUqJEiRIlJkKpQEqUKFGixEQoFUiJEiVKlJgIpQIpUaJEiRIToVQgJUqUKFFiIpQK\npESJEiVKTISaawHyYPPmzWrbtm2uxShRokSJqcK11157r1JqS9GfO1UKZNu2bVxzzTWuxShRokSJ\nqYKI3G7ic0sKq0SJEiVKTIRSgZQoUaJEiYlQKpASJUqUKDERpioGEodut8uOHTtYXl52LcqvBGZn\nZ9m6dSv1et21KCVKlFjjmHoFsmPHDtatW8e2bdsQEdfiTDWUUuzevZsdO3Zw5JFHuhanRIkSaxxT\nT2EtLy+zadOmUnkUABFh06ZNpTdXokQJLUy9AgFK5VEgyrUsUaKELpwrEBGpisj3ROSzrmUpUaJE\niRL6cK5AgNcAP3YtRNF4+ctfzic/+Ukn577tttt4xCMeEfvahz70IY455hiOOeYYPvShD1mWrESJ\nEr9KcBpEF5GtwDOBvwD+cJLPG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rIX4g3/+MqETnYf/75InKD//yV5laiRInJUCqQ\nEiU8PBJ4DXAS8DLg4Uqp0/FaiP9v/z3vAf5WKfVo4Ln+a1k4Dq999unA2/x+X+NYAK5SSj0SuBJ4\nlf/8W4Fz/OfPn+i/KlHCIMoYSIkSHr6r/JbYIvIz4DL/+R8CT/Ef/xpwQqRD8HoRWVTD2R1x+JxS\nqg20RWQnXqvtHWPv6QCf9R9fC5ztP/4f4IMi8gngPylRYo2hVCAlSnhoRx4PIn8PGN4nFeAMpdTy\nhJ/bJ/6e66phMDJ8j1Lqt0XkMXhN8b4vIqcopXbnOHeJEkZRUlglSujjMoZ0FiJyismTichRSqmr\nlVJvBe5ltAV3iRLOUSqQEiX08fvAdhG5XkR+BPy24fO9yw/i34AXG/mB4fOVKJELZRpviRIlSpSY\nCKUHUqJEiRIlJkKpQEqUKFGixEQoFUiJEiVKlJgIpQIpUaJEiRIToVQgJUqUKFFiIpQKpESJEiVK\nTIRSgZQoUaJEiYlQKpASJUqUKDER/j8j1z09Cze3OwAAAABJRU5ErkJggg==\n", - "text/plain": [ - "" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ @@ -337,10 +320,8 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": { - "collapsed": false - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "'@start','@fill','@wait','@reload','@adprep','@wait','@adread','@latch_readout'\n", @@ -371,15 +352,15 @@ }, { "cell_type": "code", - "execution_count": 83, - "metadata": { - "collapsed": false - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "from qctoolkit.expressions import Expression\n", "\n", - "class PointPulseTemplate(TablePulseTemplate):\n", + "class IndexFunction\n", + "\n", + "class PointPulseTemplate(AtomicPulseTemplate):\n", " def __init__(self, time_point_tuple_list, channel_names, **kwargs):\n", " \n", " self._time_point_tuple_list = []\n", @@ -419,9 +400,9 @@ " \n", " \n", "\n", - "ppt = PointPulseTemplate([(0, 'S_init', 'hold'),\n", - " ('t_init', 'M', 'jump'),\n", - " ('t_2', 'O', 'linear')],\n", + "ppt = PointPulseTemplate([(0, 'S_init', 'hold'),\n", + " ('t_init', 'M', 'jump'),\n", + " ('t_2', 'O', 'linear')],\n", " ['RFX', 'RFY'])\n", "\n", "class MemoryBackend(qctoolkit.serialization.StorageBackend):\n", @@ -444,10 +425,8 @@ }, { "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": false - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "d = ppt.get_serialization_data(None)\n", @@ -457,10 +436,8 @@ }, { "cell_type": "code", - "execution_count": 85, - "metadata": { - "collapsed": false - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "ppt2 = s.deserialize('main')" @@ -468,10 +445,8 @@ }, { "cell_type": "code", - "execution_count": 17, - "metadata": { - "collapsed": false - }, + "execution_count": null, + "metadata": {}, "outputs": [], "source": [ "def debug(f, *args, **kwargs):\n", @@ -487,15 +462,6 @@ " ns.update(frame.f_locals)\n", " code.interact(local=ns)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/doc/source/examples/serialized_pulses/main.json b/doc/source/examples/serialized_pulses/main.json index 3962d03e1..8c56b3550 100644 --- a/doc/source/examples/serialized_pulses/main.json +++ b/doc/source/examples/serialized_pulses/main.json @@ -1,11 +1,6 @@ { - "entries": [ - [ - [ - 0, - 0, - "hold" - ], + "entries": { + "A": [ [ "ta", "va", @@ -22,46 +17,8 @@ "jump" ] ] - ], - "is_measurement_pulse": false, - "time_parameter_declarations": [ - { - "default_value": null, - "max_value": "tb", - "min_value": 0, - "name": "ta", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": "tend", - "min_value": "ta", - "name": "tb", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": Infinity, - "min_value": "tb", - "name": "tend", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ], - "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate", - "voltage_parameter_declarations": [ - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "va", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "vb", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" } \ No newline at end of file diff --git a/doc/source/examples/serialized_pulses/sequence_embedded.json b/doc/source/examples/serialized_pulses/sequence_embedded.json index 5fcf49640..f02a65a31 100644 --- a/doc/source/examples/serialized_pulses/sequence_embedded.json +++ b/doc/source/examples/serialized_pulses/sequence_embedded.json @@ -1,38 +1,21 @@ { - "external_parameters": [], - "is_interruptable": true, + "parameter_constraints": [], "subtemplates": [ { - "mappings": { - "ta": { - "expression": "1", - "type": "qctoolkit.expressions.Expression" - }, - "tb": { - "expression": "2", - "type": "qctoolkit.expressions.Expression" - }, - "tend": { - "expression": "5", - "type": "qctoolkit.expressions.Expression" - }, - "va": { - "expression": "5", - "type": "qctoolkit.expressions.Expression" - }, - "vb": { - "expression": "0", - "type": "qctoolkit.expressions.Expression" - } + "channel_mapping": { + "A": "A" + }, + "measurement_mapping": {}, + "parameter_mapping": { + "ta": "1", + "tb": "2", + "tend": "5", + "va": "5", + "vb": "0" }, "template": { - "entries": [ - [ - [ - 0, - 0, - "hold" - ], + "entries": { + "A": [ [ "ta", "va", @@ -49,49 +32,12 @@ "jump" ] ] - ], - "is_measurement_pulse": false, - "time_parameter_declarations": [ - { - "default_value": null, - "max_value": "tb", - "min_value": 0, - "name": "ta", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": "tend", - "min_value": "ta", - "name": "tb", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": Infinity, - "min_value": "tb", - "name": "tend", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ], - "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate", - "voltage_parameter_declarations": [ - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "va", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - }, - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "vb", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ] - } + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" + }, + "type": "qctoolkit.pulses.pulse_template_parameter_mapping.MappingTemplate" } ], "type": "qctoolkit.pulses.sequence_pulse_template.SequencePulseTemplate" diff --git a/doc/source/examples/serialized_pulses/stored_template.json b/doc/source/examples/serialized_pulses/stored_template.json index cb872ed7e..e4e409d89 100644 --- a/doc/source/examples/serialized_pulses/stored_template.json +++ b/doc/source/examples/serialized_pulses/stored_template.json @@ -1,6 +1,6 @@ { - "entries": [ - [ + "entries": { + "A": [ [ 0, 0, @@ -12,25 +12,8 @@ "linear" ] ] - ], - "is_measurement_pulse": false, - "time_parameter_declarations": [ - { - "default_value": null, - "max_value": Infinity, - "min_value": 0, - "name": "4", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ], - "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate", - "voltage_parameter_declarations": [ - { - "default_value": null, - "max_value": Infinity, - "min_value": -Infinity, - "name": "20", - "type": "qctoolkit.pulses.parameters.ParameterDeclaration" - } - ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" } \ No newline at end of file diff --git a/qctoolkit/pulses/measurement.py b/qctoolkit/pulses/measurement.py index 7ee1793ae..c72fe425a 100644 --- a/qctoolkit/pulses/measurement.py +++ b/qctoolkit/pulses/measurement.py @@ -11,7 +11,7 @@ class MeasurementDefiner: - def __init__(self, measurements: Optional[List[MeasurementDeclaration]]=None): + def __init__(self, measurements: Optional[List[MeasurementDeclaration]]): if measurements is None: self._measurement_windows = [] else: @@ -50,3 +50,7 @@ def measurement_declarations(self) -> List[MeasurementDeclaration]: begin.original_expression, length.original_expression) for name, begin, length in self._measurement_windows] + + @property + def measurement_names(self) -> Set[str]: + return {name for name, *_ in self._measurement_windows} diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index fc29c45ed..14c50b61a 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -30,7 +30,8 @@ def __init__(self, template: PulseTemplate, *, parameter_mapping: Optional[Dict[str, str]]=None, measurement_mapping: Optional[Dict[str, str]] = None, channel_mapping: Optional[Dict[ChannelID, ChannelID]] = None, - parameter_constraints: Optional[List[str]]=None): + parameter_constraints: Optional[List[str]]=None, + allow_partial_parameter_mapping=False): """TODO mention nested mappings :param template: @@ -46,10 +47,14 @@ def __init__(self, template: PulseTemplate, *, else: mapped_internal_parameters = set(parameter_mapping.keys()) internal_parameters = template.parameter_names + missing_parameter_mappings = internal_parameters - mapped_internal_parameters if mapped_internal_parameters - internal_parameters: raise UnnecessaryMappingException(template, mapped_internal_parameters - internal_parameters) - elif internal_parameters - mapped_internal_parameters: - raise MissingMappingException(template, internal_parameters - mapped_internal_parameters) + elif missing_parameter_mappings: + if allow_partial_parameter_mapping: + parameter_mapping.update({p: p for p in missing_parameter_mappings}) + else: + raise MissingMappingException(template, internal_parameters - mapped_internal_parameters) parameter_mapping = dict((k, Expression(v)) for k, v in parameter_mapping.items()) measurement_mapping = dict() if measurement_mapping is None else measurement_mapping diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index f2c48df21..bc2de7a6f 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -193,7 +193,7 @@ def requires_stop(self, conditions: Dict[str, 'Condition']) -> bool: """Returns the stop requirement of the first subtemplate. If a later subtemplate requires a stop the SequencePulseTemplate can be partially sequenced.""" - return self.__subtemplates[0].requires_stop(parameters,conditions) if self.__subtemplates else False + return self.__subtemplates[0].requires_stop(parameters, conditions) if self.__subtemplates else False def build_waveform(self, parameters: Dict[str, Parameter]) -> SequenceWaveform: return SequenceWaveform([subtemplate.build_waveform(parameters) for subtemplate in self.__subtemplates]) diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index ec4bcb770..ed35b89c5 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -115,6 +115,7 @@ def push(self, sequencing_element: SequencingElement, parameters: Optional[Dict[str, Union[Parameter, float]]]=None, conditions: Optional[Dict[str, 'Condition']]=None, + *, window_mapping: Optional[Dict[str,str]]=None, channel_mapping: Optional[Dict['ChannelID','ChannelID']]=None, target_block: Optional[InstructionBlock]=None) -> None: @@ -146,10 +147,15 @@ def push(self, if target_block is None: target_block = self.__main_block if window_mapping is None: - window_mapping = dict() + if hasattr(sequencing_element, 'measurement_names'): + window_mapping = {wn: wn for wn in sequencing_element.measurement_names} + else: + window_mapping = dict() if channel_mapping is None: - channel_mapping = dict() - + if hasattr(sequencing_element, 'defined_channels'): + channel_mapping = {cn: cn for cn in sequencing_element.defined_channels} + else: + channel_mapping = dict() for (key, value) in parameters.items(): if isinstance(value, numbers.Real): parameters[key] = ConstantParameter(value) diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 1f80aa3c3..f0aa490d7 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -7,10 +7,12 @@ declared parameters. """ -from typing import Union, Dict, List, Set, Optional, NamedTuple, Any, Iterable, Tuple +from typing import Union, Dict, List, Set, Optional, NamedTuple, Any, Iterable, Tuple, Sequence import numbers import itertools import warnings +from operator import itemgetter +from collections import namedtuple import numpy as np import sympy @@ -39,10 +41,9 @@ class TableWaveform(Waveform): """Waveform obtained from instantiating a TablePulseTemplate.""" - def __init__(self, channel: ChannelID, - waveform_table: Iterable[WaveformTableEntry], + waveform_table: Sequence[WaveformTableEntry], measurement_windows: Iterable[MeasurementWindow]) -> None: """Create a new TableWaveform instance. @@ -51,12 +52,29 @@ def __init__(self, entries of the form (time as float, voltage as float, interpolation strategy). """ super().__init__() - self._table = tuple(waveform_table) + + self._table = TableWaveform._validate_input(waveform_table) self._channel_id = channel self._measurement_windows = tuple(measurement_windows) - if len(waveform_table) < 2: - raise ValueError("A given waveform table has less than two entries.") + @staticmethod + def _validate_input(input_waveform_table: Sequence[WaveformTableEntry]) -> Tuple[WaveformTableEntry]: + if len(input_waveform_table) < 2: + raise ValueError("Waveform table has less than two entries.") + + times = np.fromiter((t for t, *_ in input_waveform_table), dtype=float, count=len(input_waveform_table)) + if times[0] != 0: + raise ValueError('First time point has to be 0') + + diff_times = np.diff(times) + if np.any(diff_times) < 0: + raise ValueError('Times are not increasing') + + # filter 3 subsequent equal times + to_keep = np.full_like(times, True, dtype=np.bool_) + to_keep[1:-1] = np.logical_or(0 != diff_times[:-1], diff_times[:-1] != diff_times[1:]) + + return itemgetter(*np.flatnonzero(to_keep))(input_waveform_table) @property def compare_key(self) -> Any: @@ -84,7 +102,7 @@ def unsafe_sample(self, def defined_channels(self) -> Set[ChannelID]: return {self._channel_id} - def get_measurement_windows(self) -> Iterable[MeasurementWindow]: + def get_measurement_windows(self) -> Sequence[MeasurementWindow]: return self._measurement_windows def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': @@ -97,11 +115,15 @@ def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform' class TableEntry(tuple): - def __new__(cls, t: ValueInInit, v: ValueInInit, interp: Union[str, InterpolationStrategy]='hold'): - return tuple.__new__(cls, (t if isinstance(t, Expression) else Expression(t), - v if isinstance(v, Expression) else Expression(v), - interp if isinstance(interp, InterpolationStrategy) - else TablePulseTemplate.interpolation_strategies[interp])) + def __new__(cls, t: ValueInInit, v: ValueInInit, interp: Union[str, InterpolationStrategy]='default'): + if interp in TablePulseTemplate.interpolation_strategies: + interp = TablePulseTemplate.interpolation_strategies[interp] + if not isinstance(interp, InterpolationStrategy): + raise KeyError(interp, 'is not a valid interpolation strategy') + + return super().__new__(cls, (t if isinstance(t, Expression) else Expression(t), + v if isinstance(v, Expression) else Expression(v), + interp)) @property def t(self) -> Expression: @@ -115,11 +137,18 @@ def v(self) -> Expression: def interp(self) -> InterpolationStrategy: return self[2] + def instantiate(self, parameters: Dict[str, numbers.Real]) -> WaveformTableEntry: + return WaveformTableEntry(self.t.evaluate_numeric(**parameters), + self.v.evaluate_numeric(**parameters), + self.interp) + class TablePulseTemplate(AtomicPulseTemplate, ParameterConstrainer, MeasurementDefiner): + """TablePulseTemplate""" interpolation_strategies = {'linear': LinearInterpolationStrategy(), 'hold': HoldInterpolationStrategy(), - 'jump': JumpInterpolationStrategy()} + 'jump': JumpInterpolationStrategy(), + 'default': HoldInterpolationStrategy()} def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], identifier: Optional[str]=None, @@ -371,6 +400,61 @@ def from_array(times: np.ndarray, voltages: np.ndarray, channels: List[ChannelID voltages if voltages.ndim == 1 else voltages[i, :]))) for i, channel in enumerate(channels))) + @staticmethod + def from_entry_list(entry_list: List[Tuple], + channel_names: Optional[List[ChannelID]]=None, **kwargs) -> 'TablePulseTemplate': + """Static constructor for a TablePulseTemplate where all channel's entries share the same times. + + :param entry_list: List of tuples of the form (t, v_1, ..., v_N[, interp]) + :param channel_names: Optional list of channel identifiers to use. Default is [0, ..., N-1] + :param kwargs: Forwarded to TablePulseTemplate constructor + :return: TablePulseTemplate with + """ + # TODO: Better doc string + def is_valid_interpolation_strategy(inter): + return inter in TablePulseTemplate.interpolation_strategies or isinstance(inter, InterpolationStrategy) + + # determine number of channels + max_len = max(len(data) for data in entry_list) + min_len = min(len(data) for data in entry_list) + + if max_len - min_len > 1: + raise ValueError('There are entries of contradicting lengths: {}'.format(set(len(t) for t in entry_list))) + elif max_len - min_len == 1: + num_chan = min_len - 1 + else: + # figure out whether all last entries are interpolation strategies + if all(is_valid_interpolation_strategy(interp) for *data, interp in entry_list): + num_chan = min_len - 2 + else: + num_chan = min_len - 1 + + # insert default interpolation strategy key + entry_list = [(t, *data, interp) if len(data) == num_chan else (t, *data, interp, 'default') + for t, *data, interp in entry_list] + + for *_, last_voltage, _ in entry_list: + if last_voltage in TablePulseTemplate.interpolation_strategies: + warnings.warn('{} is also an interpolation strategy name but handled as a voltage. Is it intended?' + .format(last_voltage), AmbiguousTablePulseEntry) + + if channel_names is None: + channel_names = list(range(num_chan)) + elif len(channel_names) != num_chan: + raise ValueError('Number of channel identifiers does not correspond to the number of channels.') + + parsed = {channel_name: [] for channel_name in channel_names} + + for time, *voltages, interp in entry_list: + for channel_name, volt in zip(channel_names, voltages): + parsed[channel_name].append((time, volt, interp)) + + return TablePulseTemplate(parsed, **kwargs) + class ZeroDurationTablePulseTemplate(UserWarning): pass + + +class AmbiguousTablePulseEntry(UserWarning): + pass diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 072bf6261..aecc8ee48 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -6,7 +6,7 @@ from qctoolkit.expressions import Expression from qctoolkit.serialization import Serializer -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry, ZeroDurationTablePulseTemplate +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry, ZeroDurationTablePulseTemplate, AmbiguousTablePulseEntry from qctoolkit.pulses.parameters import ParameterNotProvidedException, ParameterConstraintViolation from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform @@ -331,6 +331,46 @@ def test_from_array_multi_one_voltage(self) -> None: for i in range(2)} self.assertEqual(entries, pulse.entries) + def test_from_entry_list_exceptions(self): + TablePulseTemplate.from_entry_list([(1, 2, 3, 'hold'), (2, 2, 2)], channel_names=['A', 'B']) + + with self.assertRaises(ValueError): + TablePulseTemplate.from_entry_list([(1, 2, 3, 'hold'), (2, 2)]) + + with self.assertRaises(ValueError): + TablePulseTemplate.from_entry_list([(1, 2, 3, 'hold'), (2, 2, 2)], channel_names=['A']) + + with self.assertWarns(AmbiguousTablePulseEntry): + TablePulseTemplate.from_entry_list([(1, 2, 3, 'hold'), (2, 2, 'linear')], channel_names=['A', 'B']) + + def test_from_entry_list(self): + entries = {0: [(0, 9, HoldInterpolationStrategy()), + (1, 2, HoldInterpolationStrategy()), + (4, 1, LinearInterpolationStrategy())], + 1: [(0, 8, HoldInterpolationStrategy()), + (1, 1, HoldInterpolationStrategy()), + (4, 2, LinearInterpolationStrategy())], + 2: [(0, 7, HoldInterpolationStrategy()), + (1, 3, HoldInterpolationStrategy()), + (4, 3, LinearInterpolationStrategy())]} + + tpt = TablePulseTemplate.from_entry_list([(0, 9, 8, 7), + (1, 2, 1, 3, 'hold'), + (4, 1, 2, 3, 'linear')], + identifier='tpt') + self.assertEqual(tpt.entries, entries) + self.assertEqual(tpt.identifier, 'tpt') + + entries = {k: entries[i] + for k, i in zip('ABC', [0, 1, 2])} + tpt = TablePulseTemplate.from_entry_list([(0, 9, 8, 7), + (1, 2, 1, 3, 'hold'), + (4, 1, 2, 3, 'linear')], + identifier='tpt', + channel_names=['A', 'B', 'C']) + self.assertEqual(tpt.entries, entries) + self.assertEqual(tpt.identifier, 'tpt') + def test_add_entry_multi_same_time_param(self) -> None: pulse = TablePulseTemplate({0: [(1, 3), ('foo', 'bar'), From 83ab807827728d148b782f0e00d5e85b2cefd989 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 14:41:56 +0200 Subject: [PATCH 064/116] Enable channelID sorting for mixed types (int, str) --- qctoolkit/pulses/multi_channel_pulse_template.py | 3 ++- tests/pulses/multi_channel_pulse_template_tests.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 753ca66f0..41b043a3e 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -100,7 +100,8 @@ def flatten_sub_waveforms(to_flatten): # sort the waveforms with their defined channels to make compare key reproducible def get_sub_waveform_sort_key(waveform): - return tuple(sorted(tuple(waveform.defined_channels))) + return tuple(sorted(tuple('{}_stringified_numeric_channel'.format(ch) if isinstance(ch, int) else ch + for ch in waveform.defined_channels))) self._sub_waveforms = sorted(flatten_sub_waveforms(sub_waveforms), key=get_sub_waveform_sort_key) diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index b19e501e1..91080ef18 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -372,9 +372,9 @@ def test_build_sequence_different_duration(self) -> None: [(dummy2, parameters, conditions, measurement_mapping, channel_mapping)]) def test_build_sequence_same_duration(self) -> None: - dummy_wf1 = DummyWaveform(duration=2.3, defined_channels={'A'}) + dummy_wf1 = DummyWaveform(duration=2.3, defined_channels={0}) dummy_wf2 = DummyWaveform(duration=2.3, defined_channels={'B'}) - dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1, duration=2.3) + dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={0}, waveform=dummy_wf1, duration=2.3) dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2, duration=2.3) sequencer = DummySequencer() @@ -382,7 +382,7 @@ def test_build_sequence_same_duration(self) -> None: parameters = {'bar': ConstantParameter(3)} measurement_mapping = {} - channel_mapping = {'A': 'A', 'B': 'B'} + channel_mapping = {0: 1, 'B': 'B'} instruction_block = DummyInstructionBlock() conditions = {} From ceefabf0a3b9fb5c8b9e7731da932a302000076a Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:26:52 +0200 Subject: [PATCH 065/116] Make names of classes associated with TablePulseTemplate all start with Table --- qctoolkit/pulses/table_pulse_template.py | 46 ++++++++++++++-------- tests/pulses/table_pulse_template_tests.py | 35 +++++++++------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index f0aa490d7..1e11e3e42 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -30,20 +30,33 @@ from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from qctoolkit.pulses.measurement import MeasurementDefiner -__all__ = ["TablePulseTemplate", "TableWaveform", "WaveformTableEntry"] +__all__ = ["TablePulseTemplate", "TableWaveform", "TableWaveformEntry"] -WaveformTableEntry = NamedTuple( - "WaveformTableEntry", - [('t', float), ('v', float), ('interp', InterpolationStrategy)] -) +class TableWaveformEntry(tuple): + def __new__(cls, t: float, v: float, interp: InterpolationStrategy): + if not isinstance(interp, InterpolationStrategy): + raise TypeError('{} is no object of type interpolation strategy'.format(interp)) + return super().__new__(cls, (t, v, interp)) + + @property + def t(self) -> float: + return self[0] + + @property + def v(self) -> float: + return self[1] + + @property + def interp(self) -> InterpolationStrategy: + return self[2] class TableWaveform(Waveform): """Waveform obtained from instantiating a TablePulseTemplate.""" def __init__(self, channel: ChannelID, - waveform_table: Sequence[WaveformTableEntry], + waveform_table: Sequence[TableWaveformEntry], measurement_windows: Iterable[MeasurementWindow]) -> None: """Create a new TableWaveform instance. @@ -58,7 +71,7 @@ def __init__(self, self._measurement_windows = tuple(measurement_windows) @staticmethod - def _validate_input(input_waveform_table: Sequence[WaveformTableEntry]) -> Tuple[WaveformTableEntry]: + def _validate_input(input_waveform_table: Sequence[TableWaveformEntry]) -> Tuple[TableWaveformEntry]: if len(input_waveform_table) < 2: raise ValueError("Waveform table has less than two entries.") @@ -74,7 +87,8 @@ def _validate_input(input_waveform_table: Sequence[WaveformTableEntry]) -> Tuple to_keep = np.full_like(times, True, dtype=np.bool_) to_keep[1:-1] = np.logical_or(0 != diff_times[:-1], diff_times[:-1] != diff_times[1:]) - return itemgetter(*np.flatnonzero(to_keep))(input_waveform_table) + return tuple(entry if isinstance(entry, TableWaveformEntry) else TableWaveformEntry(*entry) + for entry, keep_entry in zip(input_waveform_table, to_keep) if keep_entry) @property def compare_key(self) -> Any: @@ -137,8 +151,8 @@ def v(self) -> Expression: def interp(self) -> InterpolationStrategy: return self[2] - def instantiate(self, parameters: Dict[str, numbers.Real]) -> WaveformTableEntry: - return WaveformTableEntry(self.t.evaluate_numeric(**parameters), + def instantiate(self, parameters: Dict[str, numbers.Real]) -> TableWaveformEntry: + return TableWaveformEntry(self.t.evaluate_numeric(**parameters), self.v.evaluate_numeric(**parameters), self.interp) @@ -215,7 +229,7 @@ def measurement_names(self) -> Set[str]: return {name for name, _, _ in self._measurement_windows} def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ - -> Dict[ChannelID, List[WaveformTableEntry]]: + -> Dict[ChannelID, List[TableWaveformEntry]]: """Compute an instantiated list of the table's entries. Args: @@ -227,17 +241,17 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ if not (self.table_parameters <= set(parameters.keys())): raise ParameterNotProvidedException((self.table_parameters - set(parameters.keys())).pop()) - instantiated_entries = dict() # type: Dict[ChannelID,List[WaveformTableEntry]] + instantiated_entries = dict() # type: Dict[ChannelID,List[TableWaveformEntry]] for channel, channel_entries in self._entries.items(): - instantiated = [WaveformTableEntry(entry.t.evaluate_numeric(**parameters), + instantiated = [TableWaveformEntry(entry.t.evaluate_numeric(**parameters), entry.v.evaluate_numeric(**parameters), entry.interp) for entry in channel_entries] # Add (0, v) entry if wf starts at finite time if instantiated[0].t > 0: - instantiated.insert(0, WaveformTableEntry(0, + instantiated.insert(0, TableWaveformEntry(0, instantiated[0].v, TablePulseTemplate.interpolation_strategies['hold'])) @@ -253,14 +267,14 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ for channel, instantiated in instantiated_entries.items(): final_entry = instantiated[-1] if final_entry.t < duration: - instantiated.append(WaveformTableEntry(duration, + instantiated.append(TableWaveformEntry(duration, final_entry.v, TablePulseTemplate.interpolation_strategies['hold'])) instantiated_entries[channel] = TablePulseTemplate._remove_redundant_entries(instantiated) return instantiated_entries @staticmethod - def _remove_redundant_entries(entries: List[WaveformTableEntry]) -> List[WaveformTableEntry]: + def _remove_redundant_entries(entries: List[TableWaveformEntry]) -> List[TableWaveformEntry]: """ Checks if three subsequent values in a list of table entries have the same value. If so, the intermediate is redundant and removed in-place. diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index aecc8ee48..44922ca17 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -6,7 +6,7 @@ from qctoolkit.expressions import Expression from qctoolkit.serialization import Serializer -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, WaveformTableEntry, ZeroDurationTablePulseTemplate, AmbiguousTablePulseEntry +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate, TableWaveform, TableEntry, TableWaveformEntry, ZeroDurationTablePulseTemplate, AmbiguousTablePulseEntry from qctoolkit.pulses.parameters import ParameterNotProvidedException, ParameterConstraintViolation from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, LinearInterpolationStrategy, JumpInterpolationStrategy from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform @@ -16,6 +16,11 @@ from tests.pulses.measurement_tests import ParameterConstrainerTest, MeasurementDefinerTest +class WaveformEntryTest(unittest.TestCase): + def test_interpolation_exception(self): + with self.assertRaises(TypeError): + TableWaveformEntry(1, 2, 3) + class TableEntryTest(unittest.TestCase): def test_known_interpolation_strategies(self): strategies = [("linear", LinearInterpolationStrategy()), @@ -599,7 +604,7 @@ def test_identifier(self) -> None: class TableWaveformTests(unittest.TestCase): def test_duration(self) -> None: - entries = [WaveformTableEntry(0, 0, HoldInterpolationStrategy()), WaveformTableEntry(5, 1, HoldInterpolationStrategy())] + entries = [TableWaveformEntry(0, 0, HoldInterpolationStrategy()), TableWaveformEntry(5, 1, HoldInterpolationStrategy())] waveform = TableWaveform('A', entries, []) self.assertEqual(5, waveform.duration) @@ -612,31 +617,31 @@ def test_few_entries(self) -> None: with self.assertRaises(ValueError): TableWaveform('A', [[]], []) with self.assertRaises(ValueError): - TableWaveform('A', [WaveformTableEntry(0, 0, HoldInterpolationStrategy())], []) + TableWaveform('A', [TableWaveformEntry(0, 0, HoldInterpolationStrategy())], []) def test_get_measurement_windows(self): interp = DummyInterpolationStrategy() - entries = [WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)] + entries = [TableWaveformEntry(0, 0, interp), + TableWaveformEntry(2.1, -33.2, interp), + TableWaveformEntry(5.7, 123.4, interp)] waveform = TableWaveform('A', entries, measurement_windows=[('a', 1, 2), ('b', 3, 4)]) self.assertEqual(waveform.get_measurement_windows(), (('a', 1, 2), ('b', 3, 4))) def test_unsafe_get_subset_for_channels(self): interp = DummyInterpolationStrategy() - entries = [WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)] + entries = [TableWaveformEntry(0, 0, interp), + TableWaveformEntry(2.1, -33.2, interp), + TableWaveformEntry(5.7, 123.4, interp)] waveform = TableWaveform('A', entries, measurement_windows=[('a', 1, 2), ('b', 3, 4)]) self.assertIs(waveform.unsafe_get_subset_for_channels({'A'}), waveform) def test_unsafe_sample(self) -> None: interp = DummyInterpolationStrategy() - entries = [WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)] + entries = [TableWaveformEntry(0, 0, interp), + TableWaveformEntry(2.1, -33.2, interp), + TableWaveformEntry(5.7, 123.4, interp)] waveform = TableWaveform('A', entries, []) sample_times = numpy.linspace(.5, 5.5, num=11) @@ -656,9 +661,9 @@ def test_unsafe_sample(self) -> None: def test_simple_properties(self): interp = DummyInterpolationStrategy() - entries = [WaveformTableEntry(0, 0, interp), - WaveformTableEntry(2.1, -33.2, interp), - WaveformTableEntry(5.7, 123.4, interp)] + entries = [TableWaveformEntry(0, 0, interp), + TableWaveformEntry(2.1, -33.2, interp), + TableWaveformEntry(5.7, 123.4, interp)] meas = [('M', 1, 2)] chan = 'A' waveform = TableWaveform(chan, entries, meas) From 8ba157a1fa04165b2bb1dd021042e7b6593e4ac2 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:29:19 +0200 Subject: [PATCH 066/116] Add PointPulseTemplate --- qctoolkit/pulses/point_pulse_template.py | 142 +++++++++++++ tests/pulses/point_pulse_template_tests.py | 224 +++++++++++++++++++++ 2 files changed, 366 insertions(+) create mode 100644 qctoolkit/pulses/point_pulse_template.py create mode 100644 tests/pulses/point_pulse_template_tests.py diff --git a/qctoolkit/pulses/point_pulse_template.py b/qctoolkit/pulses/point_pulse_template.py new file mode 100644 index 000000000..7a09282c1 --- /dev/null +++ b/qctoolkit/pulses/point_pulse_template.py @@ -0,0 +1,142 @@ +from typing import Optional, List, Union, Set, Dict, Sequence +from numbers import Real +import itertools +import numbers + +import numpy as np + +from qctoolkit import ChannelID +from qctoolkit.expressions import Expression +from qctoolkit.pulses.parameters import Parameter, ParameterNotProvidedException, ParameterConstraint,\ + ParameterConstrainer +from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration +from qctoolkit.pulses.table_pulse_template import TableEntry, EntryInInit, TableWaveform, TableWaveformEntry +from qctoolkit.pulses.measurement import MeasurementDefiner +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform + + +__all__ = ["PointWaveform", "PointPulseTemplate", "PointPulseEntry", "PointWaveformEntry", "InvalidPointDimension"] + + +PointWaveform = TableWaveform +PointWaveformEntry = TableWaveformEntry + + +class PointPulseEntry(TableEntry): + def instantiate(self, parameters: Dict[str, numbers.Real], num_channels: int) -> Sequence[PointWaveformEntry]: + t = self.t.evaluate_numeric(**parameters) + vs = self.v.evaluate_numeric(**parameters) + + if isinstance(vs, numbers.Number): + vs = np.full(num_channels, vs, dtype=type(vs)) + elif len(vs) != num_channels: + raise InvalidPointDimension(expected=num_channels, received=len(vs)) + + return tuple(PointWaveformEntry(t, v, self.interp) + for v in vs) + + +class PointPulseTemplate(ParameterConstrainer, MeasurementDefiner, AtomicPulseTemplate): + def __init__(self, + time_point_tuple_list: List[EntryInInit], + channel_names: Sequence[ChannelID], + *, + parameter_constraints: Optional[List[Union[str, ParameterConstraint]]]=None, + measurements: Optional[List[MeasurementDeclaration]]=None, + identifier=None): + + AtomicPulseTemplate.__init__(self, identifier=identifier) + ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) + MeasurementDefiner.__init__(self, measurements=measurements) + + self._channels = tuple(channel_names) + self._entries = [PointPulseEntry(*tpt) + for tpt in time_point_tuple_list] + + @property + def defined_channels(self) -> Set[ChannelID]: + return set(self._channels) + + def build_waveform(self, + parameters: Dict[str, Real], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + self.validate_parameter_constraints(parameters) + + if self.duration.evaluate_numeric(**parameters) == 0: + return None + + mapped_channels = tuple(channel_mapping[c] for c in self._channels) + + waveform_entries = [[]]*len(mapped_channels) + for entry in self._entries: + for ch_entries, wf_entry in zip(waveform_entries, entry.instantiate(parameters, len(self._channels))): + ch_entries.append(wf_entry) + + if waveform_entries[0][0].t > 0: + for ch_entries in waveform_entries: + ch_entries[:0] = [PointWaveformEntry(0, ch_entries[0].v, ch_entries[0].interp)] + + measurements = self.get_measurement_windows(parameters=parameters, measurement_mapping=measurement_mapping) + if len(waveform_entries) == 1: + return PointWaveform(mapped_channels[0], waveform_entries[0], measurement_windows=measurements) + else: + return MultiChannelWaveform( + [PointWaveform(mapped_channels[0], waveform_entries[0], measurement_windows=measurements)] + + + [PointWaveform(channel, ch_entries, []) + for channel, ch_entries in zip(mapped_channels[1:], waveform_entries[1:])]) + + @property + def point_pulse_entries(self): + return self._entries + + def get_serialization_data(self, serializer): + data = {'time_point_tuple_list': [(t.get_most_simple_representation(), + v.get_most_simple_representation(), + str(interp)) + for t, v, interp in self._entries], + 'channel_names': self._channels} + if self.parameter_constraints: + data['parameter_constraints'] = [str(c) for c in self.parameter_constraints] + if self.measurement_declarations: + data['measurements'] = self.measurement_declarations + return data + + @staticmethod + def deserialize(serializer, **kwargs): + return PointPulseTemplate(**kwargs) + + @property + def duration(self) -> Expression: + return self._entries[-1].t + + @property + def point_parameters(self) -> Set[str]: + return set( + var + for time, point, *_ in self._entries + for var in itertools.chain(time.variables, point.variables) + ) + + @property + def parameter_names(self) -> Set[str]: + return self.point_parameters | self.measurement_parameters | self.constrained_parameters + + def requires_stop(self, + parameters: Dict[str, Parameter], + conditions: Dict[str, 'Condition']) -> bool: + try: + return any( + parameters[name].requires_stop + for name in self.parameter_names + ) + except KeyError as key_error: + raise ParameterNotProvidedException(str(key_error)) from key_error + + +class InvalidPointDimension(Exception): + def __init__(self, expected, received): + super().__init__('Expected a point of dimension {} but received {}'.format(expected, received)) + self.expected = expected + self.received = received diff --git a/tests/pulses/point_pulse_template_tests.py b/tests/pulses/point_pulse_template_tests.py new file mode 100644 index 000000000..836023b4a --- /dev/null +++ b/tests/pulses/point_pulse_template_tests.py @@ -0,0 +1,224 @@ +import unittest + +import numpy as np + +from qctoolkit.pulses.parameters import ParameterNotProvidedException + +from qctoolkit.pulses.point_pulse_template import PointPulseTemplate, PointWaveform, InvalidPointDimension, PointPulseEntry, PointWaveformEntry +from tests.pulses.measurement_tests import ParameterConstrainerTest, MeasurementDefinerTest +from tests.pulses.sequencing_dummies import DummyParameter, DummyCondition +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform +from qctoolkit.pulses.interpolation import HoldInterpolationStrategy, JumpInterpolationStrategy, LinearInterpolationStrategy +from tests.serialization_dummies import DummySerializer, DummyStorageBackend +from qctoolkit.expressions import Expression +from qctoolkit.serialization import Serializer + +class PointPulseEntryTest(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_instantiate(self): + ppe = PointPulseEntry('t', 'V', HoldInterpolationStrategy()) + + l = ppe.instantiate({'t': 1., 'V': np.arange(3.)}, 3) + expected = (PointWaveformEntry(1., 0, HoldInterpolationStrategy()), + PointWaveformEntry(1., 1, HoldInterpolationStrategy()), + PointWaveformEntry(1., 2, HoldInterpolationStrategy())) + self.assertEqual(l, expected) + + def test_invalid_point_exception(self): + ppe = PointPulseEntry('t', 'V', HoldInterpolationStrategy()) + + with self.assertRaises(InvalidPointDimension) as cm: + ppe.instantiate({'t': 1., 'V': np.ones(3)}, 4) + + self.assertEqual(cm.exception.expected, 4) + self.assertEqual(cm.exception.received, 3) + + def test_scalar_expansion(self): + ppe = PointPulseEntry('t', 'V', HoldInterpolationStrategy()) + + l = ppe.instantiate({'t': 1., 'V': 0.}, 3) + + self.assertEqual(l, (PointWaveformEntry(1., 0., HoldInterpolationStrategy()), + PointWaveformEntry(1., 0., HoldInterpolationStrategy()), + PointWaveformEntry(1., 0., HoldInterpolationStrategy()))) + + +class PointPulseTemplateTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_defined_channels(self): + self.assertEqual(PointPulseTemplate([(1, 'A')], [0]).defined_channels, {0}) + + self.assertEqual(PointPulseTemplate([(1, 'A')], [0, 'asd']).defined_channels, {0, 'asd'}) + + def test_duration(self): + self.assertEqual(PointPulseTemplate([(1, 'A')], [0]).duration, 1) + + self.assertEqual(PointPulseTemplate([(1, 'A'), ('t+6', 'B')], [0, 'asd']).duration, 't+6') + + def test_point_parameters(self): + self.assertEqual(PointPulseTemplate([(1, 'A'), ('t+6', 'B+C')], [0, 'asd']).point_parameters, + {'A', 'B', 't', 'C'}) + + def test_parameter_names(self): + self.assertEqual(PointPulseTemplate([(1, 'A'), ('t+6', 'B+C')], + [0, 'asd'], + measurements=[('M', 'n', 1)], + parameter_constraints=['a < b']).parameter_names, + {'a', 'b', 'n', 'A', 'B', 't', 'C'}) + + def test_requires_stop_missing_param(self) -> None: + table = PointPulseTemplate([('foo', 'v')], [0]) + with self.assertRaises(ParameterNotProvidedException): + table.requires_stop({'foo': DummyParameter(0, False)}, {}) + + def test_requires_stop(self) -> None: + point = PointPulseTemplate([('foo', 'v'), ('bar', 0)], [0]) + test_sets = [(False, {'foo': DummyParameter(0, False), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(False)}), + (False, {'foo': DummyParameter(0, False), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(True)}), + (True, {'foo': DummyParameter(0, True), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(False)}), + (True, {'foo': DummyParameter(0, True), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, False)}, {'foo': DummyCondition(True)}), + (True, {'foo': DummyParameter(0, False), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, True)}, {'foo': DummyCondition(False)}), + (True, {'foo': DummyParameter(0, False), 'bar': DummyParameter(0, False), 'v': DummyParameter(0, True)}, {'foo': DummyCondition(True)}), + (True, {'foo': DummyParameter(0, True), 'bar': DummyParameter(0, True), 'v': DummyParameter(0, True)}, {'foo': DummyCondition(False)}), + (True, {'foo': DummyParameter(0, True), 'bar': DummyParameter(0, True), 'v': DummyParameter(0, True)}, {'foo': DummyCondition(True)})] + for expected_result, parameter_set, condition_set in test_sets: + self.assertEqual(expected_result, point.requires_stop(parameter_set, condition_set)) + + def test_build_waveform_empty(self): + self.assertIsNone(PointPulseTemplate([('t1', 'A')], [0]).build_waveform(parameters={'t1': 0, 'A': 1}, channel_mapping={0: 1}, measurement_mapping=dict())) + + def test_build_waveform_single_channel(self): + ppt = PointPulseTemplate([('t1', 'A'), + ('t2', 0., 'hold'), + ('t1+t2', 'B+C', 'linear')], [0]) + + parameters = {'t1': 0.1, 't2': 1., 'A': 1., 'B': 2., 'C': 19.} + + wf = ppt.build_waveform(parameters=parameters, channel_mapping={0: 1}, measurement_mapping=dict()) + expected = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + (0.1, 1., HoldInterpolationStrategy()), + (1., 0., HoldInterpolationStrategy()), + (1.1, 21., LinearInterpolationStrategy())], []) + self.assertIsInstance(wf, PointWaveform) + self.assertEqual(wf, expected) + + def test_build_waveform_single_channel_with_measurements(self): + ppt = PointPulseTemplate([('t1', 'A'), + ('t2', 0., 'hold'), + ('t1+t2', 'B+C', 'linear')], [0], measurements=[('M', 'n', 1), ('L', 'n', 1)]) + + parameters = {'t1': 0.1, 't2': 1., 'A': 1., 'B': 2., 'C': 19., 'n': 0.2} + wf = ppt.build_waveform(parameters=parameters, channel_mapping={0: 1}, measurement_mapping={'M': 'K', 'L': 'L'}) + expected = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + (0.1, 1., HoldInterpolationStrategy()), + (1., 0., HoldInterpolationStrategy()), + (1.1, 21., LinearInterpolationStrategy())], + [('K', 0.2, 1), ('L', 0.2, 1)]) + self.assertEqual(wf, expected) + + def test_build_waveform_multi_channel_same(self): + ppt = PointPulseTemplate([('t1', 'A'), + ('t2', 0., 'hold'), + ('t1+t2', 'B+C', 'linear')], [0, 'A'], measurements=[('M', 'n', 1), ('L', 'n', 1)]) + + parameters = {'t1': 0.1, 't2': 1., 'A': 1., 'B': 2., 'C': 19., 'n': 0.2} + wf = ppt.build_waveform(parameters=parameters, channel_mapping={0: 1, 'A': 'A'}, measurement_mapping={'M': 'K', 'L': 'L'}) + expected_1 = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + (0.1, 1., HoldInterpolationStrategy()), + (1., 0., HoldInterpolationStrategy()), + (1.1, 21., LinearInterpolationStrategy())], + [('K', 0.2, 1), ('L', 0.2, 1)]) + expected_A = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + (0.1, 1., HoldInterpolationStrategy()), + (1., 0., HoldInterpolationStrategy()), + (1.1, 21., LinearInterpolationStrategy())], []) + self.assertEqual(wf.defined_channels, {1, 'A'}) + + def test_build_waveform_multi_channel_vectorized(self): + ppt = PointPulseTemplate([('t1', 'A'), + ('t2', 0., 'hold'), + ('t1+t2', 'B+C', 'linear')], [0, 'A'], measurements=[('M', 'n', 1), ('L', 'n', 1)]) + + parameters = {'t1': 0.1, 't2': 1., 'A': np.ones(2), 'B': np.arange(2), 'C': 19., 'n': 0.2} + wf = ppt.build_waveform(parameters=parameters, channel_mapping={0: 1, 'A': 'A'}, measurement_mapping={'M': 'K', 'L': 'L'}) + expected_1 = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + (0.1, 1., HoldInterpolationStrategy()), + (1., 0., HoldInterpolationStrategy()), + (1.1, 19., LinearInterpolationStrategy())], + [('K', 0.2, 1), ('L', 0.2, 1)]) + expected_A = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + (0.1, 1., HoldInterpolationStrategy()), + (1., 0., HoldInterpolationStrategy()), + (1.1, 20., LinearInterpolationStrategy())], []) + self.assertEqual(wf.defined_channels, {1, 'A'}) + + +class TablePulseTemplateConstraintTest(ParameterConstrainerTest): + def __init__(self, *args, **kwargs): + + def ppt_constructor(parameter_constraints=None): + return PointPulseTemplate([('t', 'V', 'hold')], channel_names=[0], + parameter_constraints=parameter_constraints, measurements=[('M', 'n', 1)]) + + super().__init__(*args, + to_test_constructor=ppt_constructor, **kwargs) + + +class TablePulseTemplateMeasurementTest(MeasurementDefinerTest): + def __init__(self, *args, **kwargs): + + def tpt_constructor(measurements=None): + return PointPulseTemplate([('t', 'V', 'hold')], channel_names=[0], + parameter_constraints=['a < b'], measurements=measurements) + + super().__init__(*args, + to_test_constructor=tpt_constructor, **kwargs) + + +class PointPulseTemplateSerializationTests(unittest.TestCase): + def setUp(self) -> None: + self.serializer = DummySerializer(lambda x: dict(name=x.name), lambda x: x.name, lambda x: x['name']) + self.entries = [('foo', 2, 'hold'), ('hugo', 'A + B', 'linear')] + self.measurements = [('m', 1, 1), ('foo', 'z', 'o')] + self.template = PointPulseTemplate(time_point_tuple_list=self.entries, channel_names=[0, 'A'], + measurements=self.measurements, + identifier='foo', parameter_constraints=['ilse>2', 'k>foo']) + self.expected_data = dict(type=self.serializer.get_type_identifier(self.template)) + self.maxDiff = None + + def test_get_serialization_data(self) -> None: + expected_data = dict(measurements=self.measurements, + time_point_tuple_list=self.entries, + channel_names=(0, 'A'), + parameter_constraints=[str(Expression('ilse>2')), str(Expression('k>foo'))]) + + data = self.template.get_serialization_data(self.serializer) + self.assertEqual(expected_data, data) + + def test_deserialize(self) -> None: + data = dict(measurements=self.measurements, + time_point_tuple_list=self.entries, + channel_names=(0, 'A'), + parameter_constraints=['ilse>2', 'k>foo'], + identifier='foo') + + # deserialize + template = PointPulseTemplate.deserialize(self.serializer, **data) + + self.assertEqual(template.point_pulse_entries, self.template.point_pulse_entries) + self.assertEqual(template.measurement_declarations, self.template.measurement_declarations) + self.assertEqual(template.parameter_constraints, self.template.parameter_constraints) + + def test_serializer_integration(self): + serializer = Serializer(DummyStorageBackend()) + serializer.serialize(self.template) + template = serializer.deserialize('foo') + + self.assertIsInstance(template, PointPulseTemplate) + self.assertEqual(template.point_pulse_entries, self.template.point_pulse_entries) + self.assertEqual(template.measurement_declarations, self.template.measurement_declarations) + self.assertEqual(template.parameter_constraints, self.template.parameter_constraints) \ No newline at end of file From 2658e7701708939e36d67e15e11be6881805018c Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:35:29 +0200 Subject: [PATCH 067/116] Remove BranchPulseTemplate --- qctoolkit/pulses/__init__.py | 1 - qctoolkit/pulses/branch_pulse_template.py | 121 ----------- tests/pulses/branch_pulse_template_tests.py | 213 -------------------- 3 files changed, 335 deletions(-) delete mode 100644 qctoolkit/pulses/branch_pulse_template.py delete mode 100644 tests/pulses/branch_pulse_template_tests.py diff --git a/qctoolkit/pulses/__init__.py b/qctoolkit/pulses/__init__.py index 64ff21e34..9fe322a0c 100644 --- a/qctoolkit/pulses/__init__.py +++ b/qctoolkit/pulses/__init__.py @@ -1,4 +1,3 @@ -from qctoolkit.pulses.branch_pulse_template import * from qctoolkit.pulses.conditions import * from qctoolkit.pulses.function_pulse_template import * from qctoolkit.pulses.instructions import * diff --git a/qctoolkit/pulses/branch_pulse_template.py b/qctoolkit/pulses/branch_pulse_template.py deleted file mode 100644 index 5c4cea149..000000000 --- a/qctoolkit/pulses/branch_pulse_template.py +++ /dev/null @@ -1,121 +0,0 @@ -"""This module defines BranchPulseTemplate, a higher-order hierarchical pulse template that -conditionally executes one out of two possible PulseTemplates.""" - -from typing import Dict, Set, List, Optional, Any, Union - -from qctoolkit.expressions import Expression - -from qctoolkit.pulses.parameters import Parameter, ParameterConstraint -from qctoolkit.pulses.pulse_template import PulseTemplate -from qctoolkit.pulses.conditions import Condition, ConditionMissingException -from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock -from qctoolkit.serialization import Serializer - -__all__ = ["BranchPulseTemplate"] - - -class BranchPulseTemplate(PulseTemplate): - """Conditional branching in a pulse. - - A BranchPulseTemplate is a PulseTemplate with different structures depending on a certain - condition. It refers to an if-branch and an else-branch, which are both PulseTemplates. - When instantiating a pulse from a BranchPulseTemplate, both branches refer to concrete pulses. - If the given condition evaluates to true at the time the pulse is executed, - the if-branch, otherwise the else-branch, is chosen for execution. - This allows for alternative execution 'paths' in pulses. - - Both branches must be of the same length. - """ - - def __init__(self, - condition: str, - if_branch: PulseTemplate, - else_branch: PulseTemplate, - identifier: Optional[str]=None) -> None: - """Create a new BranchPulseTemplate instance. - - Args: - condition (str): A unique identifier for the branching condition. Will be used to obtain - the Condition object from the mapping passed in during the sequencing process. - if_branch (PulseTemplate): A PulseTemplate representing the pulse that is to be - executed if the condition holds. - else_branch (PulseTemplate): A PulseTemplate representing the pulse that is to be - executed if the condition does not hold. - identifier (str): A unique identifier for use in serialization. (optional) - """ - super().__init__(identifier=identifier) - if if_branch.defined_channels != else_branch.defined_channels: - raise ValueError("The channels defined by the provided pulses differ!") - self.__condition = condition - self.__if_branch = if_branch - self.__else_branch = else_branch - - def __str__(self) -> str: - return "BranchPulseTemplate: Condition <{}>, If-Branch <{}>, Else-Branch <{}>"\ - .format(self.__condition, self.__if_branch, self.__else_branch) - - @property - def parameter_names(self) -> Set[str]: - return self.__if_branch.parameter_names | self.__else_branch.parameter_names - - @property - def is_interruptable(self) -> bool: - return self.__if_branch.is_interruptable and self.__else_branch.is_interruptable - - @property - def defined_channels(self) -> Set['ChannelID']: - return self.__if_branch.defined_channels - - @property - def duration(self) -> Expression: - return Expression('nan') - - @property - def measurement_names(self) -> Set[str]: - return self.__if_branch.measurement_names | self.__else_branch.measurement_names - - def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Condition: - try: - return conditions[self.__condition] - except KeyError as key_error: - raise ConditionMissingException(self.__condition) from key_error - - def build_sequence(self, - sequencer: Sequencer, - parameters: Dict[str, Parameter], - conditions: Dict[str, Condition], - measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], - instruction_block: InstructionBlock) -> None: - self.__obtain_condition_object(conditions).build_sequence_branch(self, - self.__if_branch, - self.__else_branch, - sequencer, - parameters, - conditions, - measurement_mapping, - channel_mapping, - instruction_block) - - def requires_stop(self, - parameters: Dict[str, Parameter], - conditions: Dict[str, Condition]) -> bool: - return self.__obtain_condition_object(conditions).requires_stop() - - def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - return dict( - if_branch_template=serializer.dictify(self.__if_branch), - else_branch_template=serializer.dictify(self.__else_branch), - condition=self.__condition - ) - - @staticmethod - def deserialize(serializer: Serializer, - condition: str, - if_branch_template: Dict[str, Any], - else_branch_template: Dict[str, Any], - identifier: Optional[str]=None) -> 'BranchPulseTemplate': - return BranchPulseTemplate(condition, - serializer.deserialize(if_branch_template), - serializer.deserialize(else_branch_template), - identifier) diff --git a/tests/pulses/branch_pulse_template_tests.py b/tests/pulses/branch_pulse_template_tests.py deleted file mode 100644 index 5f1437602..000000000 --- a/tests/pulses/branch_pulse_template_tests.py +++ /dev/null @@ -1,213 +0,0 @@ -import unittest - -from qctoolkit.pulses.branch_pulse_template import BranchPulseTemplate -from qctoolkit.pulses.conditions import ConditionMissingException -from qctoolkit.pulses.parameters import ParameterConstraint - -from tests.pulses.sequencing_dummies import DummyPulseTemplate, DummyParameter, DummyCondition, DummySequencer, DummyInstructionBlock -from tests.serialization_dummies import DummySerializer - - -class BranchPulseTemplateTest(unittest.TestCase): - - def test_wrong_channel_composition(self) -> None: - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'C'}) - with self.assertRaises(ValueError): - BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - - def test_identifier(self) -> None: - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy, identifier='hugo') - self.assertEqual('hugo', template.identifier) - - def test_parameter_names_and_declarations(self) -> None: - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertEqual({'foo', 'bar', 'hugo'}, template.parameter_names) - - def test_defined_channels(self) -> None: - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertEqual({'A', 'B'}, template.defined_channels) - - def test_measurement_names(self) -> None: - if_dummy = DummyPulseTemplate(measurement_names={'if_meas'}) - else_dummy = DummyPulseTemplate(measurement_names={'else_meas'}) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertEqual({'if_meas','else_meas'}, template.measurement_names) - - def test_is_interruptable(self) -> None: - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertFalse(template.is_interruptable) - - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertTrue(template.is_interruptable) - - def test_str(self) -> None: - if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, is_interruptable=True) - else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}) - template = BranchPulseTemplate('foo_condition', if_dummy, else_dummy) - self.assertIsInstance(str(template), str) - - -class BranchPulseTemplateSequencingTests(unittest.TestCase): - - def setUp(self) -> None: - self.maxDiff = None - self.if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}, - measurement_names={'if_meas'}) - self.else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}, - measurement_names={'else_meas'}) - self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) - self.sequencer = DummySequencer() - self.block = DummyInstructionBlock() - - def test_requires_stop_parameters_dont_conditions_dont(self) -> None: - conditions = dict(foo_condition=DummyCondition(requires_stop=False)) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23), - hugo=DummyParameter(3.532)) - self.assertFalse(self.template.requires_stop(parameters, conditions)) - - def test_requires_stop_parameters_do_conditions_dont(self) -> None: - conditions = dict(foo_condition=DummyCondition(requires_stop=False)) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23, requires_stop=True), - hugo=DummyParameter(3.532)) - self.assertFalse(self.template.requires_stop(parameters, conditions)) - - def test_requires_stop_parameters_dont_conditions_do(self) -> None: - conditions = dict(foo_condition=DummyCondition(requires_stop=True)) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23), - hugo=DummyParameter(3.532)) - self.assertTrue(self.template.requires_stop(parameters, conditions)) - - def test_requires_stop_parameters_do_conditions_do(self) -> None: - conditions = dict(foo_condition=DummyCondition(requires_stop=True)) - parameters = dict(foo=DummyParameter(326.272, requires_stop=True), - bar=DummyParameter(-2624.23), - hugo=DummyParameter(3.532)) - self.assertTrue(self.template.requires_stop(parameters, conditions)) - - def test_requires_stop_condition_missing(self) -> None: - conditions = dict(bar_condition=DummyCondition(requires_stop=True)) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23), - hugo=DummyParameter(3.532)) - with self.assertRaises(ConditionMissingException): - self.template.requires_stop(parameters, conditions) - - def test_requires_stop_parameters_missing(self) -> None: - conditions = dict(foo_condition=DummyCondition(requires_stop=False)) - parameters = dict(foo=DummyParameter(326.272), - hugo=DummyParameter(3.532)) - self.assertFalse(self.template.requires_stop(parameters, conditions)) - - def test_build_sequence(self) -> None: - foo_condition = DummyCondition() - conditions = dict(foo_condition=foo_condition) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23), - hugo=DummyParameter(3.532)) - window_mapping = dict(else_meas='my_meas',if_meas='thy_meas') - channel_mapping = dict() - - self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, channel_mapping, self.block) - self.assertFalse(foo_condition.loop_call_data) - self.assertEqual( - dict( - delegator=self.template, - if_branch=self.if_dummy, - else_branch=self.else_dummy, - sequencer=self.sequencer, - parameters=parameters, - conditions=conditions, - measurement_mapping=window_mapping, - channel_mapping=channel_mapping, - instruction_block=self.block - ), - foo_condition.branch_call_data - ) - self.assertFalse(self.sequencer.sequencing_stacks) - self.assertFalse(self.block.instructions) - - def test_build_sequence_condition_missing(self) -> None: - conditions = dict(bar_condition=DummyCondition(requires_stop=True)) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23), - hugo=DummyParameter(3.532)) - window_mapping = dict() - channel_mapping = dict() - with self.assertRaises(ConditionMissingException): - self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, channel_mapping, self.block) - - def test_build_sequence_parameter_missing(self) -> None: - foo_condition = DummyCondition() - conditions = dict(foo_condition=foo_condition) - parameters = dict(foo=DummyParameter(326.272), - bar=DummyParameter(-2624.23)) - window_mapping = dict(else_meas='my_meas',if_meas='thy_meas') - channel_mapping = dict() - self.template.build_sequence(self.sequencer, parameters, conditions, window_mapping, channel_mapping, self.block) - self.assertFalse(foo_condition.loop_call_data) - self.assertEqual( - dict( - delegator=self.template, - if_branch=self.if_dummy, - else_branch=self.else_dummy, - sequencer=self.sequencer, - parameters=parameters, - conditions=conditions, - measurement_mapping=window_mapping, - channel_mapping=channel_mapping, - instruction_block=self.block - ), - foo_condition.branch_call_data - ) - self.assertFalse(self.sequencer.sequencing_stacks) - self.assertFalse(self.block.instructions) - - -class BranchPulseTemplateSerializationTests(unittest.TestCase): - - def setUp(self) -> None: - self.maxDiff = None - self.if_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'bar'}, measurement_names={'if_mease'}) - self.else_dummy = DummyPulseTemplate(defined_channels={'A', 'B'}, parameter_names={'foo', 'hugo'}, measurement_names={'else_meas'}) - self.template = BranchPulseTemplate('foo_condition', self.if_dummy, self.else_dummy) - self.constraints = [''] - - def test_get_serialization_data(self) -> None: - expected_data = dict( - if_branch_template=str(id(self.if_dummy)), - else_branch_template=str(id(self.else_dummy)), - condition='foo_condition' - ) - serializer = DummySerializer() - serialized_data = self.template.get_serialization_data(serializer) - self.assertEqual(expected_data, serialized_data) - - def test_deserialize(self) -> None: - base_data = dict( - if_branch_template=str(id(self.if_dummy)), - else_branch_template=str(id(self.else_dummy)), - condition='foo_condition', - identifier='hugo' - ) - serializer = DummySerializer() - serializer.subelements[str(id(self.if_dummy))] = self.if_dummy - serializer.subelements[str(id(self.else_dummy))] = self.else_dummy - template = BranchPulseTemplate.deserialize(serializer, **base_data) - self.assertEqual('hugo', template.identifier) - serialized_data = template.get_serialization_data(serializer) - del base_data['identifier'] - self.assertEqual(base_data, serialized_data) \ No newline at end of file From 3eefb8e012043995322c1d6baad1227f202d93db Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:35:53 +0200 Subject: [PATCH 068/116] Remove unneeded coverage checker --- tests/coverage_tests.py | 13 ------- tests/property_test_helper.py | 55 ---------------------------- tests/pulses/function_pulse_tests.py | 2 - 3 files changed, 70 deletions(-) delete mode 100644 tests/coverage_tests.py delete mode 100644 tests/property_test_helper.py diff --git a/tests/coverage_tests.py b/tests/coverage_tests.py deleted file mode 100644 index 8d737ec76..000000000 --- a/tests/coverage_tests.py +++ /dev/null @@ -1,13 +0,0 @@ -import qctoolkit.pulses.function_pulse_template -import qctoolkit.pulses.table_pulse_template - -import tests.pulses.function_pulse_tests -import tests.pulses.table_pulse_template_tests - -from tests.property_test_helper import assert_public_functions_tested_tester - - -FunctionPublicFunctionsTested = assert_public_functions_tested_tester(tests.pulses.function_pulse_tests, - qctoolkit.pulses.function_pulse_template) -TablePublicFunctionsTested = assert_public_functions_tested_tester(tests.pulses.table_pulse_template_tests, - qctoolkit.pulses.table_pulse_template) \ No newline at end of file diff --git a/tests/property_test_helper.py b/tests/property_test_helper.py deleted file mode 100644 index 065399470..000000000 --- a/tests/property_test_helper.py +++ /dev/null @@ -1,55 +0,0 @@ -import inspect -import unittest - -class assert_all_properties_tested: - def __init__(self, to_test: type): - self.expected_tests = [] - base_class_members = [member - for baseclass in inspect.getmro(to_test)[1:] - for member in inspect.getmembers(baseclass)] - for name, member in inspect.getmembers(to_test): - if name.startswith('_'): - continue - if (name, member) in base_class_members: - continue - if inspect.isdatadescriptor(member): - self.expected_tests.append((name, 'test_' + name)) - - def __call__(self, tester_type: type): - def test_all_properties_tested(tester: unittest.TestCase): - not_tested = [member_name - for member_name, test_name in self.expected_tests if not hasattr(tester, test_name)] - tester.assertFalse(not_tested, 'Missing property test for {}'.format(not_tested)) - - tester_type.test_all_properties_tested = test_all_properties_tested - return tester_type - - -def assert_public_functions_tested_tester(testing_module, tested_module): - def get_public_functions(cls): - for name, member in inspect.getmembers(cls): - if inspect.getmodule(member) is tested_module: - if not name.startswith('_'): - if inspect.isfunction(member): - yield name - elif inspect.isclass(member): - yield from get_public_functions(member) - to_test = list(get_public_functions(tested_module)) - - def get_tests(cls): - for name, member in inspect.getmembers(cls): - if not name.startswith('_') and inspect.getmodule(member) is testing_module: - if inspect.isfunction(member) and name.startswith('test_'): - yield name - elif inspect.isclass(member) and issubclass(member, unittest.TestCase): - yield from get_tests(member) - testing_functions = list(get_tests(testing_module)) - - class PublicFunctionTest(unittest.TestCase): - def test_all_public_functions_tested(self): - non_tested_functions = [name for name in to_test if not any(testing_function.startswith('test_' + name) - for testing_function in testing_functions)] - self.assertFalse(non_tested_functions, "{} has no tests for {}".format(testing_module.__name__, - non_tested_functions)) - - return PublicFunctionTest diff --git a/tests/pulses/function_pulse_tests.py b/tests/pulses/function_pulse_tests.py index 51c88ced0..868375d18 100644 --- a/tests/pulses/function_pulse_tests.py +++ b/tests/pulses/function_pulse_tests.py @@ -13,7 +13,6 @@ from tests.serialization_dummies import DummySerializer, DummyStorageBackend from tests.pulses.sequencing_dummies import DummyParameter from tests.pulses.measurement_tests import MeasurementDefinerTest, ParameterConstrainerTest -from tests.property_test_helper import assert_all_properties_tested class FunctionPulseTest(unittest.TestCase): @@ -51,7 +50,6 @@ def setUp(self) -> None: self.pars = dict(a=DummyParameter(1), b=DummyParameter(2), c=DummyParameter(136.78)) -@assert_all_properties_tested(to_test=FunctionPulseTemplate) class FunctionPulsePropertyTest(FunctionPulseTest): def test_expression(self): self.assertEqual(self.fpt.expression, self.s) From 3b976a8be90bcc439d98e71f38ca0d318ff3f678 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:37:43 +0200 Subject: [PATCH 069/116] Remove simple square pulse example from package --- qctoolkit/examples/simple_square_pulse.py | 81 ----------------------- 1 file changed, 81 deletions(-) delete mode 100644 qctoolkit/examples/simple_square_pulse.py diff --git a/qctoolkit/examples/simple_square_pulse.py b/qctoolkit/examples/simple_square_pulse.py deleted file mode 100644 index 583f3f97c..000000000 --- a/qctoolkit/examples/simple_square_pulse.py +++ /dev/null @@ -1,81 +0,0 @@ -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate -from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate -from qctoolkit.pulses.plotting import plot - -# -# c -# /| -# / | -# / | -# / | -# b | -# | | -# | | -# | | -# a--------| ------------------d -# -# Point: Tuple -# a: (0,0) -# b: ('t_up', 'value1') -# c: ('t_down', 'value2') -# d: ('end', 0) -# t_down should not be given by the user, instead give the time between c and b as 'length' - -squarePulse = TablePulseTemplate() # Prepare new empty Pulse -# Then add pulses sequentially -squarePulse.add_entry('t_up', 'value1', interpolation='hold') # hold is the standard interpolation value -squarePulse.add_entry('t_down', 'value2', interpolation='linear') -squarePulse.add_entry('end', 0, interpolation='jump') - -# We can just plug in values for the parameters to get an actual pulse: -parameters = {'t_up': 200, - 't_down': 2000, - 'end': 4000, - 'value1': 2.2, - 'value2': 3.0} - -# with these parameters, we can plot the pulse: -plot(squarePulse, parameters) - -# To re-parametrize we can simply wrap the pulse definition in a SequencePulseTemplate that provides functionality -# for mapping its own parameters onto children parameters. - -mapping = {} -mapping['t_up'] = 'start' -mapping['t_down'] = 'start + length' -mapping['value1'] = 'value1' -mapping['value2'] = 'value2' -mapping['end'] = 'pulse_length * 0.5' - -doubleSquare = SequencePulseTemplate([(squarePulse, mapping), - (squarePulse, mapping)], # dictionaries with mapping functions from external parameters to subtemplate parameters - ['start', 'length', 'value1', 'value2', 'pulse_length']) # declare the new template's external parameters - -params = dict(start=5, - length=20, - value1=10, - value2=15, - pulse_length=500) - -plot(doubleSquare, params) - -nested_mapping = dict(start='start', - length='length', - value1='10', - value2='20', - pulse_length='pulse_length * 0.5') - -nested_pulse = SequencePulseTemplate([(doubleSquare, nested_mapping), - (doubleSquare, nested_mapping)], - ['start', 'length', 'pulse_length']) - -params2 = dict(start=10, length=100, pulse_length=1000) - -# # Instead of calling the convenience plot function, we can also use the PlottingSequencer directly -# # This is also an instructive example on how to use sequencers. -# plotter = Plotter() -# sequencer = Sequencer(plotter) -# sequencer.push(nested_pulse, params2) -# times, voltages = plotter.render() -# plt.step(times, voltages) -# plt.show() # eh voila, a sequence of four pulses From 115e7e3f637da17e8a9c946c827fd9914cca1c8b Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:38:17 +0200 Subject: [PATCH 070/116] Remove qcmatlab (MATLAB interface) --- qctoolkit/qcmatlab/__init__.py | 1 - qctoolkit/qcmatlab/manager.py | 82 ------------------- qctoolkit/qcmatlab/pulse_control.py | 118 ---------------------------- 3 files changed, 201 deletions(-) delete mode 100644 qctoolkit/qcmatlab/__init__.py delete mode 100644 qctoolkit/qcmatlab/manager.py delete mode 100644 qctoolkit/qcmatlab/pulse_control.py diff --git a/qctoolkit/qcmatlab/__init__.py b/qctoolkit/qcmatlab/__init__.py deleted file mode 100644 index 7f9153b61..000000000 --- a/qctoolkit/qcmatlab/__init__.py +++ /dev/null @@ -1 +0,0 @@ -__all__ = ['manager', 'pulse_control'] \ No newline at end of file diff --git a/qctoolkit/qcmatlab/manager.py b/qctoolkit/qcmatlab/manager.py deleted file mode 100644 index ddcac50bc..000000000 --- a/qctoolkit/qcmatlab/manager.py +++ /dev/null @@ -1,82 +0,0 @@ -import matlab.engine -""" Start a MATLAB session or connect to a connect to an existing one. """ - -__all__ = ["Manager", - "EngineNotFound", - "NoConnectionSupported"] - - -class NoConnectionSupported(Exception): - pass - - -class EngineNotFound(Exception): - - def __init__(self,searched_engine: str) -> None: - self.searched_engine = searched_engine - - def __str__(self) -> str: - return "Could not find the MATLAB engine with name {}".format(self.searched_engine) - - -class Manager(): - __engine_name = 'qc_toolkit_session' - __engine_store = [] - - - @staticmethod - def __start_new_engine() -> None: - Manager.__engine_store = matlab.engine.start_matlab('-desktop') - - if Manager.__engine_store.version('-release') == 'R2015a': - print('Warning this ') - else: - Manager.__engine_store.matlab.engine.shareEngine(Manager.__engine_name,nargout=0) - - @staticmethod - def __connect_to_existing_matlab_engine() -> None: - - # I found no nicer way to test the version of the python interface - try: - names = matlab.engine.find_matlab() - except AttributeError: - raise NoConnectionSupported() - - try: - Manager.__engine_store = matlab.engine.connect_matlab(Manager.__engine_name) - except matlab.engine.EngineError: - raise EngineNotFound(names) - - @staticmethod - def connect_to( engine_name: str ) -> None: - - if Manager.__engine_store is matlab.engine.matlabengine.MatlabEngine: - if Manager.__engine_name == engine_name: - print('Already connected to engine {name}.'.format(name=engine_name)) - return - - if engine_name in matlab.engine.find_matlab(): - if Manager.__engine_store is matlab.engine.matlabengine.MatlabEngine: - print('Disconnecting from old engine...') - Manager.__engine_store.quit() - Manager.__engine_store = matlab.engine.connect_engine(engine_name) - Manager.__engine_name = engine_name - else: - raise EngineNotFound(searched_engine=engine_name) - - @staticmethod - def get_engine() -> matlab.engine.matlabengine.MatlabEngine: - if Manager.__engine_store is matlab.engine.matlabengine.MatlabEngine: - return Manager.__engine_store - - try: - Manager.__connect_to_existing_matlab_engine() - except NoConnectionSupported: - print('Current MATLAB version does not support connection to running engines.\nCreating a new one...') - Manager.__start_new_engine() - except EngineNotFound: - print('Could not find running engine with name {}.\nStarting a new one....'.format(Manager.__engine_name)) - Manager.__start_new_engine() - - assert( isinstance(Manager.__engine_store, matlab.engine.matlabengine.MatlabEngine) ) - return Manager.__engine_store diff --git a/qctoolkit/qcmatlab/pulse_control.py b/qctoolkit/qcmatlab/pulse_control.py deleted file mode 100644 index 4967a784e..000000000 --- a/qctoolkit/qcmatlab/pulse_control.py +++ /dev/null @@ -1,118 +0,0 @@ -"""This module defines the PulseControlInterface, which offers functionality to convert instruction -sequences obtained by sequencing PulseTemplate structures into MATLAB-interpretable -pulse-control pulse structures.""" - -from math import floor -from typing import Any, Dict, List, Tuple - -import numpy - -from qctoolkit.pulses.instructions import Waveform, EXECInstruction, \ - STOPInstruction, InstructionSequence - -__all__ = ["PulseControlInterface"] - - -class PulseControlInterface: - """Offers functionality to convert instruction sequences obtained by sequencing PulseTemplate - structures into MATLAB-interpretable pulse control pulse structures.""" - - Pulse = Dict[str, Any] - PulseGroup = Dict[str, Any] - - def __init__(self, sample_rate: int, time_scaling: float=0.001) -> None: - """Initialize PulseControlInterface. - - Arguments: - sample_rate (int): The rate in Hz at which waveforms are sampled. - time_scaling (float): A factor that scales the time domain defined in PulseTemplates. - (default = 0.001, i.e., one unit of time in a PulseTemplate corresponds to one - microsecond) - """ - super().__init__() - self.__sample_rate = sample_rate - self.__time_scaling = time_scaling - - @staticmethod - def __get_waveform_name(waveform: Waveform) -> str: - # returns a unique identifier for a waveform object - return 'wf_{}'.format(hash(waveform)) - - def create_waveform_struct(self, - waveform: Waveform, - name: str) -> 'PulseControlInterface.Pulse': - """Construct a dictionary adhering to the waveform struct definition in pulse control. - - Arguments: - waveform (Waveform): The Waveform object to convert. - name (str): Value for the name field in the resulting waveform dictionary. - Returns: - a dictionary representing waveform as a waveform struct for pulse control - """ - if len(waveform.defined_channels) > 1: - raise ValueError('More than one channel') - channel = next(iter(waveform.defined_channels)) - - sample_count = floor(waveform.duration * self.__time_scaling * self.__sample_rate) + 1 - sample_times = numpy.linspace(0, waveform.duration, sample_count) - sampled_waveform = waveform.get_sampled(channel, sample_times) - struct = dict(name=name, - data=dict(wf=sampled_waveform.tolist(), - marker=numpy.zeros_like(sampled_waveform).tolist(), - clk=self.__sample_rate)) - # TODO: how to deal with the second channel expected in waveform structs in pulse control? - return struct - - def create_pulse_group(self, sequence: InstructionSequence, name: str)\ - -> Tuple['PulseControlInterface.PulseGroup', List['PulseControlInterface.Pulse']]: - """Construct a dictionary adhering to the pulse group struct definition in pulse control. - - All waveforms in the given InstructionSequence are converted to waveform pulse structs and - returned as a list in the second component of the returned tuple. The first component of - the result is a pulse group dictionary denoting the sequence of waveforms using their - indices in the returned list. create_pulse_group detects multiple use of waveforms and sets - up the pulse group dictionary accordingly. - - Note that pulses are not registered in pulse control. To achieve this and update the pulse - group struct accordingly, the dedicated MATLAB script has to be invoked. - - The function will raise an Exception if the given InstructionBlock does contain branching - instructions, which are not supported by pulse control. - - Arguments: - sequence (InstructionSequence): The InstructionSequence to convert. - name (str): Value for the name field in the resulting pulse group dictionary. - Returns: - a dictionary representing sequence as pulse group struct for pulse control - """ - if [x for x in sequence if not isinstance(x, (EXECInstruction, STOPInstruction))]: - raise Exception("Hardware based branching is not supported by pulse-control.") - - waveforms = [instruction.waveform - for instruction in sequence if isinstance(instruction, EXECInstruction)] - - pulse_group = dict(pulses=[], - nrep=[], - name=name, - chan=1, - ctrl='notrig') - - waveform_ids = dict() - waveform_structs = list() - - for waveform in waveforms: - if waveform not in waveform_ids: - name = self.__get_waveform_name(waveform) - waveform_struct = self.create_waveform_struct(waveform, name) - waveform_structs.append(waveform_struct) - index = len(waveform_structs) - 1 - waveform_ids[waveform] = index - else: - index = waveform_ids[waveform] - if pulse_group['pulses'] and pulse_group['pulses'][-1] == index: - pulse_group['nrep'][-1] += 1 - else: - pulse_group['pulses'].append(index) - pulse_group['nrep'].append(1) - - return (pulse_group, waveform_structs) From a8fd31550702b9c6b9240847a733dcab06d9e8f0 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:43:44 +0200 Subject: [PATCH 071/116] Remove type_check (not used at all, what was it for in the first place?) --- qctoolkit/utils/type_check.py | 117 --------------- tests/utils/type_check_tests.py | 243 -------------------------------- 2 files changed, 360 deletions(-) delete mode 100644 qctoolkit/utils/type_check.py delete mode 100644 tests/utils/type_check_tests.py diff --git a/qctoolkit/utils/type_check.py b/qctoolkit/utils/type_check.py deleted file mode 100644 index 6e54955ed..000000000 --- a/qctoolkit/utils/type_check.py +++ /dev/null @@ -1,117 +0,0 @@ -"""RELATED THIRD PARTY IMPORTS""" - -"""LOCAL IMPORTS""" -import inspect -import logging - -logger = logging.getLogger(__name__) -def __equalTypes(x, y, same_length: bool = True) -> bool: - """Test whether x and y are of (or reference to) the same type or not. - - This strictly internal function compares the type of two variables and the returned - value (bool) is True if they have the same type or if one is the type of the other. - - The argument "same_length" allows to describe schemes. So for instance if you want - to declare an array of a type of variable length, you just have to set the - "same_length" flag to false. - - """ - # If the variable has no annotation, every variable is a right variable - if inspect._empty in [x,y]: - return True - x_type = type(x) - if type(x) is type: - x_type = x - y_type = type(y) - if type(y) is type: - y_type = y - if y_type is not x_type: - return False - - #From here on, x_type can be seen as equal to y_type - sequence_types = (list,dict,tuple) - if x_type in sequence_types: - if same_length and len(x) != len(y): - return False - else: - if type(x) is list: - equal_subtypes = True - index = 0 - while equal_subtypes and index < max(len(x),len(y)): - # This can also handle iterables of unequal lengths - equal_subtypes = equal_subtypes and __equalTypes(x[index%len(x)],y[index%len(y)],same_length) - index += 1 - return equal_subtypes - elif type(x) is dict: - # TODO: Fix this quadratic check - for key_x in x: - for key_y in y: - if __equalTypes(key_x, key_y): - return __equalTypes(x[key_x], y[key_y],same_length) - return False - return False - return True - -def typecheck(same_length: bool = False, raise_on_error: bool = False, log: bool = True): - """Decorator for functions, invokes typechecking on their annotation - - This function invokes the typechecking on functions which uses the annotations of - python3. This type check will happen in runtime. - - For the "same_length" argument, please reference to "__equal_types". - - There are two ways, this function can react on type errors: - 1. "raise_on_error" will raise an "MismatchingTypeException" defined below. - 2. "log" will create a logging message on the warning level. - - Usage: - "@typecheck: - 'def f(x):" - - """ - def check(f): - #Params needed because it is an ORDERED DICTIONARY, so we can derive the variable assignement - params = inspect.signature(f).parameters - def new_f(*args, **kwargs): - #Checking keyword arguments. - for key in kwargs: - if not __equalTypes(params[key].annotation, (kwargs[key]),same_length): - if log: - logger.warning("Type Checking Error in function '{3}': Mismatching types for keyword argument {0}: {1} passed but {2} expected!".format(key,type(kwargs[key]),params[key].annotation,f.__name__)) - if raise_on_error: - raise MismatchingTypesException("Keyword argument {}".format(key)) - - #Checking non keyword arguments - for index, key in enumerate([key for key in params if key not in kwargs and key != "return"]): - if not __equalTypes(params[key].annotation,args[index],same_length): - if log: - logger.warning("Type Checking Error in function '{3}': Mismatching types for keywordless argument {0}: {1} passed but {2} expected!".format(key,type(args[index]),params[key].annotation,f.__name__)) - if raise_on_error: - raise MismatchingTypesException("Non-Keyword argument {}".format(key)) - return_value = f(*args, **kwargs) - - #Checking the return value - if "return" in f.__annotations__: - if not __equalTypes(return_value,f.__annotations__["return"],same_length): - if log: - logger.warning("Type Checking Error in function '{2}': Mismatching return value: {0} returned but {1} expected!".format(type(return_value),f.__annotations__["return"],f.__name__)) - if raise_on_error: - raise MismatchingTypesException("Return value") - return return_value - return new_f - return check - -class MismatchingTypesException(Exception): - """Exception raised when a type error occurs in "type_check" - - Has the behaviour of an default Exception due inheritance of the self. - - """ - def __init__(self,message) -> None: - super().__init__() - self.message = message - - def __str__(self, *args, **kwargs) -> str: - return "Mismatching types Exception:{}".format(self.message) - - diff --git a/tests/utils/type_check_tests.py b/tests/utils/type_check_tests.py deleted file mode 100644 index 66b6479a0..000000000 --- a/tests/utils/type_check_tests.py +++ /dev/null @@ -1,243 +0,0 @@ -import unittest - -from qctoolkit.utils.type_check import typecheck,MismatchingTypesException - -INTEGERS = [0,1,-1] -BOOLEANS = [True,False] -COMPLEX = [1j,2+3j,-1-1j,3-1j,-4+4j] -FLOATS = [0.0,1.0,-1.0] -STRINGS = ["foo","bar"] -TYPES = [INTEGERS,BOOLEANS,COMPLEX,FLOATS,STRINGS] -TYPECHECKARGS = {"raise_on_error":True,"log":False} - - -class BaseTypeCheckTest(unittest.TestCase): - def assertNotRaises(self,exception,function,*args,**kwargs) -> None: - try: - function(*args,**kwargs) - except exception: - self.assertFalse(True,"{} raised by {}".format(exception.__name__,function.__name__)) - - def _int_invoke_test(self,value) -> None: - - @typecheck(**TYPECHECKARGS) - def f(x:int): - pass - - f(value) - - def _float_invoke_test(self,value) -> None: - - @typecheck(**TYPECHECKARGS) - def f(x:float): - pass - - f(value) - - def _bool_invoke_test(self,value) -> None: - - @typecheck(**TYPECHECKARGS) - def f(x:bool): - pass - - f(value) - - def _complex_invoke_test(self,value) -> None: - - @typecheck(**TYPECHECKARGS) - def f(x:complex): - pass - - f(value) - - def _str_invoke_test(self,value) -> None: - - @typecheck(**TYPECHECKARGS) - def f(x:str): - pass - - f(value) - - def _int_return_test(self,value): - - @typecheck(**TYPECHECKARGS) - def f(x) -> int: - return x - - f(value) - - def _float_return_test(self,value): - - @typecheck(**TYPECHECKARGS) - def f(x) -> float: - return x - - f(value) - - def _bool_return_test(self,value): - - @typecheck(**TYPECHECKARGS) - def f(x) -> bool: - return x - - f(value) - - def _complex_return_test(self,value): - - @typecheck(**TYPECHECKARGS) - def f(x) -> complex: - return x - - f(value) - - def _str_return_test(self,value): - - @typecheck(**TYPECHECKARGS) - def f(x) -> str: - return x - - f(value) - - def test_type_values(self): - for type in TYPES: - for value in type: - - # Integers - if type is INTEGERS: - self.assertNotRaises(MismatchingTypesException, self._int_invoke_test,value) - self.assertNotRaises(MismatchingTypesException, self._int_return_test,value) - else: - self.assertRaises(MismatchingTypesException, self._int_invoke_test,value) - self.assertRaises(MismatchingTypesException, self._int_return_test,value) - - # Booleans - if type is BOOLEANS: - self.assertNotRaises(MismatchingTypesException, self._bool_invoke_test,value) - self.assertNotRaises(MismatchingTypesException, self._bool_return_test,value) - else: - self.assertRaises(MismatchingTypesException, self._bool_invoke_test,value) - self.assertRaises(MismatchingTypesException, self._bool_return_test,value) - - # Complex - if type is COMPLEX: - self.assertNotRaises(MismatchingTypesException, self._complex_invoke_test,value) - self.assertNotRaises(MismatchingTypesException, self._complex_return_test,value) - else: - self.assertRaises(MismatchingTypesException, self._complex_invoke_test,value) - self.assertRaises(MismatchingTypesException, self._complex_return_test,value) - - # Floats - if type is FLOATS: - self.assertNotRaises(MismatchingTypesException, self._float_invoke_test,value) - self.assertNotRaises(MismatchingTypesException, self._float_return_test,value) - else: - self.assertRaises(MismatchingTypesException, self._float_invoke_test,value) - self.assertRaises(MismatchingTypesException, self._float_return_test,value) - - # Strings - if type is STRINGS: - self.assertNotRaises(MismatchingTypesException, self._str_invoke_test,value) - self.assertNotRaises(MismatchingTypesException, self._str_return_test,value) - else: - self.assertRaises(MismatchingTypesException, self._str_invoke_test,value) - self.assertRaises(MismatchingTypesException, self._str_return_test,value) - -class ListCheckTest(unittest.TestCase): - def assertNotRaises(self,exception,function,*args,**kwargs) -> None: - try: - function(*args,**kwargs) - except exception: - self.assertFalse(True,"{} raised by {}".format(exception.__name__,function.__name__)) - - def test_list_as_argument(self): - - @typecheck(**TYPECHECKARGS) - def g(x:[int]): - pass - - self.assertRaises(MismatchingTypesException, g,[STRINGS[0]]) - self.assertNotRaises(MismatchingTypesException, g,[INTEGERS[0]]) - - def test_list_as_return(self): - @typecheck(**TYPECHECKARGS) - def g(x) -> [int]: - return [x] - - self.assertRaises(MismatchingTypesException, g,STRINGS[0]) - self.assertNotRaises(MismatchingTypesException, g,INTEGERS[0]) - - def test_nested_list(self): - @typecheck(**TYPECHECKARGS) - def g(x,y) -> [[int],[str]]: - return [[x],[y]] - - self.assertRaises(MismatchingTypesException, g,STRINGS[0],INTEGERS[0]) - self.assertNotRaises(MismatchingTypesException, g,INTEGERS[0],STRINGS[0]) - -class DictionaryCheckTest(unittest.TestCase): - def assertNotRaises(self,exception,function,*args,**kwargs) -> None: - try: - function(*args,**kwargs) - except exception: - self.assertFalse(True,"{} raised by {}".format(exception.__name__,function.__name__)) - - def test_dict_as_argument(self): - @typecheck(**TYPECHECKARGS) - def g(x:{str:int}): - pass - - self.assertNotRaises(MismatchingTypesException,g,{STRINGS[0]:INTEGERS[0],STRINGS[1]:INTEGERS[1]}) - self.assertRaises(MismatchingTypesException,g,{STRINGS[0],STRINGS[1]}) - - def test_dict_as_return(self): - @typecheck(**TYPECHECKARGS) - def g(x,y) -> {str:int}: - return {x:y} - - self.assertNotRaises(MismatchingTypesException, g,STRINGS[0],INTEGERS[0]) - self.assertRaises(MismatchingTypesException, g,INTEGERS[0],STRINGS[0]) - - def test_nested_dict(self): - @typecheck(**TYPECHECKARGS) - def g(x,y) -> {int:{str:int}}: - return {int:{y:x}} - - self.assertRaises(MismatchingTypesException, g,STRINGS[0],INTEGERS[0]) - self.assertNotRaises(MismatchingTypesException, g,INTEGERS[0],STRINGS[0]) - -class CustomClassTest(unittest.TestCase): - def assertNotRaises(self,exception,function,*args,**kwargs) -> None: - try: - function(*args,**kwargs) - except exception: - self.assertFalse(True,"{} raised by {}".format(exception.__name__,function.__name__)) - - - def test_custom_as_argument(self): - class A(object): - pass - - @typecheck(**TYPECHECKARGS) - def g(x:A): - pass - - self.assertNotRaises(MismatchingTypesException, g, A()) - self.assertRaises(MismatchingTypesException, g, 1) - - def test_custom_as_return(self): - class A(object): - pass - - @typecheck(**TYPECHECKARGS) - def g(x)-> A: - return x - - self.assertNotRaises(MismatchingTypesException, g, A()) - self.assertRaises(MismatchingTypesException, g, 1) - -class CustomSubClassTest(unittest.TestCase): - pass - -if __name__ == "__main__": - unittest.main(verbosity=2) - \ No newline at end of file From f7a5da564fd948621b34d1f46e5a969cc4179338 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:45:21 +0200 Subject: [PATCH 072/116] Remove qcmatlab tests --- tests/qcmatlab/__init__.py | 3 - tests/qcmatlab/pulse_control_tests.py | 83 --------------------------- 2 files changed, 86 deletions(-) delete mode 100644 tests/qcmatlab/__init__.py delete mode 100644 tests/qcmatlab/pulse_control_tests.py diff --git a/tests/qcmatlab/__init__.py b/tests/qcmatlab/__init__.py deleted file mode 100644 index 2ae5764a5..000000000 --- a/tests/qcmatlab/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -__all__ = [ - 'pulse_control_tests' -] \ No newline at end of file diff --git a/tests/qcmatlab/pulse_control_tests.py b/tests/qcmatlab/pulse_control_tests.py deleted file mode 100644 index dc731c35a..000000000 --- a/tests/qcmatlab/pulse_control_tests.py +++ /dev/null @@ -1,83 +0,0 @@ -import unittest -import numpy -import numpy.random -from qctoolkit.qcmatlab.pulse_control import PulseControlInterface -from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstructionBlock - - -class PulseControlInterfaceTests(unittest.TestCase): - - def test_create_waveform_struct(self) -> None: - name = 'foo' - sample_rate = 10 - expected_samples = numpy.random.rand(11) - - waveform = DummyWaveform(duration=1, sample_output=expected_samples) - pci = PulseControlInterface(sample_rate, time_scaling=1) - result = pci.create_waveform_struct(waveform, name=name) - - expected_sample_times = numpy.linspace(0, 1, 11).tolist() - self.assertAlmostEqual(expected_sample_times, waveform.sample_calls[0][1]) - expected_result = dict(name=name, - data=dict(wf=expected_samples.tolist(), - marker=numpy.zeros_like(expected_samples).tolist(), - clk=sample_rate - ) - ) - self.assertEqual(expected_result, result) - - def test_create_pulse_group_empty(self) -> None: - name = 'foo_group' - sample_rate = 10 - block = DummyInstructionBlock() - - pci = PulseControlInterface(sample_rate) - (result, _) = pci.create_pulse_group(block, name=name) - expected_result = dict( - name=name, - nrep=[], - pulses=[], - chan=1, - ctrl='notrig' - ) - self.assertEqual(expected_result, result) - - def test_create_pulse_group(self) -> None: - name = 'foo_group' - sample_rate = 10 - expected_samples_wf1 = numpy.random.rand(11) - expected_samples_wf2 = numpy.random.rand(11) - block = DummyInstructionBlock() - wf1a = DummyWaveform(duration=1, sample_output=expected_samples_wf1) - wf1b = DummyWaveform(duration=1, sample_output=expected_samples_wf1) - wf2 = DummyWaveform(duration=1, sample_output=expected_samples_wf2) - block.add_instruction_exec(wf1a) - block.add_instruction_exec(wf1b) - block.add_instruction_exec(wf2) - block.add_instruction_exec(wf1a) - - registering_function = lambda x: x['data'] - pci = PulseControlInterface(sample_rate, time_scaling=1) - (result, _) = pci.create_pulse_group(block, name=name) - expected_result = dict( - name=name, - nrep=[2, 1, 1], - pulses=[0, 1, 0], - #pulses=[registering_function(pci.create_waveform_struct(wf1a, name='')), - # registering_function(pci.create_waveform_struct(wf2, name='')), - # registering_function(pci.create_waveform_struct(wf1a, name=''))], - chan=1, - ctrl='notrig' - ) - self.assertEqual(expected_result, result) - - def test_create_pulse_group_invalid_instruction(self) -> None: - name = 'foo_group' - sample_rate = 10 - block = DummyInstructionBlock() - block.add_instruction_goto(DummyInstructionBlock()) - - pci = PulseControlInterface(sample_rate) - with self.assertRaises(Exception): - pci.create_pulse_group(block.compile_sequence(), name=name) - From ead2cfd2a8e5356299429bfebcd55dde55735b21 Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Wed, 2 Aug 2017 15:48:20 +0200 Subject: [PATCH 073/116] Add missing type annotations --- qctoolkit/pulses/point_pulse_template.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qctoolkit/pulses/point_pulse_template.py b/qctoolkit/pulses/point_pulse_template.py index 7a09282c1..e278bb9ad 100644 --- a/qctoolkit/pulses/point_pulse_template.py +++ b/qctoolkit/pulses/point_pulse_template.py @@ -88,10 +88,10 @@ def build_waveform(self, for channel, ch_entries in zip(mapped_channels[1:], waveform_entries[1:])]) @property - def point_pulse_entries(self): + def point_pulse_entries(self) -> Sequence[PointPulseEntry]: return self._entries - def get_serialization_data(self, serializer): + def get_serialization_data(self, serializer) -> Dict: data = {'time_point_tuple_list': [(t.get_most_simple_representation(), v.get_most_simple_representation(), str(interp)) @@ -104,7 +104,7 @@ def get_serialization_data(self, serializer): return data @staticmethod - def deserialize(serializer, **kwargs): + def deserialize(serializer, **kwargs) -> 'PointPulseTemplate': return PointPulseTemplate(**kwargs) @property From d1011a96c7dad327ae49fd45a3a38621f9f8056d Mon Sep 17 00:00:00 2001 From: "Simon Humpohl (Lab PC)" Date: Thu, 3 Aug 2017 11:40:48 +0200 Subject: [PATCH 074/116] First draft of hardware example --- doc/source/examples/HardwareSetup.ipynb | 185 ++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 doc/source/examples/HardwareSetup.ipynb diff --git a/doc/source/examples/HardwareSetup.ipynb b/doc/source/examples/HardwareSetup.ipynb new file mode 100644 index 000000000..0bee30061 --- /dev/null +++ b/doc/source/examples/HardwareSetup.ipynb @@ -0,0 +1,185 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel\n", + "hardware_setup = HardwareSetup()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Failed to open \"USB0::0x168C::0x2184::0000216488::INSTR\"\n(, VisaIOError('VI_ERROR_RSRC_NFOUND (-1073807343): Insufficient location information or the requested device or resource is not present in the system.',), )\n" + ] + }, + { + "ename": "AttributeError", + "evalue": "'NoneType' object has no attribute 'write'", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m# small wrapper around pytabor\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mtawg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTaborAWGRepresentation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'USB0::0x168C::0x2184::0000216488::INSTR'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreset\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[0mtawg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparanoia_level\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\hardware\\awgs\\tabor.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, external_trigger, reset, *args, **kwargs)\u001b[0m\n\u001b[0;32m 311\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 312\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mreset\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 313\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvisa_inst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m':RES'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 314\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 315\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mexternal_trigger\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'write'" + ], + "output_type": "error" + } + ], + "source": [ + "from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation\n", + "\n", + "tabor_address = r'USB0::0x168C::0x2184::0000216488::INSTR'\n", + "\n", + "# small wrapper around pytabor\n", + "tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR', reset=True)\n", + "tawg.paranoia_level = 2\n", + "\n", + "# actual awg driver object\n", + "tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0))\n", + "hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1))\n", + "hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair, 0))\n", + "hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair, 1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "ename": "ModuleNotFoundError", + "evalue": "No module named 'atsaverage'", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0matsaverage\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLogger\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog_path\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34mr'C:\\Users\\Public\\AtsAverageLog'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLogger\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrotate_file\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'atsaverage'" + ], + "output_type": "error" + } + ], + "source": [ + "import atsaverage\n", + "import atsaverage.atsaverage\n", + "atsaverage.atsaverage.Logger.log_path = r'C:\\Users\\Public\\AtsAverageLog'\n", + "atsaverage.atsaverage.Logger.rotate_file()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import atsaverage.atsaverage\n", + "atsaverage.atsaverage.Logger.log_path = r'C:\\Users\\Public\\AtsAverageLog'\n", + "atsaverage.atsaverage.Logger.rotate_file()\n", + "from qctoolkit.hardware.dacs.alazar import AlazarCard\n", + "\n", + "alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1))\n", + "\n", + "# mask MyMask will be applied/refers to data from channel 0\n", + "alazar.register_mask_for_channel('MyMask', 0)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def get_alazar_config():\n", + " from atsaverage import alazar\n", + " from atsaverage.config import ScanlineConfiguration, CaptureClockConfiguration, EngineTriggerConfiguration,\\\n", + " TRIGInputConfiguration, InputConfiguration\n", + " \n", + " r = 2.5\n", + " rid = alazar.TriggerRangeID.etr_2V5\n", + "\n", + " trig_level = int((r + 0.15) / (2*r) * 255)\n", + " assert 0 <= trig_level < 256\n", + "\n", + " config = ScanlineConfiguration()\n", + " config.triggerInputConfiguration = TRIGInputConfiguration(triggerRange=rid)\n", + " config.triggerConfiguration = EngineTriggerConfiguration(triggerOperation=alazar.TriggerOperation.J,\n", + " triggerEngine1=alazar.TriggerEngine.J,\n", + " triggerSource1=alazar.TriggerSource.external,\n", + " triggerSlope1=alazar.TriggerSlope.positive,\n", + " triggerLevel1=trig_level,\n", + " triggerEngine2=alazar.TriggerEngine.K,\n", + " triggerSource2=alazar.TriggerSource.disable,\n", + " triggerSlope2=alazar.TriggerSlope.positive,\n", + " triggerLevel2=trig_level)\n", + " config.captureClockConfiguration = CaptureClockConfiguration(source=alazar.CaptureClockType.internal_clock,\n", + " samplerate=alazar.SampleRateID.rate_100MSPS)\n", + " config.inputConfiguration = 4*[InputConfiguration(input_range=alazar.InputRangeID.range_1_V)]\n", + " config.totalRecordSize = 0\n", + " config.aimedBufferSize = 10*config.aimedBufferSize\n", + "\n", + " # is set automatically\n", + " assert config.totalRecordSize == 0\n", + " \n", + " return config" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "alazar.config = get_alazar_config()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 12e1faa0dfb1975866e35b69b3df8806727bdab3 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 3 Aug 2017 13:55:55 +0200 Subject: [PATCH 075/116] Remove qcmatlab from setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 91f314551..5f94f6749 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ else: requires_typing = [] -subpackages = ['pulses', 'utils', 'qcmatlab', 'hardware'] +subpackages = ['pulses', 'utils', 'hardware'] packages = ['qctoolkit'] + ['qctoolkit.' + subpackage for subpackage in subpackages] setup(name='qctoolkit', From cb5a15715cc56ed0f8c8aaa0ad7fc7d0d4cb0fb6 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 3 Aug 2017 15:55:02 +0200 Subject: [PATCH 076/116] Remove implicit float conversion of parameter --- qctoolkit/pulses/parameters.py | 17 +++++++---------- tests/pulses/parameters_tests.py | 8 -------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index 1cd9e593e..624f6f04c 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -59,7 +59,7 @@ def __init__(self) -> None: super().__init__(None) @abstractmethod - def get_value(self) -> float: + def get_value(self) -> Real: """Compute and return the parameter value.""" @abstractproperty @@ -71,9 +71,6 @@ def requires_stop(self) -> bool: Returns: True, if evaluating this Parameter instance requires an interruption. """ - - def __float__(self) -> float: - return float(self.get_value()) @property def compare_key(self) -> Any: @@ -83,16 +80,16 @@ def compare_key(self) -> Any: class ConstantParameter(Parameter): """A pulse parameter with a constant value.""" - def __init__(self, value: float) -> None: + def __init__(self, value: Real) -> None: """Create a ConstantParameter instance. Args: - value (float): The value of the parameter + value (Real): The value of the parameter """ super().__init__() self.__value = value - def get_value(self) -> float: + def get_value(self) -> Real: return self.__value @property @@ -106,7 +103,7 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: return dict(type=serializer.get_type_identifier(self), constant=self.__value) @staticmethod - def deserialize(serializer: Serializer, constant: float) -> 'ConstantParameter': + def deserialize(serializer: Serializer, constant: Real) -> 'ConstantParameter': return ConstantParameter(constant) @@ -147,12 +144,12 @@ def __collect_dependencies(self) -> Iterable[Parameter]: except KeyError as key_error: raise ParameterNotProvidedException(str(key_error)) from key_error - def get_value(self) -> float: + def get_value(self) -> Real: if self.requires_stop: raise Exception("Cannot evaluate MappedParameter because at least one dependency " "cannot be evaluated.") dependencies = self.__collect_dependencies() - variables = {k: float(dependencies[k]) for k in dependencies} + variables = {k: dependencies[k].get_value() for k in dependencies} return self.__expression.evaluate_numeric(**variables) @property diff --git a/tests/pulses/parameters_tests.py b/tests/pulses/parameters_tests.py index d62e9f17b..86fc8ab93 100644 --- a/tests/pulses/parameters_tests.py +++ b/tests/pulses/parameters_tests.py @@ -9,18 +9,10 @@ from tests.pulses.sequencing_dummies import DummyParameter -class ParameterTest(unittest.TestCase): - - def test_float_conversion_method(self) -> None: - parameter = DummyParameter() - self.assertEqual(parameter.value, float(parameter)) - - class ConstantParameterTest(unittest.TestCase): def __test_valid_params(self, value: float) -> None: constant_parameter = ConstantParameter(value) self.assertEqual(value, constant_parameter.get_value()) - self.assertEqual(value, float(constant_parameter)) def test_float_values(self) -> None: self.__test_valid_params(-0.3) From 01c7e0c67f7bae7632cdf17f446007d4234369cc Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 14 Aug 2017 15:21:44 +0200 Subject: [PATCH 077/116] Optimize Alazar arming if driver is already configured --- qctoolkit/hardware/dacs/alazar.py | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py index bb81dcacc..37e841ccd 100644 --- a/qctoolkit/hardware/dacs/alazar.py +++ b/qctoolkit/hardware/dacs/alazar.py @@ -25,6 +25,9 @@ class AlazarCard(DAC): def __init__(self, card, config: Optional[ScanlineConfiguration]=None): self.__card = card + self.__armed_program = None + self.update_settings = True + self.__definitions = dict() self.config = config @@ -83,22 +86,26 @@ def register_operations(self, program_name: str, operations) -> None: self._registered_programs[program_name].operations = operations def arm_program(self, program_name: str) -> None: - config = self.config - config.masks, config.operations, total_record_size = self._registered_programs[program_name] - - if not config.masks: - if config.operations: - raise RuntimeError('Invalid configuration. Operations have no masks to work with') - else: - return - - if config.totalRecordSize == 0: - config.totalRecordSize = total_record_size - elif config.totalRecordSize < total_record_size: - raise ValueError('specified total record size is smaller than needed {} < {}'.format(config.totalRecordSize, - total_record_size)) - - config.apply(self.__card, True) + to_arm = self._registered_programs[program_name] + if self.update_settings or self.__armed_program is not to_arm: + config = self.config + config.masks, config.operations, total_record_size = self._registered_programs[program_name] + + if not config.masks: + if config.operations: + raise RuntimeError('Invalid configuration. Operations have no masks to work with') + else: + return + + if config.totalRecordSize == 0: + config.totalRecordSize = total_record_size + elif config.totalRecordSize < total_record_size: + raise ValueError('specified total record size is smaller than needed {} < {}'.format(config.totalRecordSize, + total_record_size)) + config.apply(self.__card, True) + + self.update_settings = False + self.__armed_program = to_arm self.__card.startAcquisition(1) def delete_program(self, program_name: str) -> None: From c678289f5c7a4bc79f9b76217cf3b3a4827818c8 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 14 Aug 2017 15:23:21 +0200 Subject: [PATCH 078/116] Do not import matlplotlib if plotting is not used --- qctoolkit/pulses/__init__.py | 1 - qctoolkit/pulses/plotting.py | 116 ++++++++++++++------------------- tests/pulses/plotting_tests.py | 16 ++--- 3 files changed, 57 insertions(+), 76 deletions(-) diff --git a/qctoolkit/pulses/__init__.py b/qctoolkit/pulses/__init__.py index 9fe322a0c..71fa06de0 100644 --- a/qctoolkit/pulses/__init__.py +++ b/qctoolkit/pulses/__init__.py @@ -4,7 +4,6 @@ from qctoolkit.pulses.loop_pulse_template import * from qctoolkit.pulses.multi_channel_pulse_template import * from qctoolkit.pulses.parameters import * -from qctoolkit.pulses.plotting import * from qctoolkit.pulses.pulse_template import * from qctoolkit.pulses.pulse_template_parameter_mapping import * from qctoolkit.pulses.repetition_pulse_template import * diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index 519ea993b..00f9b7b6f 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -10,7 +10,6 @@ from typing import Dict, Tuple import numpy as np -from matplotlib import pyplot as plt from qctoolkit import ChannelID from qctoolkit.pulses.pulse_template import PulseTemplate @@ -20,77 +19,61 @@ REPJInstruction -__all__ = ["Plotter", "plot", "PlottingNotPossibleException"] +__all__ = ["render", "plot", "PlottingNotPossibleException"] -class Plotter: - """Plotter converts an InstructionSequence compiled by Sequencer from a PulseTemplate structure - into a series of voltage values regularly sampled over the entire time domain for plotting. +def render(sequence: InstructionSequence, sample_rate: int=10) -> Tuple[np.ndarray, Dict[ChannelID, np.ndarray]]: + """'Render' an instruction sequence (sample all contained waveforms into an array). - It currently is not able to handle instruction sequences that contain branching / jumping. + Returns: + a tuple (times, values) of numpy.ndarrays of similar size. times contains the time value + of all sample times and values the corresponding sampled value. """ - - def __init__(self, sample_rate: int=10) -> None: - """Create a new Plotter instance. - - Args: - sample_rate (int): The sample rate in samples per time unit. (default = 10) - """ - super().__init__() - self.__sample_rate = sample_rate - - def render(self, sequence: InstructionSequence) -> Tuple[np.ndarray, Dict[ChannelID, np.ndarray]]: - """'Render' an instruction sequence (sample all contained waveforms into an array). - - Returns: - a tuple (times, values) of numpy.ndarrays of similar size. times contains the time value - of all sample times and values the corresponding sampled value. - """ - if not all(isinstance(x, (EXECInstruction, STOPInstruction, REPJInstruction)) for x in sequence): - raise NotImplementedError('Can only plot waveforms without branching so far.') - - def get_waveform_generator(instruction_block): - for instruction in instruction_block: - if isinstance(instruction, EXECInstruction): - yield instruction.waveform - elif isinstance(instruction, REPJInstruction): - for _ in range(instruction.count): - yield from get_waveform_generator(instruction.target.block[instruction.target.offset:]) - else: - return - - waveforms = [wf for wf in get_waveform_generator(sequence)] - if not waveforms: - return [], [] - - total_time = sum(waveform.duration for waveform in waveforms) - - channels = waveforms[0].defined_channels - - # add one sample to see the end of the waveform - sample_count = total_time * self.__sample_rate + 1 - times = np.linspace(0, total_time, num=sample_count) - # move the last sample inside the waveform - times[-1] = np.nextafter(times[-1], times[-2]) - - voltages = dict((ch, np.empty(len(times))) for ch in channels) - offsets = {ch: 0 for ch in channels} - for waveform in waveforms: - for channel in channels: - offset = offsets[channel] - indices = slice(*np.searchsorted(times, (offset, offset+waveform.duration))) - sample_times = times[indices] - offset - output_array = voltages[channel][indices] - waveform.get_sampled(channel=channel, - sample_times=sample_times, - output_array=output_array) - offsets[channel] += waveform.duration - return times, voltages + if not all(isinstance(x, (EXECInstruction, STOPInstruction, REPJInstruction)) for x in sequence): + raise NotImplementedError('Can only plot waveforms without branching so far.') + + def get_waveform_generator(instruction_block): + for instruction in instruction_block: + if isinstance(instruction, EXECInstruction): + yield instruction.waveform + elif isinstance(instruction, REPJInstruction): + for _ in range(instruction.count): + yield from get_waveform_generator(instruction.target.block[instruction.target.offset:]) + else: + return + + waveforms = [wf for wf in get_waveform_generator(sequence)] + if not waveforms: + return [], [] + + total_time = sum(waveform.duration for waveform in waveforms) + + channels = waveforms[0].defined_channels + + # add one sample to see the end of the waveform + sample_count = total_time * sample_rate + 1 + times = np.linspace(0, total_time, num=sample_count) + # move the last sample inside the waveform + times[-1] = np.nextafter(times[-1], times[-2]) + + voltages = dict((ch, np.empty(len(times))) for ch in channels) + offsets = {ch: 0 for ch in channels} + for waveform in waveforms: + for channel in channels: + offset = offsets[channel] + indices = slice(*np.searchsorted(times, (offset, offset+waveform.duration))) + sample_times = times[indices] - offset + output_array = voltages[channel][indices] + waveform.get_sampled(channel=channel, + sample_times=sample_times, + output_array=output_array) + offsets[channel] += waveform.duration + return times, voltages def plot(pulse: PulseTemplate, parameters: Dict[str, Parameter]=None, - sample_rate: int=10) -> plt.Figure: # pragma: no cover + sample_rate: int=10) -> 'plt.Figure': # pragma: no cover """Plot a pulse using matplotlib. The given pulse will first be sequenced using the Sequencer class. The resulting @@ -110,11 +93,12 @@ def plot(pulse: PulseTemplate, because a parameter value could not be evaluated all Exceptions possibly raised during sequencing """ + from matplotlib import pyplot as plt + channels = pulse.defined_channels if parameters is None: parameters = dict() - plotter = Plotter(sample_rate=sample_rate) sequencer = Sequencer() sequencer.push(pulse, parameters, @@ -123,7 +107,7 @@ def plot(pulse: PulseTemplate, sequence = sequencer.build() if not sequencer.has_finished(): raise PlottingNotPossibleException(pulse) - times, voltages = plotter.render(sequence) + times, voltages = render(sequence, sample_rate) # plot to figure figure = plt.figure() diff --git a/tests/pulses/plotting_tests.py b/tests/pulses/plotting_tests.py index 955b149cd..86c4b29a2 100644 --- a/tests/pulses/plotting_tests.py +++ b/tests/pulses/plotting_tests.py @@ -1,7 +1,7 @@ import unittest import numpy -from qctoolkit.pulses.plotting import Plotter, PlottingNotPossibleException +from qctoolkit.pulses.plotting import PlottingNotPossibleException, render from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate @@ -15,12 +15,12 @@ class PlotterTests(unittest.TestCase): def test_render_unsupported_instructions(self) -> None: block = InstructionBlock() block.add_instruction(DummyInstruction()) - plotter = Plotter() + with self.assertRaises(NotImplementedError): - plotter.render(block) + render(block) def test_render_no_waveforms(self) -> None: - self.assertEqual(([], []), Plotter().render(InstructionBlock())) + self.assertEqual(([], []), render(InstructionBlock())) def test_render(self) -> None: wf1 = DummyWaveform(duration=19) @@ -41,8 +41,7 @@ def test_render(self) -> None: expected_times = numpy.arange(start=0, stop=42, step=2) expected_result = numpy.concatenate((wf1.sample_output, wf2.sample_output)) - plotter = Plotter(sample_rate=0.5) - times, voltages = plotter.render(block) + times, voltages = render(block, sample_rate=0.5) self.assertEqual(len(wf1.sample_calls), 1) self.assertEqual(len(wf2.sample_calls), 1) @@ -88,11 +87,10 @@ def integrated_test_with_sequencer_and_pulse_templates(self) -> None: # run the sequencer and render the plot sample_rate = 20 - plotter = Plotter(sample_rate=sample_rate) - sequencer = Sequencer(plotter) + sequencer = Sequencer() sequencer.push(sequence, parameters) block = sequencer.build() - times, voltages = plotter.render(block) + times, voltages = render(block, sample_rate=sample_rate) # compute expected values expected_times = numpy.linspace(0, 100, sample_rate) From fe56986164cac52574bd51a1cb884cbf1bf93c21 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 14 Aug 2017 15:26:05 +0200 Subject: [PATCH 079/116] ForLoopPulseTemplate casts range variables to int if possible --- qctoolkit/pulses/loop_pulse_template.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index db7070f2f..34ab589cc 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -70,9 +70,17 @@ def to_tuple(self) -> Tuple[Any, Any, Any]: self.step.get_most_simple_representation()) def to_range(self, parameters: Dict[str, Any]) -> range: - return range(self.start.evaluate_numeric(**parameters), - self.stop.evaluate_numeric(**parameters), - self.step.evaluate_numeric(**parameters)) + def to_int(x) -> int: + if isinstance(x, int): + return x + int_x = int(round(x)) + if abs(x-int_x) > 1e-10: + raise ValueError('Non integer in range') + return int_x + + return range(to_int(self.start.evaluate_numeric(**parameters)), + to_int(self.stop.evaluate_numeric(**parameters)), + to_int(self.step.evaluate_numeric(**parameters))) @property def parameter_names(self) -> Set[str]: @@ -96,7 +104,7 @@ def __init__(self, self._loop_range = loop_range elif isinstance(loop_range, (int, str)): self._loop_range = ParametrizedRange(loop_range) - elif isinstance(loop_range, tuple): + elif isinstance(loop_range, (tuple, list)): self._loop_range = ParametrizedRange(*loop_range) elif isinstance(loop_range, range): self._loop_range = ParametrizedRange(start=loop_range.start, From 19e4e83fb69b0dfc3d58f32da80527ae99417fb5 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 14 Aug 2017 15:26:47 +0200 Subject: [PATCH 080/116] Give MappingPulseTemplates identifiers --- qctoolkit/pulses/pulse_template_parameter_mapping.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index 14c50b61a..9ac0a4684 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -27,6 +27,7 @@ class MappingTemplate(PulseTemplate, ParameterConstrainer): def __init__(self, template: PulseTemplate, *, + identifier: Optional[str]=None, parameter_mapping: Optional[Dict[str, str]]=None, measurement_mapping: Optional[Dict[str, str]] = None, channel_mapping: Optional[Dict[ChannelID, ChannelID]] = None, @@ -39,7 +40,7 @@ def __init__(self, template: PulseTemplate, *, :param measurement_mapping: mappings for other measurement names are inserted :param channel_mapping: mappings for other channels are auto inserted """ - PulseTemplate.__init__(self, identifier=None) + PulseTemplate.__init__(self, identifier=identifier) ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) if parameter_mapping is None: @@ -183,9 +184,9 @@ def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: @staticmethod def deserialize(serializer: 'Serializer', - template: Union[str, Dict[str, Any]], - identifier: Optional[str]=None, **kwargs) -> 'MappingTemplate': - return MappingTemplate(template=serializer.deserialize(template), **kwargs) + template: Union[str, Dict[str, Any]], **kwargs) -> 'MappingTemplate': + return MappingTemplate(template=serializer.deserialize(template), + **kwargs) def map_parameters(self, parameters: Dict[str, Union[Parameter, numbers.Real]]) -> Dict[str, Parameter]: From 6bc3efede0d96ea39e932f53b066c9d60cd964b7 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 16 Aug 2017 14:52:29 +0200 Subject: [PATCH 081/116] FIX BUG: permanent delete of loop variable --- qctoolkit/pulses/loop_pulse_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index 34ab589cc..cbf4b3338 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -136,7 +136,7 @@ def duration(self) -> Expression: @property def parameter_names(self) -> Set[str]: - parameter_names = self.body.parameter_names + parameter_names = self.body.parameter_names.copy() parameter_names.remove(self._loop_index) return parameter_names | self._loop_range.parameter_names From bfa411dc9c6eae2474dcbc92d785d372d99ccdc3 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 16 Aug 2017 18:36:06 +0200 Subject: [PATCH 082/116] Fix AtomicMultiChannelPulseTemplate serialization --- qctoolkit/pulses/multi_channel_pulse_template.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 41b043a3e..a868fb828 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -258,13 +258,12 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: @staticmethod def deserialize(serializer: Serializer, subtemplates: Iterable[Dict[str, Any]], - atomicity: bool, - identifier: Optional[str] = None) -> 'MultiChannelPulseTemplate': + parameter_constraints: Any, + identifier: Optional[str] = None) -> 'AtomicMultiChannelPulseTemplate': subtemplates = [serializer.deserialize(st) for st in subtemplates] - external_parameters = set.union(*(st.parameter_names for st in subtemplates)) - mul_template = MultiChannelPulseTemplate(subtemplates, external_parameters, identifier=identifier) - mul_template.atomicity = atomicity - return mul_template + return AtomicMultiChannelPulseTemplate(*subtemplates, + parameter_constraints=parameter_constraints, + identifier=identifier) class MultiChannelPulseTemplate(PulseTemplate): From bdbe89869ca4132b7bb058032a27e8a5e0fced82 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 16 Aug 2017 18:40:33 +0200 Subject: [PATCH 083/116] Remove MultiChannelPulseTemplate for now. To be added again later on --- .../pulses/multi_channel_pulse_template.py | 180 +--------------- .../multi_channel_pulse_template_tests.py | 192 +----------------- 2 files changed, 7 insertions(+), 365 deletions(-) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index a868fb828..d545c6674 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -25,7 +25,7 @@ from qctoolkit.pulses.conditions import Condition from qctoolkit.expressions import Expression -__all__ = ["MultiChannelWaveform", "MultiChannelPulseTemplate"] +__all__ = ["MultiChannelWaveform", "AtomicMultiChannelPulseTemplate"] def _parse_subtemplates(subtemplates): @@ -44,10 +44,6 @@ def _parse_subtemplates(subtemplates): return subtemplates -def split_mappings(templates, mappings): - pass - - class MultiChannelWaveform(Waveform): """A MultiChannelWaveform is a Waveform object that allows combining arbitrary Waveform objects to into a single waveform defined for several channels. @@ -266,180 +262,6 @@ def deserialize(serializer: Serializer, identifier=identifier) -class MultiChannelPulseTemplate(PulseTemplate): - """A multi-channel group of several AtomicPulseTemplate objects. - - While SequencePulseTemplate combines several subtemplates (with an identical number of channels) - for subsequent execution, MultiChannelPulseTemplate combines several subtemplates into a new - template defined for the sum of the channels of the subtemplates. - A constraint is that the subtemplates must only be AtomicPulseTemplate objects, i.e., not define - any changes in the control flow and be directly translatable into waveforms. Additionally, - for each possible set of parameter value assignments, the waveforms resulting from the templates - must be of the same duration. However, this cannot be enforced during the construction of the - MultiChannelPulseTemplate object. Instead, if subtemplate misbehave, an exception will be raised - during translation in the build_waveform and build_sequence methods. - - MultiChannelPulseTemplate allows an arbitrary mapping of channels defined by the subtemplates - and the channels it defines. For example, if the MultiChannelPulseTemplate consists - of a two subtemplates A and B which define two channels each, then the channels of the - MultiChannelPulseTemplate may be 0: A.1, 1: B.0, 2: B.1, 3: A.0 where A.0 means channel 0 of A. - The channel mapping must be sane, i.e., to no channel of the MultiChannelPulseTemplate must be - assigned more than one channel of any subtemplate. - - Finally, MultiChannelPulseTemplate allows a mapping of parameter in the same way and with - the same constraints as SequencePulseTemplate. - - See Also: - - SequencePulseTemplate - - AtomicPulseTemplate - - MultiChannelWaveform - """ - - def __init__(self, - subtemplates: Iterable[Union[PulseTemplate, MappingTuple]], - external_parameters: Set[str], - identifier: str=None) -> None: - """Creates a new MultiChannelPulseTemplate instance. - - Requires a list of subtemplates in the form - (PulseTemplate, Dict(str -> str), List(int)) where the dictionary is a mapping between the - external parameters exposed by this PulseTemplate to the parameters declared by the - subtemplates, specifying how the latter are derived from the former, i.e., the mapping is - subtemplate_parameter_name -> mapping_expression (as str) where the free variables in the - mapping_expression are parameters declared by this MultiChannelPulseTemplate. - The list defines the channel mapping, i.e., a value y at index x in the list means that - channel x of the subtemplate will be mapped to channel y of this MultiChannelPulseTemplate. - - Args: - subtemplates (List(Subtemplate)): The list of subtemplates of this - MultiChannelPulseTemplate as tuples of the form - (PulseTemplate, Dict(str -> str), List(int)). - external_parameters (List(str)): A set of names for external parameters of this - MultiChannelPulseTemplate. - identifier (str): A unique identifier for use in serialization. (optional) - Raises: - ValueError, if a channel mapping is out of bounds of the channels defined by this - MultiChannelPulseTemplate. - ValueError, if several subtemplate channels are assigned to a single channel of this - MultiChannelPulseTemplate. - MissingMappingException, if a parameter of a subtemplate is not mapped to the external - parameters of this MultiChannelPulseTemplate. - MissingParameterDeclarationException, if a parameter mapping requires a parameter - that was not declared in the external parameters of this MultiChannelPulseTemplate. - """ - super().__init__(identifier=identifier) - - self._subtemplates = [st if isinstance(st, PulseTemplate) else MappingTemplate.from_tuple(st) for st in subtemplates] - if not self._subtemplates: - raise ValueError('Cannot create empty MultiChannelPulseTemplate') - - defined_channels = [st.defined_channels for st in self._subtemplates] - - # check there are no intersections between channels - for i, channels_i in enumerate(defined_channels): - for j, channels_j in enumerate(defined_channels[i+1:]): - if channels_i & channels_j: - raise ChannelMappingException(self._subtemplates[i], - self._subtemplates[i + 1 + j], - (channels_i | channels_j).pop()) - - remaining = external_parameters.copy() - for subtemplate in self._subtemplates: - missing = subtemplate.parameter_names - external_parameters - if missing: - raise MissingParameterDeclarationException(subtemplate.template, missing.pop()) - remaining -= subtemplate.parameter_names - if remaining: - raise MissingMappingException(subtemplate.template, remaining.pop()) - - @property - def parameter_names(self) -> Set[str]: - return set.union(*(st.parameter_names for st in self._subtemplates)) - - @property - def subtemplates(self) -> Iterable[MappingTemplate]: - return iter(self._subtemplates) - - @property - def duration(self) -> Expression: - durations = [subtemplate.duration for subtemplate in self.subtemplates] - equality_condition = ','.join('Eq({}, {})'.format(d1, d2) for d1, d2 in zip(durations, durations[1:])) - return Expression('Piecewise( ({duration}, And({condition})), (nan, True))'.format(duration=durations[0], - condition=equality_condition)) - - @property - def is_interruptable(self) -> bool: - return all(st.is_interruptable for st in self.subtemplates) - - @property - def defined_channels(self) -> Set[ChannelID]: - return set.union(*(st.defined_channels for st in self._subtemplates)) - - @property - def measurement_names(self) -> Set[str]: - return set.union(*(st.measurement_names for st in self._subtemplates)) - - def build_waveform(self, parameters: Dict[str, Parameter], - measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['MultiChannelWaveform']: - return MultiChannelWaveform( - [subtemplate.build_waveform(parameters, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping) for subtemplate in self._subtemplates]) - - def build_sequence(self, - sequencer: 'Sequencer', - parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition'], - measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], - instruction_block: InstructionBlock) -> None: - - atomic_build = False - if all(subtemplate.atomicity for subtemplate in self.subtemplates): - duration = self.duration - duration = duration.evaluate_numeric(**dict((k, parameters[k].get_value()) - for k in duration.variables)) - if duration >= 0: - atomic_build = True - - if atomic_build: - waveform = self.build_waveform(parameters={k: parameters[k].get_value() for k in self.parameter_names}, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping) - if waveform: - instruction_block.add_instruction_exec(waveform) - else: - channel_to_instruction_block = dict() - for subtemplate in self.subtemplates: - block = InstructionBlock() - sequencer.push(subtemplate, parameters, conditions, measurement_mapping, channel_mapping, block) - channel_to_instruction_block.update(dict.fromkeys(subtemplate.defined_channels, block)) - instruction_block.add_instruction_chan(channel_to_instruction=channel_to_instruction_block) - - def requires_stop(self, - parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition']) -> bool: - return any(st.requires_stop(parameters, conditions) for st in self._subtemplates) - - def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: - data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], - atomicity=self.atomicity, - type=serializer.get_type_identifier(self)) - return data - - @staticmethod - def deserialize(serializer: Serializer, - subtemplates: Iterable[Dict[str, Any]], - atomicity: bool, - identifier: Optional[str]=None) -> 'MultiChannelPulseTemplate': - subtemplates = [serializer.deserialize(st) for st in subtemplates] - external_parameters = set.union(*(st.parameter_names for st in subtemplates)) - mul_template = MultiChannelPulseTemplate(subtemplates, external_parameters, identifier=identifier) - mul_template.atomicity = atomicity - return mul_template - - class ChannelMappingException(Exception): def __init__(self, obj1, obj2, intersect_set): self.intersect_set = intersect_set diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index 91080ef18..5213e95f9 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -5,7 +5,7 @@ from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ MissingParameterDeclarationException, UnnecessaryMappingException from qctoolkit.pulses.parameters import ParameterNotProvidedException, MappedParameter, ConstantParameter -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelPulseTemplate, MultiChannelWaveform, MappingTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform, MappingTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate from qctoolkit.expressions import Expression from qctoolkit.pulses.instructions import CHANInstruction, EXECInstruction @@ -13,8 +13,8 @@ from tests.serialization_dummies import DummySerializer from tests.pulses.pulse_template_tests import PulseTemplateStub -class MultiChannelWaveformTest(unittest.TestCase): +class MultiChannelWaveformTest(unittest.TestCase): def test_init_no_args(self) -> None: with self.assertRaises(ValueError): MultiChannelWaveform(dict()) @@ -250,188 +250,8 @@ def test_defined_channels(self): template = AtomicMultiChannelPulseTemplate(*subtemp_args) self.assertEqual(template.defined_channels, {'cc1', 'cc2', 'cc3'}) + def test_deserialization(self): + self.assertTrue(False) -class MultiChannelPulseTemplateTest(unittest.TestCase): - def __init__(self,*args,**kwargs): - super().__init__(*args,**kwargs) - - self.subtemplates = [DummyPulseTemplate(parameter_names={'p1'}, measurement_names={'m1'}, defined_channels={'c1'}, - requires_stop=True, is_interruptable=False), - DummyPulseTemplate(parameter_names={'p2'}, measurement_names={'m2'}, defined_channels={'c2'}, - requires_stop=False, is_interruptable=True), - DummyPulseTemplate(parameter_names={'p3'}, measurement_names={'m3'}, defined_channels={'c3'}, - requires_stop=False, is_interruptable=True)] - self.no_param_maps = [{'p1': '1'}, {'p2': '2'}, {'p3': '3'}] - self.param_maps = [{'p1': 'pp1'}, {'p2': 'pp2'}, {'p3': 'pp3'}] - self.chan_maps = [{'c1': 'cc1'}, {'c2': 'cc2'}, {'c3': 'cc3'}] - - def test_init_empty(self) -> None: - with self.assertRaises(ValueError): - MultiChannelPulseTemplate([], {}, identifier='foo') - - def test_mapping_template_pure_conversion(self): - subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] - template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) - - for st, pm, cm in zip(template.subtemplates, self.param_maps, self.chan_maps): - self.assertEqual(st.parameter_names, set(pm.values())) - self.assertEqual(st.defined_channels, set(cm.values())) - - def test_mapping_template_mixed_conversion(self): - subtemp_args = [ - (self.subtemplates[0], self.param_maps[0], self.chan_maps[0]), - MappingTemplate(self.subtemplates[1], parameter_mapping=self.param_maps[1], channel_mapping=self.chan_maps[1]), - (self.subtemplates[2], self.param_maps[2], self.chan_maps[2]) - ] - template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) - - for st, pm, cm in zip(template.subtemplates, self.param_maps, self.chan_maps): - self.assertEqual(st.parameter_names, set(pm.values())) - self.assertEqual(st.defined_channels, set(cm.values())) - - def test_channel_intersection(self): - chan_maps = self.chan_maps.copy() - chan_maps[-1]['c3'] = 'cc1' - with self.assertRaises(ChannelMappingException): - MultiChannelPulseTemplate(zip(self.subtemplates, self.param_maps, chan_maps), external_parameters={'pp1', 'pp2', 'pp3'}) - - def test_external_parameter_error(self): - subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] - with self.assertRaises(MissingParameterDeclarationException): - MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2'}) - with self.assertRaises(MissingMappingException): - MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3', 'foo'}) - - def test_defined_channels(self): - subtemp_args = [*zip(self.subtemplates, self.param_maps, self.chan_maps)] - template = MultiChannelPulseTemplate(subtemp_args, external_parameters={'pp1', 'pp2', 'pp3'}) - self.assertEqual(template.defined_channels, {'cc1', 'cc2', 'cc3'}) - - def test_is_interruptable(self): - subtemp_args = [*zip(self.subtemplates, self.no_param_maps, self.chan_maps)] - - self.assertFalse( - MultiChannelPulseTemplate(subtemp_args, external_parameters=set()).is_interruptable) - - self.assertTrue( - MultiChannelPulseTemplate(subtemp_args[1:], external_parameters=set()).is_interruptable) - - -class MultiChannelPulseTemplateSequencingTests(unittest.TestCase): - - def test_requires_stop_false_mapped_parameters(self) -> None: - dummy = DummyPulseTemplate(parameter_names={'foo'}) - pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), {'default': 'A'}), - (dummy, dict(foo='rab-5'), {'default': 'B'})], - {'bar', 'rab'}) - self.assertEqual({'bar', 'rab'}, pulse.parameter_names) - - parameters = dict(bar=ConstantParameter(-3.6), rab=ConstantParameter(35.26)) - self.assertFalse(pulse.requires_stop(parameters, dict())) - - def test_requires_stop_true_mapped_parameters(self) -> None: - dummy = DummyPulseTemplate(parameter_names={'foo'}, requires_stop=True) - pulse = MultiChannelPulseTemplate([(dummy, dict(foo='2*bar'), {'default': 'A'}), - (dummy, dict(foo='rab-5'), {'default': 'B'})], - {'bar', 'rab'}) - self.assertEqual({'bar', 'rab'}, pulse.parameter_names) - parameters = dict(bar=ConstantParameter(-3.6), rab=ConstantParameter(35.26)) - self.assertTrue(pulse.requires_stop(parameters, dict())) - - def test_build_sequence_different_duration(self) -> None: - dummy_wf1 = DummyWaveform(duration=2.4, defined_channels={'A'}) - dummy_wf2 = DummyWaveform(duration=2.3, defined_channels={'B'}) - dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={'A'}, waveform=dummy_wf1, duration=2.4) - dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2, duration=2.3) - - sequencer = DummySequencer() - pulse = MultiChannelPulseTemplate([dummy1, dummy2], {'bar'}) - - parameters = {'bar': ConstantParameter(3)} - measurement_mapping = {} - channel_mapping = {'A': 'A', 'B': 'B'} - instruction_block = DummyInstructionBlock() - conditions = {} - - pulse.build_sequence(sequencer, parameters=parameters, - conditions=conditions, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - - self.assertEqual(len(instruction_block), 2) - self.assertIsInstance(instruction_block[0], CHANInstruction) - - for chan, sub_block_ptr in instruction_block[0].channel_to_instruction_block.items(): - self.assertIn(chan,('A', 'B')) - if chan == 'A': - self.assertEqual( sequencer.sequencing_stacks[sub_block_ptr.block], - [(dummy1, parameters, conditions, measurement_mapping, channel_mapping)]) - if chan == 'B': - self.assertEqual(sequencer.sequencing_stacks[sub_block_ptr.block], - [(dummy2, parameters, conditions, measurement_mapping, channel_mapping)]) - - def test_build_sequence_same_duration(self) -> None: - dummy_wf1 = DummyWaveform(duration=2.3, defined_channels={0}) - dummy_wf2 = DummyWaveform(duration=2.3, defined_channels={'B'}) - dummy1 = DummyPulseTemplate(parameter_names={'bar'}, defined_channels={0}, waveform=dummy_wf1, duration=2.3) - dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B'}, waveform=dummy_wf2, duration=2.3) - - sequencer = DummySequencer() - pulse = MultiChannelPulseTemplate([dummy1, dummy2], {'bar'}) - - parameters = {'bar': ConstantParameter(3)} - measurement_mapping = {} - channel_mapping = {0: 1, 'B': 'B'} - instruction_block = DummyInstructionBlock() - conditions = {} - - pulse.build_sequence(sequencer, parameters=parameters, - conditions=conditions, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping, - instruction_block=instruction_block) - - self.assertEqual(len(instruction_block), 2) - self.assertIsInstance(instruction_block[0], EXECInstruction) - self.assertIsInstance(instruction_block[0].waveform, MultiChannelWaveform) - - self.assertEqual(instruction_block[0].waveform.compare_key, (dummy_wf1.compare_key, dummy_wf2.compare_key)) - - -class MultiChannelPulseTemplateSerializationTests(unittest.TestCase): - - def __init__(self, methodName) -> None: - super().__init__(methodName=methodName) - self.maxDiff = None - self.dummy1 = DummyPulseTemplate(parameter_names={'foo'}, defined_channels={'A'}, measurement_names={'meas_1'}) - self.dummy2 = DummyPulseTemplate(parameter_names={}, defined_channels={'B', 'C'}) - - def test_get_serialization_data(self) -> None: - serializer = DummySerializer( - serialize_callback=lambda x: str(x) if isinstance(x, Expression) else str(id(x))) - template = MultiChannelPulseTemplate( - [ self.dummy1, self.dummy2 ], - {'foo'}, - identifier='herbert' - ) - template.atomicity = True - expected_data = dict( - subtemplates=[str(id(self.dummy1)), str(id(self.dummy2))], - atomicity=True, - type=serializer.get_type_identifier(template)) - data = template.get_serialization_data(serializer) - self.assertEqual(expected_data, data) - - def test_deserialize(self) -> None: - serializer = DummySerializer(serialize_callback=lambda x: str(x) if isinstance(x, Expression) else str(id(x))) - serializer.subelements[str(id(self.dummy1))] = self.dummy1 - serializer.subelements[str(id(self.dummy2))] = self.dummy2 - - data = dict( - subtemplates=[str(id(self.dummy1)), str(id(self.dummy2))], - atomicity=False) - - template = MultiChannelPulseTemplate.deserialize(serializer, **data) - for st_expected, st_found in zip( [self.dummy1, self.dummy2], template.subtemplates ): - self.assertEqual(st_expected,st_found) + def test_serialize(self): + self.assertTrue(False) \ No newline at end of file From 30b2b802f0a6af7c78e0e6f9032d1a5468eb7516 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 16:53:36 +0200 Subject: [PATCH 084/116] Move "flatten" funtionality from tabor specific part to program and add testing for it --- qctoolkit/hardware/awgs/tabor.py | 102 ++++++++++++------------------- qctoolkit/hardware/program.py | 32 +++++++++- tests/hardware/program_tests.py | 77 +++++++++++++++++++++++ tests/hardware/tabor_tests.py | 43 +++++++++---- 4 files changed, 177 insertions(+), 77 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index d8696b328..a1c2454e0 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,7 +1,8 @@ """""" import fractions import sys -from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any +from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any, Sequence, cast +from enum import Enum # Provided by Tabor electronics for python 2.7 # a python 3 version is in a private repository on https://git.rwth-aachen.de/qutech @@ -30,14 +31,13 @@ def __new__(cls, ch_a: Optional[np.ndarray], ch_b: Optional[np.ndarray]): def __init__(self, ch_a, ch_b): if ch_a is None and ch_b is None: raise TaborException('Empty TaborSegments are not allowed') + if ch_a is not None and ch_b is not None and len(ch_a) != len(ch_b): + raise TaborException('Channel entries to have to have the same length') def __hash__(self) -> int: return hash((bytes(self[0]) if self[0] is not None else 0, bytes(self[1]) if self[1] is not None else 0)) - def __eq__(self, other) -> bool: - return - @property def num_points(self) -> int: return len(self[0]) if self[1] is None else len(self[1]) @@ -47,9 +47,12 @@ def get_as_binary(self) -> np.ndarray: return make_combined_wave([self]) -class TaborProgram: - WAVEFORM_MODES = ('single', 'advanced', 'sequence') +class TaborSequencing(Enum): + SINGLE = 1 + ADVANCED = 2 + +class TaborProgram: def __init__(self, program: Loop, device_properties, @@ -64,7 +67,7 @@ def __init__(self, markers if marker is not None) self._program = program - self.__waveform_mode = 'advanced' + self.__waveform_mode = None self._channels = tuple(channels) self._markers = tuple(markers) self.__used_channels = channel_set @@ -74,12 +77,17 @@ def __init__(self, self._sequencer_tables = [] self._advanced_sequencer_table = [] - if self.program.depth() == 0: - self.setup_single_waveform_mode() - elif self.program.depth() == 1: - self.setup_single_sequence_mode() - else: + if self.program.repetition_count > 1: + self.program.encapsulate() + + if self.program.depth() > 1: self.setup_advanced_sequence_mode() + self.__waveform_mode = TaborSequencing.ADVANCED + else: + if self.program.depth() == 0: + self.program.encapsulate() + self.setup_single_sequence_mode() + self.__waveform_mode = TaborSequencing.SINGLE @property def markers(self) -> Tuple[Optional[ChannelID], Optional[ChannelID]]: @@ -93,7 +101,8 @@ def sampled_segments(self, sample_rate: float, voltage_amplitude: Tuple[float, float], voltage_offset: Tuple[float, float], - voltage_transformation: Tuple[Callable, Callable]) -> List[TaborSegment]: + voltage_transformation: Tuple[Callable, Callable]) -> Tuple[Sequence[TaborSegment], + Sequence[int]]: sample_rate = fractions.Fraction(sample_rate, 10**9) segment_lengths = [waveform.duration*sample_rate for waveform in self._waveforms] @@ -127,7 +136,7 @@ def get_marker_data(waveform: MultiChannelWaveform, time): astype(dtype=np.uint16) << marker_index+14 return marker_data - segments = np.empty_like(self._waveforms, dtype=object) + segments = np.empty_like(self._waveforms, dtype=TaborSegment) for i, waveform in enumerate(self._waveforms): t = time_array[:int(waveform.duration*sample_rate)] segment_a = voltage_to_data(waveform, t, 0) @@ -139,16 +148,8 @@ def get_marker_data(waveform: MultiChannelWaveform, time): segments[i] = TaborSegment(segment_a, segment_b) return segments, segment_lengths - def setup_single_waveform_mode(self) -> None: - self.__waveform_mode = 'single' - self._waveforms = [self.program.waveform.get_subset_for_channels(self.__used_channels)] - self._sequencer_tables = [[(self.program.repetition_count, 1, 0)]] - self._advanced_sequencer_table = [] - def setup_single_sequence_mode(self) -> None: - self.__waveform_mode = 'sequence' - if len(self.program) < self.__device_properties['min_seq_len']: - raise TaborException('SEQuence:LENGth has to be >={min_seq_len}'.format(**self.__device_properties)) + assert self.program.depth() == 1 sequencer_table = [] waveforms = [] @@ -163,36 +164,15 @@ def setup_single_sequence_mode(self) -> None: waveforms.append(waveform) sequencer_table.append((repetition_count, segment_no, 0)) - self.__waveform_mode = 'sequence' self._waveforms = waveforms self._sequencer_tables = [sequencer_table] self._advanced_sequencer_table = [(self.program.repetition_count, 1, 0)] def setup_advanced_sequence_mode(self) -> None: - if self.program.depth() == 1: - self.program.encapsulate() - while self.program.depth() > 2 or not self.program.is_balanced(): - for i, sequence_table in enumerate(self.program): - if sequence_table.depth() == 0: - sequence_table.encapsulate() - elif sequence_table.depth() == 1: - assert (sequence_table.is_balanced()) - elif len(sequence_table) == 1 and len(sequence_table[0]) == 1: - sequence_table.join_loops() - elif sequence_table.is_balanced(): - if len(self.program) < self.__device_properties['min_aseq_len'] or (sequence_table.repetition_count // self.__device_properties['max_aseq_len'] < - max(entry.repetition_count for entry in sequence_table) / - self.__device_properties['max_seq_len']): - sequence_table.unroll() - else: - for entry in sequence_table.children: - entry.unroll() + assert self.program.depth() > 1 + assert self.program.repetition_count == 1 - else: - depth_to_unroll = sequence_table.depth() - 1 - for entry in sequence_table: - if entry.depth() == depth_to_unroll: - entry.unroll() + self.program.flatten_and_balance(2) min_seq_len = self.__device_properties['min_seq_len'] max_seq_len = self.__device_properties['max_seq_len'] @@ -234,10 +214,16 @@ def check_partial_unroll(program, n): elif check_partial_unroll(self.program, i): i += 1 - elif i > 0 and len(self.program[i]) + len(self.program[i-1]) < max_seq_len: + # check if sequence table can be extended by unrolling a neighbor + elif (i > 0 + and self.program[i - 1].repetition_count > 1 + and len(self.program[i]) + len(self.program[i-1]) < max_seq_len): self.program[i][:0] = self.program[i-1].copy_tree_structure()[:] self.program[i - 1].repetition_count -= 1 - elif i+1 < len(self.program) and len(self.program[i]) + len(self.program[i+1]) < max_seq_len: + + elif (i+1 < len(self.program) + and self.program[i+1].repetition_count > 1 + and len(self.program[i]) + len(self.program[i+1]) < max_seq_len): self.program[i][len(self.program[i]):] = self.program[i+1].copy_tree_structure()[:] self.program[i+1].repetition_count -= 1 @@ -250,11 +236,6 @@ def check_partial_unroll(program, n): else: i += 1 - assert (self.program.repetition_count == 1) - if len(self.program) < self.__device_properties['min_aseq_len']: - raise TaborException() - if len(self.program) > self.__device_properties['max_aseq_len']: - raise TaborException() for sequence_table in self.program: if len(sequence_table) < self.__device_properties['min_seq_len']: raise TaborException('Sequence table is too short') @@ -589,9 +570,6 @@ def upload(self, name: str, self._segment_references[waveform_to_segment[waveform_to_segment >= 0]] += 1 if to_insert: - # as we have to insert waveforms the waveforms behind the last referenced are discarded - self.cleanup() - for wf_index, segment_index in to_insert: self._upload_segment(segment_index, segments[wf_index]) waveform_to_segment[wf_index] = segment_index @@ -679,6 +657,7 @@ def remove(self, name: str) -> None: name (str): The name of the program to remove. """ self.free_program(name) + self.cleanup() def set_marker_state(self, marker, active) -> None: command_string = ':INST:SEL {}; :SOUR:MARK:SEL {}; :SOUR:MARK:SOUR USER; :SOUR:MARK:STAT {}'.format( @@ -713,17 +692,14 @@ def change_armed_program(self, name: str) -> None: advanced_sequencer_table = [(rep_count, seq_no + 1, jump_flag) for rep_count, seq_no, jump_flag in program.get_advanced_sequencer_table()] - if program.waveform_mode == 'single': - assert len(advanced_sequencer_table) == 0 + if program.waveform_mode == TaborSequencing.SINGLE: + assert len(advanced_sequencer_table) == 1 assert len(sequencer_tables) == 2 - assert len(sequencer_tables[1]) == 1 while len(sequencer_tables[1]) < self._device.dev_properties['min_seq_len']: sequencer_tables[1].append((1, 1, 0)) - advanced_sequencer_table = [(1, 2, 0)] - - # insert idle waveform + # insert idle sequence in advanced sequence table advanced_sequencer_table = [(1, 1, 1)] + advanced_sequencer_table while len(advanced_sequencer_table) < self._device.dev_properties['min_aseq_len']: diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index bfd083dfd..4d6919268 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -1,5 +1,5 @@ import itertools -from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable, Tuple +from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable, Tuple, cast from collections import deque, defaultdict from copy import deepcopy from ctypes import c_double as MutableFloat @@ -171,6 +171,36 @@ def split_one_child(self, child_index=None) -> None: self[child_index+1:child_index+1] = (new_child,) self.assert_tree_integrity() + def flatten_and_balance(self, depth: int) -> None: + """ + Modifies the program so all tree branches have the same depth + :param depth: Target depth of the program + :return: + """ + i = 0 + while i < len(self): + # only used by type checker + sub_program = cast(Loop, self[i]) + + if sub_program.depth() < depth - 1: + sub_program.encapsulate() + + elif not sub_program.is_balanced(): + sub_program.flatten_and_balance(depth - 1) + + elif sub_program.depth() == depth - 1: + i += 1 + + elif len(sub_program) == 1 and len(sub_program[0]) == 1: + sub_sub_program = cast(Loop, sub_program[0]) + + sub_program.repetition_count = sub_program.repetition_count * sub_sub_program.repetition_count + sub_program[:] = sub_sub_program[:] + sub_program.waveform = sub_sub_program.waveform + + else: + sub_program.unroll() + class ChannelSplit(Exception): def __init__(self, channel_sets): diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py index b004274a7..5aaba2411 100644 --- a/tests/hardware/program_tests.py +++ b/tests/hardware/program_tests.py @@ -212,6 +212,83 @@ def test_is_balanced(self): self.assertTrue(root_loop[3].is_balanced()) self.assertTrue(root_loop[4].is_balanced()) + def test_flatten_and_balance(self): + before = LoopTests.get_test_loop(lambda: DummyWaveform()) + before[1][0].encapsulate() + + after = before.copy_tree_structure() + after.flatten_and_balance(2) + + wf_reprs = dict(zip(ascii_uppercase, + (repr(loop.waveform) + for loop in before.get_depth_first_iterator() + if loop.is_leaf()))) + + before_repr = """\ +LOOP 1 times: + ->EXEC {A} 1 times + ->LOOP 10 times: + ->LOOP 1 times: + ->EXEC {B} 50 times + ->LOOP 17 times: + ->LOOP 2 times: + ->EXEC {C} 1 times + ->EXEC {D} 1 times + ->EXEC {E} 1 times + ->LOOP 3 times: + ->EXEC {F} 1 times + ->EXEC {G} 1 times + ->LOOP 4 times: + ->LOOP 6 times: + ->EXEC {H} 7 times + ->EXEC {I} 8 times + ->LOOP 9 times: + ->EXEC {J} 10 times + ->EXEC {K} 11 times""".format(**wf_reprs) + self.assertEqual(repr(before), before_repr) + + expected_after_repr = """\ +LOOP 1 times: + ->LOOP 1 times: + ->EXEC {A} 1 times + ->LOOP 10 times: + ->EXEC {B} 50 times + ->LOOP 17 times: + ->EXEC {C} 1 times + ->EXEC {D} 1 times + ->EXEC {C} 1 times + ->EXEC {D} 1 times + ->EXEC {E} 1 times + ->LOOP 3 times: + ->EXEC {F} 1 times + ->EXEC {G} 1 times + ->LOOP 6 times: + ->EXEC {H} 7 times + ->EXEC {I} 8 times + ->LOOP 9 times: + ->EXEC {J} 10 times + ->EXEC {K} 11 times + ->LOOP 6 times: + ->EXEC {H} 7 times + ->EXEC {I} 8 times + ->LOOP 9 times: + ->EXEC {J} 10 times + ->EXEC {K} 11 times + ->LOOP 6 times: + ->EXEC {H} 7 times + ->EXEC {I} 8 times + ->LOOP 9 times: + ->EXEC {J} 10 times + ->EXEC {K} 11 times + ->LOOP 6 times: + ->EXEC {H} 7 times + ->EXEC {I} 8 times + ->LOOP 9 times: + ->EXEC {J} 10 times + ->EXEC {K} 11 times""".format(**wf_reprs) + + self.assertEqual(expected_after_repr, repr(after)) + class MultiChannelTests(unittest.TestCase): def __init__(self, *args, **kwargs): diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 83ce4af97..ed5d8d38e 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -12,7 +12,8 @@ dummy_modules.import_package('atsaverage', dummy_modules.dummy_atsaverage) dummy_modules.import_package('teawg', dummy_modules.dummy_teawg) -from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair +from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair,\ + TaborSegment from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.hardware.util import voltage_to_uint16 @@ -22,13 +23,6 @@ from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests - -class DummyTaborAWGRepresentation(dummy_modules.dummy_teawg.TEWXAwg): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - select_channel = dummy_modules.dummy_teawg.TEWXAwg.send_cmd - - if with_hardware: # fix on your machine possible_addresses = ('127.0.0.1', ) @@ -37,7 +31,10 @@ def __init__(self, *args, **kwargs): reset=True, paranoia_level=2) instrument._visa_inst.timeout = 25000 - break + if instrument.is_open: + break + if not instrument.is_open: + raise RuntimeError('Could not connect to instrument') else: instrument = TaborAWGRepresentation('dummy_address', reset=True, paranoia_level=2) instrument._visa_inst.answers[':OUTP:COUP'] = 'DC' @@ -45,6 +42,24 @@ def __init__(self, *args, **kwargs): instrument._visa_inst.answers[':FREQ:RAST'] = '1e9' +class TaborSegmentTests(unittest.TestCase): + def test_init(self): + with self.assertRaises(TaborException): + TaborSegment(None, None) + with self.assertRaises(TaborException): + TaborSegment(np.zeros(5), np.zeros(4)) + + ch_a = np.zeros(5) + ch_b = np.ones(5) + + ts = TaborSegment(ch_a=ch_a, ch_b=ch_b) + self.assertIs(ts[0], ch_a) + self.assertIs(ts[1], ch_b) + + def test_num_points(self): + self.assertEqual(TaborSegment(np.zeros(5), np.zeros(5)).num_points, 5) + + class TaborProgramTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -67,12 +82,15 @@ def root_loop(self): def test_init(self): prog = MultiChannelProgram(MultiChannelTests().root_block) TaborProgram(prog['A'], self.instr_props, ('A', None), (None, None)) + with self.assertRaises(KeyError): TaborProgram(prog['A'], self.instr_props, ('A', 'B'), (None, None)) - @unittest.skip - def test_setup_single_waveform_mode(self): - pass + with self.assertRaises(TaborException): + TaborProgram(prog['A'], self.instr_props, ('A', 'B'), (None, None, None)) + with self.assertRaises(TaborException): + TaborProgram(prog['A'], self.instr_props, ('A', 'B', 'C'), (None, None)) + def test_sampled_segments(self): @@ -134,7 +152,6 @@ def my_gen(gen): self.assertTrue(np.all(sampled_seg[1] << 2 == data[1] << 2)) -@unittest.skipIf(isinstance(instrument, DummyTaborAWGRepresentation), "No instrument present") class TaborAWGRepresentationTests(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) From 834250c76c4d5865b326d7a5c901405c712f2725 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 16:54:08 +0200 Subject: [PATCH 085/116] FIX: Removing channels from HardwareSetup --- qctoolkit/hardware/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index f5bb2ea8d..24ff082d2 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -160,7 +160,7 @@ def set_channel(self, identifier: ChannelID, single_channel: Union[PlaybackChann raise ValueError('Channel must be either a playback or a marker channel') def rm_channel(self, identifier: ChannelID) -> None: - self._playback_channel_map.pop(identifier) + self._channel_map.pop(identifier) def registered_channels(self) -> Set[PlaybackChannel]: return self._channel_map.copy() From 8773c6fc6889dcb106a424ae858cbec1a7539e00 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 16:55:02 +0200 Subject: [PATCH 086/116] FIX RepetitionPulseTemplate sequencing --- qctoolkit/pulses/repetition_pulse_template.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index 9d7b22ee8..411f42cbc 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -151,7 +151,8 @@ def build_sequence(self, raise ParameterNotProvidedException(next(v for v in self.repetition_count.variables if v not in parameters)) instruction_block.add_instruction_repj(self.get_repetition_count_value(real_parameters), body_block) - sequencer.push(self.body, parameters, conditions, measurement_mapping, channel_mapping, body_block) + sequencer.push(self.body, parameters=parameters, conditions=conditions, + window_mapping=measurement_mapping, channel_mapping=channel_mapping, target_block=body_block) def requires_stop(self, parameters: Dict[str, Parameter], From 83d1475e878bc437ca0455b720585ee47d0cfe23 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 16:56:15 +0200 Subject: [PATCH 087/116] Extend examples --- doc/source/examples/HardwareSetup.ipynb | 140 +++++----- .../TestMeasurement_nested_loops.ipynb | 228 ++++++++++++++++ .../serialized_pulses/nested_loops.json | 252 ++++++++++++++++++ 3 files changed, 548 insertions(+), 72 deletions(-) create mode 100644 doc/source/examples/TestMeasurement_nested_loops.ipynb create mode 100644 doc/source/examples/serialized_pulses/nested_loops.json diff --git a/doc/source/examples/HardwareSetup.ipynb b/doc/source/examples/HardwareSetup.ipynb index 0bee30061..1c0f2f14f 100644 --- a/doc/source/examples/HardwareSetup.ipynb +++ b/doc/source/examples/HardwareSetup.ipynb @@ -6,13 +6,21 @@ "collapsed": true }, "source": [ - "" + "This notebook creates the following objects:\n", + " - ```hardware_setup```: An object of type HardwareSetup and the central object\n", + " - ```tawg```: An object that represents that physical TaborAWG\n", + " - ```tchannelpair_AB``` and ```tchannelpair_CD```: Objects that represents a pair of channels each\n", + " - ```alazar_DAC```: An object that represents the Alazar data acquisition card\n", + "\n", + "Furthermore, the alazar will create a 10MHz reference clock that the AWG expects to receive at its ref clock input" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 1, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "from qctoolkit.hardware.setup import HardwareSetup, PlaybackChannel, MarkerChannel\n", @@ -21,102 +29,80 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 15, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Failed to open \"USB0::0x168C::0x2184::0000216488::INSTR\"\n(, VisaIOError('VI_ERROR_RSRC_NFOUND (-1073807343): Insufficient location information or the requested device or resource is not present in the system.',), )\n" - ] - }, - { - "ename": "AttributeError", - "evalue": "'NoneType' object has no attribute 'write'", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m# small wrapper around pytabor\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mtawg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTaborAWGRepresentation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'USB0::0x168C::0x2184::0000216488::INSTR'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreset\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[0mtawg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparanoia_level\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;32mc:\\users\\lablocal\\documents\\code\\qc-toolkit\\qctoolkit\\hardware\\awgs\\tabor.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, external_trigger, reset, *args, **kwargs)\u001b[0m\n\u001b[0;32m 311\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 312\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mreset\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 313\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvisa_inst\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m':RES'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 314\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 315\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mexternal_trigger\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'write'" - ], - "output_type": "error" - } - ], + "outputs": [], "source": [ "from qctoolkit.hardware.awgs.tabor import TaborChannelPair, TaborAWGRepresentation\n", + "import pyvisa\n", "\n", - "tabor_address = r'USB0::0x168C::0x2184::0000216488::INSTR'\n", + "def find_tabor_address():\n", + " known_instruments = pyvisa.ResourceManager().list_resources()\n", + " \n", + " # find tabor AWG address\n", + " tabor_address = None\n", + " for address in known_instruments:\n", + " if r'0x168C::0x2184' in address:\n", + " tabor_address = address\n", + " break\n", + " if tabor_address is None:\n", + " raise RuntimeError('Could not locate TaborAWG')\n", + " \n", + " return tabor_address\n", "\n", "# small wrapper around pytabor\n", - "tawg = TaborAWGRepresentation(r'USB0::0x168C::0x2184::0000216488::INSTR', reset=True)\n", + "tawg = TaborAWGRepresentation(find_tabor_address(), reset=True)\n", "tawg.paranoia_level = 2\n", "\n", "# actual awg driver object\n", - "tchannelpair = TaborChannelPair(tawg, (1, 2), 'TABOR_AB')" + "tchannelpair_AB = TaborChannelPair(tawg, (1, 2), 'TABOR_AB')\n", + "tchannelpair_CD = TaborChannelPair(tawg, (3, 4), 'TABOR_CD')" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, + "execution_count": 3, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair, 0))\n", - "hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair, 1))\n", - "hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair, 0))\n", - "hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair, 1))" + "hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair_AB, 0))\n", + "hardware_setup.set_channel('TABOR_B', PlaybackChannel(tchannelpair_AB, 1))\n", + "hardware_setup.set_channel('TABOR_A_MARKER', MarkerChannel(tchannelpair_AB, 0))\n", + "hardware_setup.set_channel('TABOR_B_MARKER', MarkerChannel(tchannelpair_AB, 1))\n", + "\n", + "hardware_setup.set_channel('TABOR_C', PlaybackChannel(tchannelpair_CD, 0))\n", + "hardware_setup.set_channel('TABOR_D', PlaybackChannel(tchannelpair_CD, 1))\n", + "hardware_setup.set_channel('TABOR_C_MARKER', MarkerChannel(tchannelpair_CD, 0))\n", + "hardware_setup.set_channel('TABOR_D_MARKER', MarkerChannel(tchannelpair_CD, 1))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [ - { - "ename": "ModuleNotFoundError", - "evalue": "No module named 'atsaverage'", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0matsaverage\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLogger\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog_path\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34mr'C:\\Users\\Public\\AtsAverageLog'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0matsaverage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLogger\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrotate_file\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'atsaverage'" - ], - "output_type": "error" - } - ], + "outputs": [], "source": [ - "import atsaverage\n", "import atsaverage.atsaverage\n", + "import atsaverage.core\n", + "\n", "atsaverage.atsaverage.Logger.log_path = r'C:\\Users\\Public\\AtsAverageLog'\n", "atsaverage.atsaverage.Logger.rotate_file()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "import atsaverage.atsaverage\n", "atsaverage.atsaverage.Logger.log_path = r'C:\\Users\\Public\\AtsAverageLog'\n", "atsaverage.atsaverage.Logger.rotate_file()\n", "from qctoolkit.hardware.dacs.alazar import AlazarCard\n", "\n", - "alazar = AlazarCard(atsaverage.core.getLocalCard(1, 1))\n", + "alazar_DAC = AlazarCard(atsaverage.core.getLocalCard(1, 1))\n", "\n", - "# mask MyMask will be applied/refers to data from channel 0\n", - "alazar.register_mask_for_channel('MyMask', 0)\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ "def get_alazar_config():\n", " from atsaverage import alazar\n", " from atsaverage.config import ScanlineConfiguration, CaptureClockConfiguration, EngineTriggerConfiguration,\\\n", @@ -148,38 +134,48 @@ " # is set automatically\n", " assert config.totalRecordSize == 0\n", " \n", - " return config" + " return config\n", + "alazar_DAC.config = get_alazar_config()\n", + "\n", + "hardware_setup.register_dac(alazar_DAC)" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ - "alazar.config = get_alazar_config()" + "# 10MHz clock out on AUX\n", + "atsaverage.alazar.ConfigureAuxIO(alazar_DAC.card.handle, atsaverage.alazar.AUX_IO_Mode.out_pacer, 10)\n", + "\n", + "# 10MHz ref clock in\n", + "tawg.send_cmd(':ROSC:FREQ 10M')\n", + "tawg.send_cmd(':ROSC:SOUR EXT')" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.6" + "pygments_lexer": "ipython3", + "version": "3.5.3" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/doc/source/examples/TestMeasurement_nested_loops.ipynb b/doc/source/examples/TestMeasurement_nested_loops.ipynb new file mode 100644 index 000000000..ce7710730 --- /dev/null +++ b/doc/source/examples/TestMeasurement_nested_loops.ipynb @@ -0,0 +1,228 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "Run the HardwareSetup script to get access to ```hardware_setup``` and ```alazar_DAC```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "%run HardwareSetup.ipynb" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Start the atsaverage status window" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import atsaverage.gui\n", + "status_window = atsaverage.gui.ThreadedStatusWindow(alazar_DAC.card)\n", + "status_window.start()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Load the example pulse" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from qctoolkit.serialization import Serializer, FilesystemBackend\n", + "\n", + "nested_loop_pulse = Serializer(FilesystemBackend('./serialized_pulses')).deserialize('nested_loops')\n", + "\n", + "parameters=dict(N_T=10, N_T_rep=5, N_rep=17, T=192, T_start=2*192, T_step=160)\n", + "\n", + "assert nested_loop_pulse.measurement_names == {'M'}\n", + "\n", + "alazar_DAC.register_mask_for_channel('M', 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "operations = [atsaverage.operations.Downsample()]\n", + "operations[0].maskID = 'M'\n", + "operations[0].identifier = 'DSM'\n", + "\n", + "alazar_DAC.register_operations('nested_loops', operations)\n", + "\n", + "# store raw data for later inspection\n", + "alazar_DAC.config.rawDataMask = atsaverage.atsaverage.ChannelMask(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "from qctoolkit.pulses.sequencing import Sequencer\n", + "\n", + "seq = Sequencer()\n", + "seq.push(nested_loop_pulse, \n", + " parameters=parameters, channel_mapping=dict(A='TABOR_A', marker='TABOR_A_MARKER'))\n", + "g = seq.build()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Multiply all voltages of ```TABOR_A``` with 0.5" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "hardware_setup.rm_channel('TABOR_A')\n", + "hardware_setup.set_channel('TABOR_A', PlaybackChannel(tchannelpair_AB, 0, voltage_transformation=lambda x: 0.5*x))\n", + "hardware_setup.register_program('nested_loops', g)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " Scanline configuration:\n", + " Total record size: 4352\n", + " Sample rate: 100000000\n", + " Total time: 4.352e-05 s\n", + "\n", + " Buffer size: 4352\n", + " Time per buffer: 4.352e-05 s\n", + " Number of buffers: 1\n", + "\n", + " Recording Channels: A\n", + " Store raw data: True\n", + " \n" + ] + } + ], + "source": [ + "hardware_setup.arm_program('nested_loops')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "tchannelpair_AB.run_current_program()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "results = alazar_DAC.card.extractNextScanline()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([33736, 34248, 34652, ..., 30788, 30860, 30904], dtype=uint16)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "raw = results.rawData" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD8CAYAAACcjGjIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvXm0NVd1H/jbNd373qcBkMRgwAhbYEdogWwGi05WuoOX\nbXXcMR67abvb/oPYy8vuJCvuTqdxpw0OxmsROya2Y3AwjjGxG9tNTCAeEuPgId1BEmKQAFnIAiQk\ngeDT9I3v3VtVZ/cfZ6hTp85w69ynJ/C7ey3B++6rW6/uuVV7n99v//bexMzY2c52trOd7cy24om+\ngJ3tbGc729mXnu2Cw852trOd7Wxiu+Cws53tbGc7m9guOOxsZzvb2c4mtgsOO9vZzna2s4ntgsPO\ndrazne1sYrvgsLOd7WxnO5vYLjjsbGc729nOJrYLDjvb2c52trOJVU/0BeTalVdeyVdfffUTfRk7\n29nOdvZlZR/60IceYuarUsd92Qa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1LDs+Y5oUaqAAAAAElFTkSuQmCC\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from matplotlib import pyplot as plt\n", + "plt.plot(raw)\n", + "plt.show()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.3" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/doc/source/examples/serialized_pulses/nested_loops.json b/doc/source/examples/serialized_pulses/nested_loops.json new file mode 100644 index 000000000..769b8c986 --- /dev/null +++ b/doc/source/examples/serialized_pulses/nested_loops.json @@ -0,0 +1,252 @@ +{ + "parameter_constraints": [], + "subtemplates": [ + { + "parameter_constraints": [], + "subtemplates": [ + { + "entries": { + "marker": [ + [ + "T", + 0, + "hold" + ] + ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" + }, + { + "channel": "A", + "duration_expression": { + "expression": "T", + "type": "qctoolkit.expressions.Expression" + }, + "expression": { + "expression": "sin(t/T*2*pi)", + "type": "qctoolkit.expressions.Expression" + }, + "measurement_declarations": [ + [ + "M", + 0, + "T" + ] + ], + "parameter_constraints": [], + "type": "qctoolkit.pulses.function_pulse_template.FunctionPulseTemplate" + } + ], + "type": "qctoolkit.pulses.multi_channel_pulse_template.AtomicMultiChannelPulseTemplate" + }, + { + "body": { + "channel_mapping": { + "A": "A", + "marker": "marker" + }, + "measurement_mapping": { + "M": "M" + }, + "parameter_mapping": { + "T": "T_start + T_step*i_T" + }, + "template": { + "parameter_constraints": [], + "subtemplates": [ + { + "entries": { + "marker": [ + [ + "T", + 1, + "hold" + ] + ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" + }, + { + "channel": "A", + "duration_expression": { + "expression": "T", + "type": "qctoolkit.expressions.Expression" + }, + "expression": { + "expression": "sin(t/T*2*pi)", + "type": "qctoolkit.expressions.Expression" + }, + "measurement_declarations": [ + [ + "M", + 0, + "T" + ] + ], + "parameter_constraints": [], + "type": "qctoolkit.pulses.function_pulse_template.FunctionPulseTemplate" + } + ], + "type": "qctoolkit.pulses.multi_channel_pulse_template.AtomicMultiChannelPulseTemplate" + }, + "type": "qctoolkit.pulses.pulse_template_parameter_mapping.MappingTemplate" + }, + "loop_index": "i_T", + "loop_range": [ + 0, + "N_T", + 1 + ], + "type": "qctoolkit.pulses.loop_pulse_template.ForLoopPulseTemplate" + }, + { + "body": { + "channel_mapping": { + "A": "A", + "marker": "marker" + }, + "measurement_mapping": { + "M": "M" + }, + "parameter_mapping": { + "N_T": "N_T_rep", + "T_start": "192", + "T_step": "16" + }, + "template": { + "body": { + "channel_mapping": { + "A": "A", + "marker": "marker" + }, + "measurement_mapping": { + "M": "M" + }, + "parameter_mapping": { + "T": "T_start + T_step*i_T" + }, + "template": { + "parameter_constraints": [], + "subtemplates": [ + { + "entries": { + "marker": [ + [ + "T", + 1, + "hold" + ] + ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" + }, + { + "channel": "A", + "duration_expression": { + "expression": "T", + "type": "qctoolkit.expressions.Expression" + }, + "expression": { + "expression": "sin(t/T*2*pi)", + "type": "qctoolkit.expressions.Expression" + }, + "measurement_declarations": [ + [ + "M", + 0, + "T" + ] + ], + "parameter_constraints": [], + "type": "qctoolkit.pulses.function_pulse_template.FunctionPulseTemplate" + } + ], + "type": "qctoolkit.pulses.multi_channel_pulse_template.AtomicMultiChannelPulseTemplate" + }, + "type": "qctoolkit.pulses.pulse_template_parameter_mapping.MappingTemplate" + }, + "loop_index": "i_T", + "loop_range": [ + 0, + "N_T", + 1 + ], + "type": "qctoolkit.pulses.loop_pulse_template.ForLoopPulseTemplate" + }, + "type": "qctoolkit.pulses.pulse_template_parameter_mapping.MappingTemplate" + }, + "parameter_constraints": [], + "repetition_count": "N_rep", + "type": "qctoolkit.pulses.repetition_pulse_template.RepetitionPulseTemplate" + }, + { + "body": { + "channel_mapping": { + "A": "A", + "marker": "marker" + }, + "measurement_mapping": { + "M": "M" + }, + "parameter_mapping": { + "T": "T_start + T_step*i_T" + }, + "template": { + "parameter_constraints": [], + "subtemplates": [ + { + "entries": { + "marker": [ + [ + "T", + 1, + "hold" + ] + ] + }, + "measurements": [], + "parameter_constraints": [], + "type": "qctoolkit.pulses.table_pulse_template.TablePulseTemplate" + }, + { + "channel": "A", + "duration_expression": { + "expression": "T", + "type": "qctoolkit.expressions.Expression" + }, + "expression": { + "expression": "sin(t/T*2*pi)", + "type": "qctoolkit.expressions.Expression" + }, + "measurement_declarations": [ + [ + "M", + 0, + "T" + ] + ], + "parameter_constraints": [], + "type": "qctoolkit.pulses.function_pulse_template.FunctionPulseTemplate" + } + ], + "type": "qctoolkit.pulses.multi_channel_pulse_template.AtomicMultiChannelPulseTemplate" + }, + "type": "qctoolkit.pulses.pulse_template_parameter_mapping.MappingTemplate" + }, + "loop_index": "i_T", + "loop_range": [ + 0, + "N_T", + 1 + ], + "type": "qctoolkit.pulses.loop_pulse_template.ForLoopPulseTemplate" + } + ], + "type": "qctoolkit.pulses.sequence_pulse_template.SequencePulseTemplate" +} \ No newline at end of file From 2a676e4f8937c740091155e06382093f006abbbe Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 16:56:54 +0200 Subject: [PATCH 088/116] Fix docstring error and warning --- qctoolkit/pulses/repetition_pulse_template.py | 1 - setup.cfg | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index 411f42cbc..b1cf38eb9 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -89,7 +89,6 @@ def __init__(self, body (PulseTemplate): The PulseTemplate which will be repeated. repetition_count (int or ParameterDeclaration): The number of repetitions either as a constant integer value or as a parameter declaration. - loop_index (str): If specified the loop index identifier (str): A unique identifier for use in serialization. (optional) """ LoopPulseTemplate.__init__(self, identifier=identifier, body=body) diff --git a/setup.cfg b/setup.cfg index 315f5fb3b..0fa8cf315 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,3 @@ -[pytest] +[tool:pytest] testpaths = tests/pulses tests/experiment tests/bugs tests/hardware python_files=*_tests.py *_bug.py From f59443b2d19761bda6d3410adf43de3834c277c0 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 18:05:33 +0200 Subject: [PATCH 089/116] Add AtomicMultiChannelPT serialization tests --- .../pulses/multi_channel_pulse_template.py | 2 +- .../multi_channel_pulse_template_tests.py | 48 ++++++++++++++++--- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index d545c6674..16978983f 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -248,7 +248,7 @@ def requires_stop(self, def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], - parameter_constraints=self.parameter_constraints) + parameter_constraints=[str(constraint) for constraint in self.parameter_constraints]) return data @staticmethod diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index 5213e95f9..81bf79116 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -3,11 +3,9 @@ import numpy from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ - MissingParameterDeclarationException, UnnecessaryMappingException -from qctoolkit.pulses.parameters import ParameterNotProvidedException, MappedParameter, ConstantParameter + MissingParameterDeclarationException from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform, MappingTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate -from qctoolkit.expressions import Expression -from qctoolkit.pulses.instructions import CHANInstruction, EXECInstruction +from qctoolkit.pulses.parameters import ParameterConstraint from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform from tests.serialization_dummies import DummySerializer @@ -250,8 +248,44 @@ def test_defined_channels(self): template = AtomicMultiChannelPulseTemplate(*subtemp_args) self.assertEqual(template.defined_channels, {'cc1', 'cc2', 'cc3'}) - def test_deserialization(self): - self.assertTrue(False) + def test_deserialize(self): + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'})] + + def deserialization_callback(ident: str): + self.assertIn(ident, ('0', '1')) + + if ident == '0': + return 0 + else: + return 1 + + serializer = DummySerializer(deserialize_callback=deserialization_callback) + serializer.subelements = sts + + data = dict(subtemplates=['0', '1'], parameter_constraints=['a < d']) + + template = AtomicMultiChannelPulseTemplate.deserialize(serializer, **data) + + self.assertIs(template.subtemplates[0], sts[0]) + self.assertIs(template.subtemplates[1], sts[1]) + self.assertEqual(template.parameter_constraints, [ParameterConstraint('a < d')]) def test_serialize(self): - self.assertTrue(False) \ No newline at end of file + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'})] + constraints = ['a < d'] + template = AtomicMultiChannelPulseTemplate(*sts, + parameter_constraints=constraints) + + expected_data = dict(subtemplates=['0', '1'], parameter_constraints=['a < d']) + + def serialize_callback(obj) -> str: + self.assertIn(obj, sts) + return str(sts.index(obj)) + + serializer = DummySerializer(serialize_callback=serialize_callback, identifier_callback=serialize_callback) + + data = template.get_serialization_data(serializer=serializer) + + self.assertEqual(expected_data, data) From 7c9f15719ed3dd040d762c9b17c2e32a7dceeebb Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 18:11:41 +0200 Subject: [PATCH 090/116] Restructure __init__ files to be more user friendly --- qctoolkit/__init__.py | 3 +-- qctoolkit/hardware/__init__.py | 11 ++++++----- qctoolkit/hardware/awgs/__init__.py | 17 ++++++++++++----- qctoolkit/hardware/awgs/tabor.py | 4 ++-- qctoolkit/hardware/setup.py | 4 ++-- qctoolkit/pulses/__init__.py | 24 ++++++++++++------------ 6 files changed, 35 insertions(+), 28 deletions(-) diff --git a/qctoolkit/__init__.py b/qctoolkit/__init__.py index 8007d00a7..87b745791 100644 --- a/qctoolkit/__init__.py +++ b/qctoolkit/__init__.py @@ -1,7 +1,6 @@ import typing -__all__ = ["hardware", "pulses", "utils", "qcmatlab", "expressions", "serialization", - "MeasurementWindow", "ChannelID"] +__all__ = ["MeasurementWindow", "ChannelID"] MeasurementWindow = typing.Tuple[str, float, float] ChannelID = typing.Union[str, int] diff --git a/qctoolkit/hardware/__init__.py b/qctoolkit/hardware/__init__.py index 633d1eafc..c11191499 100644 --- a/qctoolkit/hardware/__init__.py +++ b/qctoolkit/hardware/__init__.py @@ -1,5 +1,6 @@ -__all__ = [ - 'awgs', - 'dacs', - 'program' -] +from qctoolkit.hardware.setup import HardwareSetup + +from qctoolkit.hardware import awgs +from qctoolkit.hardware import dacs + +__all__ = ["HardwareSetup", "awgs", "dacs"] diff --git a/qctoolkit/hardware/awgs/__init__.py b/qctoolkit/hardware/awgs/__init__.py index 7c66c21ba..90da30620 100644 --- a/qctoolkit/hardware/awgs/__init__.py +++ b/qctoolkit/hardware/awgs/__init__.py @@ -1,6 +1,13 @@ -from qctoolkit.hardware.awgs.base import AWG, DummyAWG +__all__ = [] -__all__ = [ - 'AWG', - 'DummyAWG' -] +try: + from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborChannelPair + __all__.extend(["TaborAWGRepresentation", "TaborChannelPair"]) +except ImportError: + pass + +try: + from qctoolkit.hardware.awgs.tektronix import TektronixAWG + __all__.extend("TektronixAWG") +except ImportError: + pass diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index a1c2454e0..e9030271a 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,7 +1,7 @@ """""" import fractions import sys -from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any, Sequence, cast +from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any, Sequence from enum import Enum # Provided by Tabor electronics for python 2.7 @@ -14,7 +14,7 @@ from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from qctoolkit.hardware.program import Loop from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave -from qctoolkit.hardware.awgs import AWG +from qctoolkit.hardware.awgs.base import AWG assert(sys.byteorder == 'little') diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index 24ff082d2..a62aef7b2 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -4,9 +4,9 @@ from ctypes import c_int64 as MutableInt -from qctoolkit.hardware.awgs import AWG +from qctoolkit.hardware.awgs.base import AWG from qctoolkit.hardware.dacs import DAC -from qctoolkit.hardware.program import MultiChannelProgram, Loop +from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit import ChannelID diff --git a/qctoolkit/pulses/__init__.py b/qctoolkit/pulses/__init__.py index 71fa06de0..e13185201 100644 --- a/qctoolkit/pulses/__init__.py +++ b/qctoolkit/pulses/__init__.py @@ -1,12 +1,12 @@ -from qctoolkit.pulses.conditions import * -from qctoolkit.pulses.function_pulse_template import * -from qctoolkit.pulses.instructions import * -from qctoolkit.pulses.loop_pulse_template import * -from qctoolkit.pulses.multi_channel_pulse_template import * -from qctoolkit.pulses.parameters import * -from qctoolkit.pulses.pulse_template import * -from qctoolkit.pulses.pulse_template_parameter_mapping import * -from qctoolkit.pulses.repetition_pulse_template import * -from qctoolkit.pulses.sequence_pulse_template import * -from qctoolkit.pulses.sequencing import * -from qctoolkit.pulses.table_pulse_template import * +from qctoolkit.pulses.function_pulse_template import FunctionPulseTemplate as FunctionPT +from qctoolkit.pulses.loop_pulse_template import ForLoopPulseTemplate as ForLoopPT +from qctoolkit.pulses.multi_channel_pulse_template import AtomicMultiChannelPulseTemplate as AtomicMultiChannelPT +from qctoolkit.pulses.pulse_template_parameter_mapping import MappingTemplate as MappingPT +from qctoolkit.pulses.repetition_pulse_template import RepetitionPulseTemplate as RepetitionPT +from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate as SequencePT +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate as TablePT + +from qctoolkit.pulses.sequencing import Sequencer + +__all__ = ["FunctionPT", "ForLoopPT", "AtomicMultiChannelPT", "MappingPT", "RepetitionPT", "SequencePT", "TablePT", + "Sequencer"] From 74acba25480e003b6fabf34b9148b9a48a105487 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 17 Aug 2017 18:12:59 +0200 Subject: [PATCH 091/116] Fix broken imports and make dummy module load central --- tests/hardware/__init__.py | 13 +++++++++++++ tests/hardware/alazar_tests.py | 3 +-- tests/hardware/awg_tests.py | 11 ++++++----- tests/hardware/tabor_tests.py | 13 +++---------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tests/hardware/__init__.py b/tests/hardware/__init__.py index e69de29bb..3eacfb940 100644 --- a/tests/hardware/__init__.py +++ b/tests/hardware/__init__.py @@ -0,0 +1,13 @@ + +from . import dummy_modules + + +use_dummy_tabor = True +if use_dummy_tabor: + dummy_modules.import_package('pytabor', dummy_modules.dummy_pytabor) + dummy_modules.import_package('pyvisa', dummy_modules.dummy_pyvisa) + dummy_modules.import_package('teawg', dummy_modules.dummy_teawg) + +use_dummy_atsaverage = True +if use_dummy_atsaverage: + dummy_modules.import_package('atsaverage') diff --git a/tests/hardware/alazar_tests.py b/tests/hardware/alazar_tests.py index d23aeca1a..946eddf28 100644 --- a/tests/hardware/alazar_tests.py +++ b/tests/hardware/alazar_tests.py @@ -1,7 +1,6 @@ import unittest -from . import dummy_modules -dummy_modules.import_package('atsaverage') +from ..hardware import * import atsaverage import atsaverage.config diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 45c94cd2b..1365935fa 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -3,15 +3,16 @@ import qctoolkit.hardware.awgs.base as awg import qctoolkit.hardware.awgs.tektronix as tek -import qctoolkit.pulses as pls +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate +from qctoolkit.pulses.sequencing import Sequencer class DummyAWGTest(unittest.TestCase): def setUp(self): - self.pulse_template = pls.TablePulseTemplate({'default': [('value', 5)]}) + self.pulse_template = TablePulseTemplate({'default': [('value', 5)]}) - self.sequencer = pls.Sequencer() + self.sequencer = Sequencer() for i in range(1, 12): pars = dict(value=i) self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) @@ -27,8 +28,8 @@ def test_ProgramOverwriteException(self): class TektronixAWGTest(unittest.TestCase): def setUp(self): - self.pulse_template = pls.TablePulseTemplate({'default': [('value', 5)]}) - self.sequencer = pls.Sequencer() + self.pulse_template = TablePulseTemplate({'default': [('value', 5)]}) + self.sequencer = Sequencer() for i in range(1,12): pars = dict(value=i) self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index ed5d8d38e..125eb0c81 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -4,26 +4,19 @@ from copy import copy, deepcopy import numpy as np -with_hardware = False -if not with_hardware: - from . import dummy_modules - dummy_modules.import_package('pytabor', dummy_modules.dummy_pytabor) - dummy_modules.import_package('pyvisa', dummy_modules.dummy_pyvisa) - dummy_modules.import_package('atsaverage', dummy_modules.dummy_atsaverage) - dummy_modules.import_package('teawg', dummy_modules.dummy_teawg) from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair,\ TaborSegment from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.hardware.util import voltage_to_uint16 +from ..hardware import use_dummy_tabor from teawg import model_properties_dict -import pytabor from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests -if with_hardware: +if not use_dummy_tabor: # fix on your machine possible_addresses = ('127.0.0.1', ) for instrument_address in possible_addresses: @@ -36,7 +29,7 @@ if not instrument.is_open: raise RuntimeError('Could not connect to instrument') else: - instrument = TaborAWGRepresentation('dummy_address', reset=True, paranoia_level=2) + instrument = TaborAWGRepresentation('dummy_address', reset=False, paranoia_level=2) instrument._visa_inst.answers[':OUTP:COUP'] = 'DC' instrument._visa_inst.answers[':VOLT'] = '1.0' instrument._visa_inst.answers[':FREQ:RAST'] = '1e9' From 4dbe763778587832fa4dd87055b42ed2797019f1 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Fri, 18 Aug 2017 15:10:39 +0200 Subject: [PATCH 092/116] Remove dead code Increase coverage import cleanup --- qctoolkit/comparable.py | 29 ---- qctoolkit/expressions.py | 7 +- qctoolkit/hardware/awgs/base.py | 6 +- qctoolkit/hardware/dacs/alazar.py | 5 +- qctoolkit/hardware/program.py | 5 +- qctoolkit/hardware/setup.py | 6 +- qctoolkit/hardware/util.py | 2 +- qctoolkit/pulses/loop_pulse_template.py | 30 +---- .../pulses/multi_channel_pulse_template.py | 6 +- qctoolkit/pulses/pulse_template.py | 4 +- .../pulse_template_parameter_mapping.py | 18 ++- qctoolkit/utils/__init__.py | 13 +- tests/expression_tests.py | 45 ++++++- tests/pulses/loop_pulse_template_tests.py | 125 +++++++++++++++++- .../pulse_template_parameter_mapping_tests.py | 39 ++++++ tests/utils/__init__.py | 1 - tests/utils/utils_tests.py | 34 +++++ 17 files changed, 284 insertions(+), 91 deletions(-) create mode 100644 tests/utils/utils_tests.py diff --git a/qctoolkit/comparable.py b/qctoolkit/comparable.py index 35af40a30..3d3803952 100644 --- a/qctoolkit/comparable.py +++ b/qctoolkit/comparable.py @@ -34,32 +34,3 @@ def __eq__(self, other: Any) -> bool: def __ne__(self, other: Any) -> bool: """True, if other is not equal to this Comparable object.""" return not self == other - - -def extend_comparison(cls): - if not hasattr(cls, '__lt__'): - raise ValueError('Class does not implement __lt__') - - def __eq__(self, other): - return not self < other and not other < self - - def __ne__(self, other): - return self < other or other < self - - def __gt__(self, other): - return other < self - - def __ge__(self, other): - return not self < other - - def __le__(self, other): - return not other < self - operations = {'__eq__': __eq__, - '__ne__': __ne__, - '__gt__': __gt__, - '__ge__': __ge__, - '__le__': __le__} - for operation_name, operation_func in operations.items(): - if not hasattr(cls, operation_name): - setattr(cls, operation_name, operation_func) - return cls diff --git a/qctoolkit/expressions.py b/qctoolkit/expressions.py index 16be58ab5..da33a867d 100644 --- a/qctoolkit/expressions.py +++ b/qctoolkit/expressions.py @@ -5,6 +5,7 @@ from typing import Any, Dict, Iterable, Optional, Union from numbers import Number import sympy +from sympy.core.numbers import Number as SympyNumber import numpy from qctoolkit.comparable import Comparable @@ -44,12 +45,12 @@ def get_most_simple_representation(self) -> Union[str, int, float, complex]: return str(self._sympified_expression) elif self._sympified_expression.is_integer: return int(self._sympified_expression) - elif self._sympified_expression.is_complex: - return complex(self._sympified_expression) elif self._sympified_expression.is_real: return float(self._sympified_expression) + elif self._sympified_expression.is_complex: + return complex(self._sympified_expression) else: - return self._original_expression + return self._original_expression # pragma: no cover @staticmethod def _sympify(other: Union['Expression', Number, sympy.Expr]) -> sympy.Expr: diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index 8303ea3dd..7b5ff6302 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -7,13 +7,13 @@ - OutOfWaveformMemoryException """ -from abc import ABCMeta, abstractmethod, abstractproperty +from abc import abstractmethod, abstractproperty from typing import Set, Tuple, List, Callable, Optional -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit import ChannelID from qctoolkit.hardware.program import Loop from qctoolkit.comparable import Comparable -from qctoolkit.pulses.instructions import InstructionSequence, EXECInstruction +from qctoolkit.pulses.instructions import InstructionSequence __all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", "OutOfWaveformMemoryException"] diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py index 37e841ccd..ab904b8e6 100644 --- a/qctoolkit/hardware/dacs/alazar.py +++ b/qctoolkit/hardware/dacs/alazar.py @@ -1,11 +1,10 @@ -from typing import Union, Dict, NamedTuple, List, Any, Optional, Tuple -from collections import deque, defaultdict +from typing import Dict, Any, Optional, Tuple +from collections import defaultdict import numpy as np from atsaverage.config import ScanlineConfiguration from atsaverage.masks import CrossBufferMask, Mask -from atsaverage.operations import OperationDefinition from qctoolkit.hardware.dacs import DAC diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 4d6919268..27ecace82 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -1,12 +1,11 @@ import itertools -from typing import Union, Dict, Set, Iterable, FrozenSet, List, NamedTuple, Any, Callable, Tuple, cast +from typing import Union, Dict, Set, Iterable, FrozenSet, Tuple, cast from collections import deque, defaultdict from copy import deepcopy -from ctypes import c_double as MutableFloat import numpy as np -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit import ChannelID from qctoolkit.pulses.instructions import AbstractInstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction, Waveform from qctoolkit.comparable import Comparable from qctoolkit.utils.tree import Node, is_tree_circular diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index a62aef7b2..b5a5775e6 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -1,11 +1,7 @@ -from typing import NamedTuple, Any, Set, Callable, Dict, Tuple, Union -import itertools +from typing import NamedTuple, Set, Callable, Dict, Tuple, Union from collections import defaultdict, deque -from ctypes import c_int64 as MutableInt - from qctoolkit.hardware.awgs.base import AWG -from qctoolkit.hardware.dacs import DAC from qctoolkit.hardware.program import MultiChannelProgram from qctoolkit import ChannelID diff --git a/qctoolkit/hardware/util.py b/qctoolkit/hardware/util.py index 323450467..d9d6f6684 100644 --- a/qctoolkit/hardware/util.py +++ b/qctoolkit/hardware/util.py @@ -1,4 +1,4 @@ -from typing import List, Tuple +from typing import List import numpy as np diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index cbf4b3338..f19a1f7f1 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -7,6 +7,7 @@ from qctoolkit.serialization import Serializer from qctoolkit.expressions import Expression +from qctoolkit.utils import checked_int_cast from qctoolkit.pulses.parameters import Parameter, ConstantParameter, InvalidParameterNameException from qctoolkit.pulses.pulse_template import PulseTemplate, ChannelID from qctoolkit.pulses.conditions import Condition, ConditionMissingException @@ -37,7 +38,7 @@ def measurement_names(self) -> Set[str]: @property def is_interruptable(self): - raise NotImplementedError() + raise NotImplementedError() # pragma: no cover class ParametrizedRange: @@ -70,17 +71,9 @@ def to_tuple(self) -> Tuple[Any, Any, Any]: self.step.get_most_simple_representation()) def to_range(self, parameters: Dict[str, Any]) -> range: - def to_int(x) -> int: - if isinstance(x, int): - return x - int_x = int(round(x)) - if abs(x-int_x) > 1e-10: - raise ValueError('Non integer in range') - return int_x - - return range(to_int(self.start.evaluate_numeric(**parameters)), - to_int(self.stop.evaluate_numeric(**parameters)), - to_int(self.step.evaluate_numeric(**parameters))) + return range(checked_int_cast(self.start.evaluate_numeric(**parameters)), + checked_int_cast(self.stop.evaluate_numeric(**parameters)), + checked_int_cast(self.step.evaluate_numeric(**parameters))) @property def parameter_names(self) -> Set[str]: @@ -140,10 +133,6 @@ def parameter_names(self) -> Set[str]: parameter_names.remove(self._loop_index) return parameter_names | self._loop_range.parameter_names - @property - def parameter_declarations(self) -> Set['ParameterDeclaration']: - raise NotImplementedError() - def _body_parameter_generator(self, parameters: Dict[str, Parameter], forward=True) -> Generator: loop_range_parameters = dict((parameter_name, parameters[parameter_name].get_value()) for parameter_name in self._loop_range.parameter_names) @@ -163,7 +152,7 @@ def build_sequence(self, conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], - instruction_block: InstructionBlock) -> None: + instruction_block: InstructionBlock) -> None: for local_parameters in self._body_parameter_generator(parameters, forward=False): sequencer.push(self.body, parameters=local_parameters, @@ -183,7 +172,6 @@ def requires_stop(self, def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict( - type=serializer.get_type_identifier(self), body=serializer.dictify(self.body), loop_range=self._loop_range.to_tuple(), loop_index=self._loop_index @@ -249,12 +237,6 @@ def __obtain_condition_object(self, conditions: Dict[str, Condition]) -> Conditi except: raise ConditionMissingException(self._condition) - def build_waveform(self, - parameters: Dict[str, Parameter], - measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> None: - raise NotImplementedError() - def build_sequence(self, sequencer: Sequencer, parameters: Dict[str, Parameter], diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 16978983f..80da2795e 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -7,7 +7,7 @@ - MultiChannelWaveform: A waveform defined for several channels by combining waveforms """ -from typing import Dict, List, Tuple, FrozenSet, Optional, Any, Iterable, Union, Set +from typing import Dict, List, Optional, Any, Iterable, Union, Set import itertools import numbers @@ -17,11 +17,11 @@ from qctoolkit.serialization import Serializer from qctoolkit import MeasurementWindow, ChannelID -from qctoolkit.pulses.instructions import InstructionBlock, Waveform, InstructionPointer +from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.pulse_template import PulseTemplate, AtomicPulseTemplate from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingTemplate,\ MissingParameterDeclarationException, MappingTuple -from qctoolkit.pulses.parameters import Parameter, ParameterConstraint, ParameterConstrainer +from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer from qctoolkit.pulses.conditions import Condition from qctoolkit.expressions import Expression diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index 8790c61f5..70a48d546 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -7,11 +7,11 @@ directly translated into a waveform. """ from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Dict, List, Tuple, Set, Optional, Union, Any, Iterable +from typing import Dict, Tuple, Set, Optional, Union import itertools from numbers import Real -from qctoolkit import ChannelID, MeasurementWindow +from qctoolkit import ChannelID from qctoolkit.serialization import Serializable from qctoolkit.expressions import Expression diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index 9ac0a4684..a97c70d8c 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -64,6 +64,8 @@ def __init__(self, template: PulseTemplate, *, if mapped_internal_names - internal_names: raise UnnecessaryMappingException(template, mapped_internal_names - internal_names) missing_name_mappings = internal_names - mapped_internal_names + measurement_mapping = dict(itertools.chain(((name, name) for name in missing_name_mappings), + measurement_mapping.items())) channel_mapping = dict() if channel_mapping is None else channel_mapping internal_channels = template.defined_channels @@ -71,8 +73,10 @@ def __init__(self, template: PulseTemplate, *, if mapped_internal_channels - internal_channels: raise UnnecessaryMappingException(template,mapped_internal_channels - internal_channels) missing_channel_mappings = internal_channels - mapped_internal_channels + channel_mapping = dict(itertools.chain(((name, name) for name in missing_channel_mappings), + channel_mapping.items())) - if isinstance(template, MappingTemplate): + if isinstance(template, MappingTemplate) and template.identifier is None: # avoid nested mappings parameter_mapping = {p: expr.evaluate_symbolic(parameter_mapping) for p, expr in template.parameter_mapping.items()} @@ -86,10 +90,8 @@ def __init__(self, template: PulseTemplate, *, self.__parameter_mapping = parameter_mapping self.__external_parameters = set(itertools.chain(*(expr.variables for expr in self.__parameter_mapping.values()))) self.__external_parameters |= self.constrained_parameters - self.__measurement_mapping = dict(itertools.chain(((name, name) for name in missing_name_mappings), - measurement_mapping.items())) - self.__channel_mapping = dict(itertools.chain(((name, name) for name in missing_channel_mappings), - channel_mapping.items())) + self.__measurement_mapping = measurement_mapping + self.__channel_mapping = channel_mapping @staticmethod def from_tuple(mapping_tuple: MappingTuple) -> 'MappingTemplate': @@ -161,16 +163,12 @@ def measurement_names(self) -> Set[str]: @property def is_interruptable(self) -> bool: - return self.template.is_interruptable + return self.template.is_interruptable # pragma: no cover @property def defined_channels(self) -> Set[ChannelID]: return {self.__channel_mapping[k] for k in self.template.defined_channels} - @property - def atomicity(self) -> bool: - return self.__template.atomicity - @property def duration(self) -> Expression: return self.__template.duration.evaluate_symbolic(self.__parameter_mapping) diff --git a/qctoolkit/utils/__init__.py b/qctoolkit/utils/__init__.py index 546e713cc..3b03f45d1 100644 --- a/qctoolkit/utils/__init__.py +++ b/qctoolkit/utils/__init__.py @@ -1 +1,12 @@ -__all__ = ["type_check"] \ No newline at end of file +from typing import Union + +__all__ = ["checked_int_cast"] + + +def checked_int_cast(x: Union[float, int], epsilon: float=1e-10) -> int: + if isinstance(x, int): + return x + int_x = int(round(x)) + if abs(x - int_x) > epsilon: + raise ValueError('No integer', x) + return int_x diff --git a/tests/expression_tests.py b/tests/expression_tests.py index 01ccddbba..da10d0832 100644 --- a/tests/expression_tests.py +++ b/tests/expression_tests.py @@ -3,7 +3,7 @@ import numpy as np from sympy import sympify -from qctoolkit.expressions import Expression, ExpressionVariableMissingException +from qctoolkit.expressions import Expression, ExpressionVariableMissingException, NonNumericEvaluation from qctoolkit.serialization import Serializer class ExpressionTests(unittest.TestCase): @@ -17,6 +17,10 @@ def test_evaluate_numeric(self) -> None: } self.assertEqual(2 * 1.5 - 7, e.evaluate_numeric(**params)) + with self.assertRaises(NonNumericEvaluation): + params['a'] = sympify('h') + e.evaluate_numeric(**params) + def test_evaluate_numpy(self): self e = Expression('a * b + c') @@ -137,3 +141,42 @@ def test_number_comparison(self): self.assertIs(3 <= valued, False) self.assertIs(3 >= valued, True) + def test_get_most_simple_representation(self): + cpl = Expression('1 + 1j').get_most_simple_representation() + self.assertIsInstance(cpl, complex) + self.assertEqual(cpl, 1 + 1j) + + integer = Expression('3').get_most_simple_representation() + self.assertIsInstance(integer, int) + self.assertEqual(integer, 3) + + flt = Expression('3.').get_most_simple_representation() + self.assertIsInstance(flt, float) + self.assertEqual(flt, 3.) + + def test_is_nan(self): + self.assertTrue(Expression('nan').is_nan()) + self.assertTrue(Expression('0./0.').is_nan()) + + self.assertFalse(Expression(456).is_nan()) + + +class ExpressionExceptionTests(unittest.TestCase): + def test_expression_variable_missing(self): + variable = 's' + expression = Expression('s*t') + + self.assertEqual(str(ExpressionVariableMissingException(variable, expression)), + "Could not evaluate : A value for variable is missing!") + + def test_non_numeric_evaluation(self): + expression = Expression('a*b') + call_arguments = dict() + + expected = "The result of evaluate_numeric is of type {} " \ + "which is not a number".format(float) + self.assertEqual(str(NonNumericEvaluation(expression, 1., call_arguments)), expected) + + expected = "The result of evaluate_numeric is of type {} " \ + "which is not a number".format(np.zeros(1).dtype) + self.assertEqual(str(NonNumericEvaluation(expression, np.zeros(1), call_arguments)), expected) diff --git a/tests/pulses/loop_pulse_template_tests.py b/tests/pulses/loop_pulse_template_tests.py index 8d6172618..3430dac88 100644 --- a/tests/pulses/loop_pulse_template_tests.py +++ b/tests/pulses/loop_pulse_template_tests.py @@ -2,14 +2,41 @@ from sympy import sympify +from qctoolkit.expressions import Expression from qctoolkit.pulses.loop_pulse_template import ForLoopPulseTemplate, WhileLoopPulseTemplate,\ - ConditionMissingException, ParametrizedRange, LoopIndexNotUsedException + ConditionMissingException, ParametrizedRange, LoopIndexNotUsedException, LoopPulseTemplate from qctoolkit.pulses.parameters import ConstantParameter -from tests.pulses.sequencing_dummies import DummyCondition, DummyPulseTemplate, DummySequencer, DummyInstructionBlock +from tests.pulses.sequencing_dummies import DummyCondition, DummyPulseTemplate, DummySequencer, DummyInstructionBlock,\ + DummyParameter from tests.serialization_dummies import DummySerializer +class DummyLoopPulseTemplate(LoopPulseTemplate): + pass +DummyLoopPulseTemplate.__abstractmethods__ = set() + + +class LoopPulseTemplateTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_body(self): + body = DummyPulseTemplate() + tpl = DummyLoopPulseTemplate(body) + self.assertIs(tpl.body, body) + + def test_defined_channels(self): + body = DummyPulseTemplate(defined_channels={'A'}) + tpl = DummyLoopPulseTemplate(body) + self.assertIs(tpl.defined_channels, body.defined_channels) + + def test_measurement_names(self): + body = DummyPulseTemplate(measurement_names={'A'}) + tpl = DummyLoopPulseTemplate(body) + self.assertIs(tpl.measurement_names, body.measurement_names) + + class ParametrizedRangeTest(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -30,6 +57,9 @@ def test_init(self): with self.assertRaises(TypeError): ParametrizedRange(1, 2, 3, 4) + with self.assertRaises(TypeError): + ParametrizedRange(1, 2, stop=6) + def test_to_range(self): pr = ParametrizedRange(4, 'l*k', 'k') @@ -84,6 +114,92 @@ def test_body_parameter_generator(self): expected_local_params = dict(k=ConstantParameter(5), i=ConstantParameter(i)) self.assertEqual(expected_local_params, local_params) + def test_loop_index(self): + loop_index = 'i' + dt = DummyPulseTemplate(parameter_names={'i', 'k'}) + flt = ForLoopPulseTemplate(body=dt, loop_index=loop_index, loop_range=('a', 'b', 'c')) + self.assertIs(loop_index, flt.loop_index) + + def test_duration(self): + dt = DummyPulseTemplate(parameter_names={'i', 'k'}, duration=Expression('d')) + flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=(3, 9, 2)) + + self.assertEqual(flt.duration, Expression('d*3')) + + def test_parameter_names(self): + dt = DummyPulseTemplate(parameter_names={'i', 'k'}) + flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c')) + + self.assertEqual(flt.parameter_names, {'k', 'a', 'b', 'c'}) + + def test_build_sequence(self): + dt = DummyPulseTemplate(parameter_names={'i'}) + flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('a', 'b', 'c')) + + sequencer = DummySequencer() + block = DummyInstructionBlock() + parameters = {'a': ConstantParameter(1), 'b': ConstantParameter(4), 'c': ConstantParameter(2)} + measurement_mapping = dict(A='B') + channel_mapping = dict(C='D') + flt.build_sequence(sequencer, parameters, dict(), measurement_mapping, channel_mapping, block) + + expected_stack = [(dt, {'i': ConstantParameter(3)}, dict(), measurement_mapping, channel_mapping), + (dt, {'i': ConstantParameter(1)}, dict(), measurement_mapping, channel_mapping)] + + self.assertEqual(sequencer.sequencing_stacks[block], expected_stack) + + def test_requires_stop(self): + parameters = dict(A=DummyParameter(requires_stop=False), B=DummyParameter(requires_stop=False)) + + dt = DummyPulseTemplate(parameter_names={'i'}) + flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('A', 'B')) + + self.assertFalse(flt.requires_stop(parameters, dict())) + + parameters['A'] = DummyParameter(requires_stop=True) + self.assertTrue(flt.requires_stop(parameters, dict())) + + def test_get_serialization_data(self): + + dt = DummyPulseTemplate(parameter_names={'i'}) + flt = ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=('A', 'B')) + + def check_dt(to_dictify) -> str: + self.assertIs(to_dictify, dt) + return 'dt' + + serializer = DummySerializer(serialize_callback=check_dt) + + data = flt.get_serialization_data(serializer) + expected_data = dict(body='dt', + loop_range=('A', 'B', 1), + loop_index='i') + self.assertEqual(data, expected_data) + + def test_deserialize(self): + body_str = 'dt' + dt = DummyPulseTemplate(parameter_names={'i'}) + + def make_dt(ident: str): + self.assertEqual(body_str, ident) + return ident + + data = dict(body=body_str, + loop_range=('A', 'B', 1), + loop_index='i', + identifier='meh') + + serializer = DummySerializer(deserialize_callback=make_dt) + serializer.subelements['dt'] = dt + + flt = ForLoopPulseTemplate.deserialize(serializer, **data) + self.assertEqual(flt.identifier, 'meh') + self.assertEqual(flt.body, dt) + self.assertEqual(flt.loop_index, 'i') + self.assertEqual(flt.loop_range.to_tuple(), ('A', 'B', 1)) + + + class WhileLoopPulseTemplateTest(unittest.TestCase): @@ -207,5 +323,10 @@ def test(self) -> None: self.assertIsInstance(str(exc), str) +class LoopIndexNotUsedExceptionTest(unittest.TestCase): + def str_test(self): + self.assertEqual(str(LoopIndexNotUsedException('a', {'b', 'c'})), "The parameter a is missing in the body's parameter names: {}".format({'b', 'c'})) + + if __name__ == "__main__": unittest.main(verbosity=2) \ No newline at end of file diff --git a/tests/pulses/pulse_template_parameter_mapping_tests.py b/tests/pulses/pulse_template_parameter_mapping_tests.py index 254151cb0..5a005c7f2 100644 --- a/tests/pulses/pulse_template_parameter_mapping_tests.py +++ b/tests/pulses/pulse_template_parameter_mapping_tests.py @@ -40,6 +40,9 @@ def test_from_tuple_exceptions(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, measurement_names={'foo', 'foobar'}, defined_channels={'bar', 'foobar'}) + + with self.assertRaises(ValueError): + MappingTemplate.from_tuple((template, {'A': 'B'})) with self.assertRaises(AmbiguousMappingException): MappingTemplate.from_tuple((template, {'foo': 'foo'})) with self.assertRaises(AmbiguousMappingException): @@ -51,6 +54,14 @@ def test_from_tuple_exceptions(self): with self.assertRaises(MappingCollisionException): MappingTemplate.from_tuple((template, {'foo': '1', 'bar': 2}, {'foo': '1', 'bar': 4})) + template = DummyPulseTemplate(defined_channels={'A'}) + with self.assertRaises(MappingCollisionException): + MappingTemplate.from_tuple((template, {'A': 'N'}, {'A': 'C'})) + + template = DummyPulseTemplate(measurement_names={'M'}) + with self.assertRaises(MappingCollisionException): + MappingTemplate.from_tuple((template, {'M': 'N'}, {'M': 'N'})) + def test_from_tuple(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, measurement_names={'m1', 'm2'}, @@ -109,6 +120,34 @@ def test_map_parameters(self): with self.assertRaises(ParameterNotProvidedException): st.map_parameters(parameters) + parameters = dict(t=3, k=2, l=7) + values = {'foo': 6, 'bar': 21} + for k, v in st.map_parameters(parameters).items(): + self.assertEqual(v.get_value(), values[k]) + + def test_partial_parameter_mapping(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + st = MappingTemplate(template, parameter_mapping={'foo': 't*k'}, allow_partial_parameter_mapping=True) + + self.assertEqual(st.parameter_mapping, {'foo': 't*k', 'bar': 'bar'}) + + def test_nested_mapping_avoidance(self): + template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) + st_1 = MappingTemplate(template, parameter_mapping={'foo': 't*k'}, allow_partial_parameter_mapping=True) + st_2 = MappingTemplate(st_1, parameter_mapping={'bar': 't*l'}, allow_partial_parameter_mapping=True) + + self.assertIs(st_2.template, template) + self.assertEqual(st_2.parameter_mapping, {'foo': 't*k', 'bar': 't*l'}) + + st_3 = MappingTemplate(template, + parameter_mapping={'foo': 't*k'}, + allow_partial_parameter_mapping=True, + identifier='käse') + st_4 = MappingTemplate(st_3, parameter_mapping={'bar': 't*l'}, allow_partial_parameter_mapping=True) + self.assertIs(st_4.template, st_3) + self.assertEqual(st_4.parameter_mapping, {'t': 't', 'k': 'k', 'bar': 't*l'}) + + def test_get_updated_channel_mapping(self): template = DummyPulseTemplate(defined_channels={'foo', 'bar'}) st = MappingTemplate(template, channel_mapping={'bar': 'kneipe'}) diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index c5d36f055..e69de29bb 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -1 +0,0 @@ -__all__ = ['type_check_tests'] \ No newline at end of file diff --git a/tests/utils/utils_tests.py b/tests/utils/utils_tests.py new file mode 100644 index 000000000..f5005e57c --- /dev/null +++ b/tests/utils/utils_tests.py @@ -0,0 +1,34 @@ +import unittest +from qctoolkit.utils import checked_int_cast + + +class CheckedIntCastTest(unittest.TestCase): + def test_int_forwarding(self): + my_int = 6 + self.assertIs(my_int, checked_int_cast(my_int)) + + def test_no_int_detection(self): + with self.assertRaises(ValueError): + checked_int_cast(0.5) + + with self.assertRaises(ValueError): + checked_int_cast(-0.5) + + with self.assertRaises(ValueError): + checked_int_cast(123124.2) + + with self.assertRaises(ValueError): + checked_int_cast(123124 + 1e-6) + + def test_float_cast(self): + self.assertEqual(6, checked_int_cast(6+1e-11)) + + self.assertEqual(-6, checked_int_cast(-6 + 1e-11)) + + def test_variable_epsilon(self): + self.assertEqual(6, checked_int_cast(6 + 1e-11)) + + with self.assertRaises(ValueError): + checked_int_cast(6 + 1e-11, epsilon=1e-15) + + From f739d71a71be329cffda5ab7e6b2fc13de159a62 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Fri, 18 Aug 2017 15:17:12 +0200 Subject: [PATCH 093/116] Make tests pass --- tests/pulses/pulse_template_parameter_mapping_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/pulses/pulse_template_parameter_mapping_tests.py b/tests/pulses/pulse_template_parameter_mapping_tests.py index 5a005c7f2..ba863efb7 100644 --- a/tests/pulses/pulse_template_parameter_mapping_tests.py +++ b/tests/pulses/pulse_template_parameter_mapping_tests.py @@ -123,7 +123,7 @@ def test_map_parameters(self): parameters = dict(t=3, k=2, l=7) values = {'foo': 6, 'bar': 21} for k, v in st.map_parameters(parameters).items(): - self.assertEqual(v.get_value(), values[k]) + self.assertEqual(v, values[k]) def test_partial_parameter_mapping(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) From 6975fdf11dcaf3ed5e5c1831c81a5e362f09baf5 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 21 Aug 2017 13:06:22 +0200 Subject: [PATCH 094/116] Remove dead code --- .../pulses/multi_channel_pulse_template.py | 16 ------------- qctoolkit/pulses/parameters.py | 23 ------------------- 2 files changed, 39 deletions(-) diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index 80da2795e..d44405807 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -28,22 +28,6 @@ __all__ = ["MultiChannelWaveform", "AtomicMultiChannelPulseTemplate"] -def _parse_subtemplates(subtemplates): - subtemplates = [st if isinstance(st, PulseTemplate) else MappingTemplate.from_tuple(st) for st in subtemplates] - - defined_channels = [st.defined_channels for st in subtemplates] - - # check there are no intersections between channels - for i, channels_i in enumerate(defined_channels): - for j, channels_j in enumerate(defined_channels[i + 1:]): - if channels_i & channels_j: - raise ChannelMappingException(subtemplates[i], - subtemplates[i + 1 + j], - (channels_i | channels_j).pop()) - - return subtemplates - - class MultiChannelWaveform(Waveform): """A MultiChannelWaveform is a Waveform object that allows combining arbitrary Waveform objects to into a single waveform defined for several channels. diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index 624f6f04c..052ee7e8b 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -23,29 +23,6 @@ "ParameterNotProvidedException", "ParameterConstraintViolation"] -def make_parameter(value): - """Convenience function """ - if isinstance(value, Parameter): - return value - if isinstance(value, Real): - return ConstantParameter(value) - if isinstance(value, str): - return MappedParameter(Expression(value)) - raise TypeError('Can not convert object of type {} to a parameter'.format(type(value))) - - -class ParameterDict(dict): - """Conve""" - def __init__(self, *args, **kwargs): - super().__init__( - *((k, make_parameter(v)) for k, v in args), - **dict((k, make_parameter(v)) for k, v in kwargs.items()) - ) - - def __setitem__(self, key, value) -> None: - super().__setitem__(key, make_parameter(value)) - - class Parameter(Serializable, Comparable, metaclass=ABCMeta): """A parameter for pulses. From bfbe26210cc0aa7685015c60798840e70cb4d8bc Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Mon, 21 Aug 2017 13:12:25 +0200 Subject: [PATCH 095/116] Increase test coverage --- qctoolkit/pulses/instructions.py | 11 +- qctoolkit/pulses/repetition_pulse_template.py | 15 ++- qctoolkit/pulses/sequence_pulse_template.py | 31 ++++-- qctoolkit/pulses/table_pulse_template.py | 57 ++++------ qctoolkit/utils/__init__.py | 7 +- tests/pulses/instructions_tests.py | 23 +++- tests/pulses/loop_pulse_template_tests.py | 5 +- .../multi_channel_pulse_template_tests.py | 66 ++++++++++- tests/pulses/parameters_tests.py | 15 ++- tests/pulses/pulse_template_tests.py | 7 +- .../pulses/repetition_pulse_template_tests.py | 67 ++++++++++++ tests/pulses/sequence_pulse_template_tests.py | 65 ++++++++++- tests/pulses/table_pulse_template_tests.py | 100 +++++++++++------ tests/serialization_tests.py | 103 +++++++++++++++++- 14 files changed, 463 insertions(+), 109 deletions(-) diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index a40705436..f4eee6986 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -323,8 +323,9 @@ def compare_key(self) -> Dict[ChannelID, InstructionPointer]: return self.channel_to_instruction_block def __str__(self) -> str: - return "chan " + ",".join("{target} for {channel}" - .format(target=v,channel=k) for k,v in self.channel_to_instruction_block.items()) + return "chan " + ", ".join("{target} for {channel}" + .format(target=v, channel=k) + for k, v in sorted(self.channel_to_instruction_block.items(), key=lambda arg: arg[0])) def __getitem__(self, item) -> InstructionPointer: return self.channel_to_instruction_block[item] @@ -392,11 +393,11 @@ def __getitem__(self, index: Union[int,slice]) -> Union[Instruction,Iterable[Ins if index > len(self.instructions) or index < -(len(self.instructions) + 1): raise IndexError() - if index < 0: + elif index < 0: return self[len(self) + index] - if index < len(self.instructions): + elif index < len(self.instructions): return self.instructions[index] - elif index == len(self.instructions): + else: if self.return_ip is None: return STOPInstruction() else: diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index b1cf38eb9..b10513f3c 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -9,6 +9,7 @@ from qctoolkit import MeasurementWindow, ChannelID from qctoolkit.expressions import Expression +from qctoolkit.utils import checked_int_cast from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.loop_pulse_template import LoopPulseTemplate from qctoolkit.pulses.sequencing import Sequencer @@ -109,12 +110,10 @@ def repetition_count(self) -> Expression: def get_repetition_count_value(self, parameters: Dict[str, 'Real']) -> int: value = self._repetition_count.evaluate_numeric(**parameters) - if isinstance(value, float): - if value.is_integer(): - value = int(value) - else: - raise ParameterNotIntegerException(str(self._repetition_count), value) - return value + try: + return checked_int_cast(value) + except ValueError: + raise ParameterNotIntegerException(str(self._repetition_count), value) def __str__(self) -> str: return "RepetitionPulseTemplate: <{}> times <{}>"\ @@ -130,7 +129,7 @@ def measurement_names(self) -> Set[str]: @property def duration(self) -> Expression: - return Expression(self.repetition_count * self.body.duration.sympified_expression) + return Expression(self.repetition_count.sympified_expression * self.body.duration.sympified_expression) def build_sequence(self, sequencer: Sequencer, @@ -179,7 +178,7 @@ def deserialize(serializer: Serializer, class ParameterNotIntegerException(Exception): """Indicates that the value of the parameter given as repetition count was not an integer.""" - def __init__(self, parameter_name: str, parameter_value: float) -> None: + def __init__(self, parameter_name: str, parameter_value: Any) -> None: super().__init__() self.parameter_name = parameter_name self.parameter_value = parameter_value diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index bc2de7a6f..72ade7114 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -3,7 +3,7 @@ import numpy as np from typing import Dict, List, Tuple, Set, Optional, Any, Iterable, Union -import itertools +from numbers import Real from qctoolkit.serialization import Serializer @@ -161,8 +161,7 @@ def __init__(self, (self.constrained_parameters-external_parameters).pop()) remaining -= self.constrained_parameters if remaining: - MissingMappingException(self, remaining.pop()) - self.__atomicity = False + raise MissingMappingException(self, remaining.pop()) @property def parameter_names(self) -> Set[str]: @@ -195,8 +194,15 @@ def requires_stop(self, SequencePulseTemplate can be partially sequenced.""" return self.__subtemplates[0].requires_stop(parameters, conditions) if self.__subtemplates else False - def build_waveform(self, parameters: Dict[str, Parameter]) -> SequenceWaveform: - return SequenceWaveform([subtemplate.build_waveform(parameters) for subtemplate in self.__subtemplates]) + def build_waveform(self, + parameters: Dict[str, Real], + measurement_mapping: Dict[str, str], + channel_mapping: Dict[ChannelID, ChannelID]) -> SequenceWaveform: + self.validate_parameter_constraints(parameters=parameters) + return SequenceWaveform([sub_template.build_waveform(parameters, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) + for sub_template in self.__subtemplates]) def build_sequence(self, sequencer: Sequencer, @@ -205,13 +211,14 @@ def build_sequence(self, measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: - for subtemplate in reversed(self.subtemplates): - sequencer.push(subtemplate, - parameters=parameters, - conditions=conditions, - window_mapping=measurement_mapping, - channel_mapping=channel_mapping, - target_block=instruction_block) + self.validate_parameter_constraints(parameters=parameters) + for subtemplate in reversed(self.subtemplates): + sequencer.push(subtemplate, + parameters=parameters, + conditions=conditions, + window_mapping=measurement_mapping, + channel_mapping=channel_mapping, + target_block=instruction_block) def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: data = dict(subtemplates=[serializer.dictify(subtemplate) for subtemplate in self.subtemplates], diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index 1e11e3e42..afe514194 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -71,22 +71,38 @@ def __init__(self, self._measurement_windows = tuple(measurement_windows) @staticmethod - def _validate_input(input_waveform_table: Sequence[TableWaveformEntry]) -> Tuple[TableWaveformEntry]: + def _validate_input(input_waveform_table: Sequence[TableWaveformEntry]) -> Tuple[TableWaveformEntry, ...]: + """ Checks that: + - the time is increasing, + - there are at least two entries + and removes subsequent entries with same time or voltage values. + + :param input_waveform_table: + :return: + """ if len(input_waveform_table) < 2: raise ValueError("Waveform table has less than two entries.") times = np.fromiter((t for t, *_ in input_waveform_table), dtype=float, count=len(input_waveform_table)) if times[0] != 0: raise ValueError('First time point has to be 0') + if times[-1] == 0: + raise ValueError('Last time point has to be larger 0') diff_times = np.diff(times) - if np.any(diff_times) < 0: + if np.any(diff_times < 0): raise ValueError('Times are not increasing') # filter 3 subsequent equal times to_keep = np.full_like(times, True, dtype=np.bool_) to_keep[1:-1] = np.logical_or(0 != diff_times[:-1], diff_times[:-1] != diff_times[1:]) + voltages = np.fromiter((v for _, v, _ in input_waveform_table), dtype=float, count=len(input_waveform_table)) + diff_voltages = np.diff(voltages[to_keep]) + + # filter 3 subsequent equal voltages + to_keep[1:-1][to_keep[1:-1]] = np.logical_or(0 != diff_voltages[:-1], diff_voltages[1:] != 0) + return tuple(entry if isinstance(entry, TableWaveformEntry) else TableWaveformEntry(*entry) for entry, keep_entry in zip(input_waveform_table, to_keep) if keep_entry) @@ -244,9 +260,7 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ instantiated_entries = dict() # type: Dict[ChannelID,List[TableWaveformEntry]] for channel, channel_entries in self._entries.items(): - instantiated = [TableWaveformEntry(entry.t.evaluate_numeric(**parameters), - entry.v.evaluate_numeric(**parameters), - entry.interp) + instantiated = [entry.instantiate(parameters) for entry in channel_entries] # Add (0, v) entry if wf starts at finite time @@ -254,11 +268,6 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ instantiated.insert(0, TableWaveformEntry(0, instantiated[0].v, TablePulseTemplate.interpolation_strategies['hold'])) - - for (previous_time, _, _), (time, _, _) in zip(instantiated, instantiated[1:]): - if time < previous_time: - raise Exception("Time value {0} is smaller than the previous value {1}." - .format(time, previous_time)) instantiated_entries[channel] = instantiated duration = max(instantiated[-1].t for instantiated in instantiated_entries.values()) @@ -270,31 +279,9 @@ def get_entries_instantiated(self, parameters: Dict[str, numbers.Real]) \ instantiated.append(TableWaveformEntry(duration, final_entry.v, TablePulseTemplate.interpolation_strategies['hold'])) - instantiated_entries[channel] = TablePulseTemplate._remove_redundant_entries(instantiated) + instantiated_entries[channel] = instantiated return instantiated_entries - @staticmethod - def _remove_redundant_entries(entries: List[TableWaveformEntry]) -> List[TableWaveformEntry]: - """ Checks if three subsequent values in a list of table entries have the same value. - If so, the intermediate is redundant and removed in-place. - - Args: - entries (List(TableEntry)): List of table entries to clean. Will be modified in-place. - Returns: - a reference to entries - """ - length = len(entries) - if not entries or length < 3: - return entries - - for index in range(length - 2, 0, -1): - previous_step = entries[index - 1] - step = entries[index] - next_step = entries[index + 1] - if step.v == previous_step.v and step.v == next_step.v: - entries.pop(index) - return entries - @property def table_parameters(self) -> Set[str]: return set( @@ -334,10 +321,6 @@ def requires_stop(self, except KeyError as key_error: raise ParameterNotProvidedException(str(key_error)) from key_error - @property - def num_channels(self) -> int: - return len(self._entries) - def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: return dict( entries=dict( diff --git a/qctoolkit/utils/__init__.py b/qctoolkit/utils/__init__.py index 3b03f45d1..10795e6ee 100644 --- a/qctoolkit/utils/__init__.py +++ b/qctoolkit/utils/__init__.py @@ -1,9 +1,14 @@ from typing import Union +import numpy + __all__ = ["checked_int_cast"] -def checked_int_cast(x: Union[float, int], epsilon: float=1e-10) -> int: +def checked_int_cast(x: Union[float, int, numpy.ndarray], epsilon: float=1e-10) -> int: + if isinstance(x, numpy.ndarray): + if len(x) != 1: + raise ValueError('Not a scalar value') if isinstance(x, int): return x int_x = int(round(x)) diff --git a/tests/pulses/instructions_tests.py b/tests/pulses/instructions_tests.py index 9f671da63..3c48b6a9e 100644 --- a/tests/pulses/instructions_tests.py +++ b/tests/pulses/instructions_tests.py @@ -4,7 +4,7 @@ from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer,\ Trigger, CJMPInstruction, REPJInstruction, GOTOInstruction, EXECInstruction, STOPInstruction,\ - InstructionSequence, AbstractInstructionBlock, ImmutableInstructionBlock, Instruction + InstructionSequence, AbstractInstructionBlock, ImmutableInstructionBlock, Instruction, CHANInstruction from tests.pulses.sequencing_dummies import DummyWaveform, DummyInstructionBlock @@ -229,6 +229,27 @@ def test_str(self) -> None: self.assertEqual("goto to {}".format(str(InstructionPointer(block, 3))), str(instr)) +class CHANInstructionTest(unittest.TestCase): + def test_compare_key(self): + c_to_i = dict(a=5) + + instr = CHANInstruction(c_to_i) + + self.assertIs(instr.compare_key, c_to_i) + self.assertIs(instr.channel_to_instruction_block, c_to_i) + + def test_get_item(self): + c_to_i = dict(a='b') + instr = CHANInstruction(c_to_i) + + self.assertIs(instr['a'], c_to_i['a']) + + def test_str(self): + c_to_i = dict(a='b', c='d') + instr = CHANInstruction(c_to_i) + + self.assertEqual(str(instr), 'chan b for a, d for c') + class EXECInstructionTest(unittest.TestCase): def test_initialization(self): diff --git a/tests/pulses/loop_pulse_template_tests.py b/tests/pulses/loop_pulse_template_tests.py index 3430dac88..1795f3c14 100644 --- a/tests/pulses/loop_pulse_template_tests.py +++ b/tests/pulses/loop_pulse_template_tests.py @@ -5,7 +5,7 @@ from qctoolkit.expressions import Expression from qctoolkit.pulses.loop_pulse_template import ForLoopPulseTemplate, WhileLoopPulseTemplate,\ ConditionMissingException, ParametrizedRange, LoopIndexNotUsedException, LoopPulseTemplate -from qctoolkit.pulses.parameters import ConstantParameter +from qctoolkit.pulses.parameters import ConstantParameter, InvalidParameterNameException from tests.pulses.sequencing_dummies import DummyCondition, DummyPulseTemplate, DummySequencer, DummyInstructionBlock,\ DummyParameter @@ -90,6 +90,9 @@ def test_init(self): loop_range=ParametrizedRange('a', 'b', 'c')).loop_range.to_tuple(), ('a', 'b', 'c')) + with self.assertRaises(InvalidParameterNameException): + ForLoopPulseTemplate(body=dt, loop_index='1i', loop_range=6) + with self.assertRaises(ValueError): ForLoopPulseTemplate(body=dt, loop_index='i', loop_range=slice(None)) diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index 81bf79116..f76f42fb7 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -5,7 +5,7 @@ from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ MissingParameterDeclarationException from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform, MappingTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate -from qctoolkit.pulses.parameters import ParameterConstraint +from qctoolkit.pulses.parameters import ParameterConstraint, ParameterConstraintViolation from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform from tests.serialization_dummies import DummySerializer @@ -19,6 +19,20 @@ def test_init_no_args(self) -> None: with self.assertRaises(ValueError): MultiChannelWaveform(None) + def test_get_item(self): + dwf_a = DummyWaveform(duration=2.2, defined_channels={'A'}) + dwf_b = DummyWaveform(duration=2.2, defined_channels={'B'}) + dwf_c = DummyWaveform(duration=2.2, defined_channels={'C'}) + + wf = MultiChannelWaveform([dwf_a, dwf_b, dwf_c]) + + self.assertIs(wf['A'], dwf_a) + self.assertIs(wf['B'], dwf_b) + self.assertIs(wf['C'], dwf_c) + + with self.assertRaises(KeyError): + wf['D'] + def test_init_single_channel(self) -> None: dwf = DummyWaveform(duration=1.3, defined_channels={'A'}) @@ -164,6 +178,7 @@ def test_init_empty(self) -> None: def test_non_atomic_subtemplates(self): non_atomic_pt = PulseTemplateStub(duration='t1', defined_channels={'A'}, parameter_names=set()) atomic_pt = DummyPulseTemplate(defined_channels={'B'}, duration='t1') + non_atomic_mapping = MappingTemplate(non_atomic_pt) with self.assertRaises(TypeError): AtomicMultiChannelPulseTemplate(non_atomic_pt) @@ -172,10 +187,10 @@ def test_non_atomic_subtemplates(self): AtomicMultiChannelPulseTemplate(non_atomic_pt, atomic_pt) with self.assertRaises(TypeError): - AtomicMultiChannelPulseTemplate(MappingTemplate(non_atomic_pt), atomic_pt) + AtomicMultiChannelPulseTemplate(non_atomic_mapping, atomic_pt) with self.assertRaises(TypeError): - AtomicMultiChannelPulseTemplate((non_atomic_pt, {'B': 'C'}), atomic_pt) + AtomicMultiChannelPulseTemplate((non_atomic_pt, {'A': 'C'}), atomic_pt) def test_duration(self): sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}), @@ -248,6 +263,51 @@ def test_defined_channels(self): template = AtomicMultiChannelPulseTemplate(*subtemp_args) self.assertEqual(template.defined_channels, {'cc1', 'cc2', 'cc3'}) + def test_measurement_names(self): + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}, measurement_names={'A', 'C'}), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'}, measurement_names={'A', 'B'})] + + self.assertEqual(AtomicMultiChannelPulseTemplate(*sts).measurement_names, {'A', 'B', 'C'}) + + def test_requires_stop(self): + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}, requires_stop=False), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'}, requires_stop=False)] + + self.assertFalse(AtomicMultiChannelPulseTemplate(*sts).requires_stop(dict(), dict())) + sts = [ + DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}, requires_stop=False), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'}, requires_stop=True)] + + self.assertTrue(AtomicMultiChannelPulseTemplate(*sts).requires_stop(dict(), dict())) + + def test_build_waveform(self): + wfs = [DummyWaveform(duration=1.1, defined_channels={'A'}), DummyWaveform(duration=1.1, defined_channels={'B'})] + + sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}, + measurement_names={'A', 'C'}, waveform=wfs[0]), + DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'}, + measurement_names={'A', 'B'}, waveform=wfs[1])] + + pt = AtomicMultiChannelPulseTemplate(*sts, parameter_constraints=['a < b']) + + parameters = dict(a=2.2, b = 1.1, c=3.3) + channel_mapping = dict() + measurement_mapping = dict() + with self.assertRaises(ParameterConstraintViolation): + pt.build_waveform(parameters, channel_mapping=dict(), measurement_mapping=dict()) + + parameters['a'] = 0.5 + wf = pt.build_waveform(parameters, channel_mapping=channel_mapping, measurement_mapping=measurement_mapping) + self.assertEqual(wf['A'], wfs[0]) + self.assertEqual(wf['B'], wfs[1]) + + for st in sts: + self.assertEqual(st.build_waveform_calls, [(parameters, measurement_mapping, channel_mapping)]) + self.assertIs(parameters, st.build_waveform_calls[0][0]) + self.assertIs(measurement_mapping, st.build_waveform_calls[0][1]) + self.assertIs(channel_mapping, st.build_waveform_calls[0][2]) + + def test_deserialize(self): sts = [DummyPulseTemplate(duration='t1', defined_channels={'A'}, parameter_names={'a', 'b'}), DummyPulseTemplate(duration='t1', defined_channels={'B'}, parameter_names={'a', 'c'})] diff --git a/tests/pulses/parameters_tests.py b/tests/pulses/parameters_tests.py index 86fc8ab93..7229a3707 100644 --- a/tests/pulses/parameters_tests.py +++ b/tests/pulses/parameters_tests.py @@ -3,7 +3,7 @@ from qctoolkit.expressions import Expression from qctoolkit.pulses.parameters import ConstantParameter, MappedParameter, ParameterNotProvidedException,\ - ParameterConstraint, ParameterConstraintViolation + ParameterConstraint, ParameterConstraintViolation, InvalidParameterNameException from tests.serialization_dummies import DummySerializer from tests.pulses.sequencing_dummies import DummyParameter @@ -117,6 +117,11 @@ def test_no_relation(self): ParameterConstraint('a*b') ParameterConstraint('1 < 2') + def test_str(self): + self.assertEqual(str(ParameterConstraint('a < b')), 'a < b') + + self.assertEqual(str(ParameterConstraint('a==b')), 'a==b') + class ParameterNotProvidedExceptionTests(unittest.TestCase): @@ -124,6 +129,14 @@ def test(self) -> None: exc = ParameterNotProvidedException('foo') self.assertEqual("No value was provided for parameter 'foo' and no default value was specified.", str(exc)) + +class InvalidParameterNameExceptionTests(unittest.TestCase): + def test(self): + exception = InvalidParameterNameException('asd') + + self.assertEqual(exception.parameter_name, 'asd') + self.assertEqual(str(exception), 'asd is an invalid parameter name') + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index 6ea15ecd3..a696ca333 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -17,12 +17,14 @@ class PulseTemplateStub(PulseTemplate): def __init__(self, identifier=None, defined_channels=None, duration=None, - parameter_names=None): + parameter_names=None, + measurement_names=None): super().__init__(identifier=identifier) self._defined_channels = defined_channels self._duration = duration self._parameter_names = parameter_names + self._measurement_names = set() if measurement_names is None else measurement_names @property def defined_channels(self) -> Set['ChannelID']: @@ -69,8 +71,9 @@ def build_sequence(self, def is_interruptable(self): raise NotImplementedError() + @property def measurement_names(self): - raise NotImplementedError() + return self._measurement_names def requires_stop(self, parameters: Dict[str, Parameter], diff --git a/tests/pulses/repetition_pulse_template_tests.py b/tests/pulses/repetition_pulse_template_tests.py index baf7d2707..b8c819aa1 100644 --- a/tests/pulses/repetition_pulse_template_tests.py +++ b/tests/pulses/repetition_pulse_template_tests.py @@ -2,6 +2,7 @@ import numpy as np +from qctoolkit.expressions import Expression from qctoolkit.pulses.repetition_pulse_template import RepetitionPulseTemplate,ParameterNotIntegerException, RepetitionWaveform from qctoolkit.pulses.parameters import ParameterNotProvidedException, ParameterConstraintViolation, ConstantParameter, \ ParameterConstraint @@ -16,6 +17,43 @@ class RepetitionWaveformTest(unittest.TestCase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + def test_init(self): + body_wf = DummyWaveform() + + with self.assertRaises(ValueError): + RepetitionWaveform(body_wf, -1) + + with self.assertRaises(ValueError): + RepetitionWaveform(body_wf, 1.1) + + wf = RepetitionWaveform(body_wf, 3) + self.assertIs(wf._body, body_wf) + self.assertEqual(wf._repetition_count, 3) + + def test_duration(self): + wf = RepetitionWaveform(DummyWaveform(duration=2.2), 3) + self.assertEqual(wf.duration, 2.2*3) + + def test_defined_channels(self): + body_wf = DummyWaveform(defined_channels={'a'}) + self.assertIs(RepetitionWaveform(body_wf, 2).defined_channels, body_wf.defined_channels) + + def test_compare_key(self): + body_wf = DummyWaveform(defined_channels={'a'}) + wf = RepetitionWaveform(body_wf, 2) + self.assertEqual(wf.compare_key, (body_wf.compare_key, 2)) + + def test_unsafe_get_subset_for_channels(self): + body_wf = DummyWaveform(defined_channels={'a', 'b'}) + + chs = {'a'} + + subset = RepetitionWaveform(body_wf, 3).get_subset_for_channels(chs) + self.assertIsInstance(subset, RepetitionWaveform) + self.assertIsInstance(subset._body, DummyWaveform) + self.assertIs(subset._body.defined_channels, chs) + self.assertEqual(subset._repetition_count, 3) + def test_unsafe_sample(self): body_wf = DummyWaveform(duration=7) @@ -55,6 +93,9 @@ def test_init(self) -> None: self.assertEqual(repetition_count, t.repetition_count) self.assertEqual(body, t.body) + with self.assertRaises(ValueError): + RepetitionPulseTemplate(body, Expression(-1)) + def test_parameter_names_and_declarations(self) -> None: body = DummyPulseTemplate() t = RepetitionPulseTemplate(body, 5) @@ -79,6 +120,19 @@ def test_str(self) -> None: t = RepetitionPulseTemplate(body, 'foo') self.assertIsInstance(str(t), str) + def test_measurement_names(self): + measurement_names = {'M'} + body = DummyPulseTemplate(measurement_names=measurement_names) + t = RepetitionPulseTemplate(body, 9) + + self.assertIs(measurement_names, t.measurement_names) + + def test_duration(self): + body = DummyPulseTemplate(duration='foo') + t = RepetitionPulseTemplate(body, 'bar') + + self.assertEqual(t.duration, Expression('foo*bar')) + class RepetitionPulseTemplateSequencingTests(unittest.TestCase): @@ -142,6 +196,19 @@ def test_build_sequence_declaration_success(self) -> None: self.sequencer.sequencing_stacks[body_block]) self.assertEqual([REPJInstruction(3, InstructionPointer(body_block, 0))], self.block.instructions) + def test_parameter_not_provided(self): + parameters = dict(foo=ConstantParameter(4)) + conditions = dict(foo=DummyCondition(requires_stop=True)) + measurement_mapping = dict(moth='fire') + channel_mapping = dict(asd='f') + + template = RepetitionPulseTemplate(self.body, 'foo*bar', parameter_constraints=['foo<9']) + + with self.assertRaises(ParameterNotProvidedException): + template.build_sequence(self.sequencer, parameters, conditions, measurement_mapping, channel_mapping, + self.block) + + def test_build_sequence_declaration_exceeds_bounds(self) -> None: parameters = dict(foo=ConstantParameter(9)) conditions = dict(foo=DummyCondition(requires_stop=True)) diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index 1e6a66c76..ac6408e7a 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -8,7 +8,7 @@ from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate, SequenceWaveform from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate -from qctoolkit.pulses.parameters import ParameterNotProvidedException, ConstantParameter, ParameterConstraint +from qctoolkit.pulses.parameters import ParameterNotProvidedException, ConstantParameter, ParameterConstraint, ParameterConstraintViolation from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate,\ DummyNoValueParameter, DummyWaveform @@ -23,6 +23,9 @@ def test_init(self): dwf_ab = DummyWaveform(duration=1.1, defined_channels={'A', 'B'}) dwf_abc = DummyWaveform(duration=2.2, defined_channels={'A', 'B', 'C'}) + with self.assertRaises(ValueError): + SequenceWaveform([]) + with self.assertRaises(ValueError): SequenceWaveform((dwf_ab, dwf_abc)) @@ -51,6 +54,25 @@ def test_unsafe_sample(self): output_2 = swf.unsafe_sample('A', sample_times=sample_times, output_array=output) self.assertIs(output_2, output) + def test_unsafe_get_subset_for_channels(self): + dwf_1 = DummyWaveform(duration=2.2, defined_channels={'A', 'B', 'C'}) + dwf_2 = DummyWaveform(duration=3.3, defined_channels={'A', 'B', 'C'}) + + wf = SequenceWaveform([dwf_1, dwf_2]) + + subset = {'A', 'C'} + sub_wf = wf.unsafe_get_subset_for_channels(subset) + self.assertIsInstance(sub_wf, SequenceWaveform) + + self.assertEqual(len(sub_wf.compare_key), 2) + self.assertEqual(sub_wf.compare_key[0].defined_channels, subset) + self.assertEqual(sub_wf.compare_key[1].defined_channels, subset) + + self.assertEqual(sub_wf.compare_key[0].duration, 2.2) + self.assertEqual(sub_wf.compare_key[1].duration, 3.3) + + + def test_get_measurement_windows(self): dwfs = (DummyWaveform(duration=1., measurement_windows=[('M', 0.2, 0.5)]), DummyWaveform(duration=3., measurement_windows=[('N', 0.6, 0.7)]), @@ -95,6 +117,47 @@ def __init__(self, *args, **kwargs) -> None: measurement_mapping=self.window_name_mapping), external_parameters=self.outer_parameters) + def test_init(self): + with self.assertRaises(MissingParameterDeclarationException): + SequencePulseTemplate(DummyPulseTemplate(parameter_names={'a', 'b'}), external_parameters={'a'}) + + with self.assertRaises(MissingParameterDeclarationException): + SequencePulseTemplate(DummyPulseTemplate(parameter_names={'a'}), + parameter_constraints=['b < 4'], + external_parameters={'a'}) + + with self.assertRaises(MissingMappingException): + SequencePulseTemplate(DummyPulseTemplate(parameter_names={'a'}), + parameter_constraints=['b < 4'], + external_parameters={'a', 'b', 'c'}) + + def test_duration(self): + pt = SequencePulseTemplate(DummyPulseTemplate(duration='a'), + DummyPulseTemplate(duration='a'), + DummyPulseTemplate(duration='b')) + self.assertEqual(pt.duration, Expression('a+a+b')) + + def test_build_waveform(self): + wfs = [DummyWaveform(), DummyWaveform()] + pts = [DummyPulseTemplate(waveform=wf) for wf in wfs] + + spt = SequencePulseTemplate(*pts, parameter_constraints=['a < 3']) + with self.assertRaises(ParameterConstraintViolation): + spt.build_waveform(dict(a=4), dict(), dict()) + + parameters = dict(a=2) + channel_mapping = dict() + measurement_mapping = dict() + wf = spt.build_waveform(parameters, channel_mapping=channel_mapping, measurement_mapping=measurement_mapping) + + for wfi, pt in zip(wfs, pts): + self.assertEqual(pt.build_waveform_calls, [(parameters, dict(), dict())]) + self.assertIs(pt.build_waveform_calls[0][0], parameters) + + self.assertIsInstance(wf, SequenceWaveform) + for wfa, wfb in zip(wf.compare_key, wfs): + self.assertIs(wfa, wfb) + def test_identifier(self) -> None: identifier = 'some name' pulse = SequencePulseTemplate(DummyPulseTemplate(), external_parameters=set(), identifier=identifier) diff --git a/tests/pulses/table_pulse_template_tests.py b/tests/pulses/table_pulse_template_tests.py index 44922ca17..6df9c500d 100644 --- a/tests/pulses/table_pulse_template_tests.py +++ b/tests/pulses/table_pulse_template_tests.py @@ -99,6 +99,10 @@ def test_inconsistent_parameters(self): TablePulseTemplate({0: [('a', 1), (2, 0)]}, parameter_constraints=['a>3']) + with self.assertRaises(ValueError): + TablePulseTemplate({0: [('a', 1), + (2, 0)]}, parameter_constraints=['2>3']) + @unittest.skip(reason='Needs a better inequality solver') def test_time_not_increasing_hard(self): with self.assertRaises(ValueError): @@ -240,33 +244,6 @@ def test_get_entries_instantiated_two_equal_entries(self) -> None: ] self.assertEqual({0: expected}, entries) - def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries(self) -> None: - table = TablePulseTemplate({0: [(1, 5), - (1.5, 5), - (2, 5), - (3, 0)]}) - entries = table.get_entries_instantiated(dict()) - expected = [ - TableEntry(0, 5, HoldInterpolationStrategy()), - TableEntry(2, 5, HoldInterpolationStrategy()), - TableEntry(3, 0, HoldInterpolationStrategy()) - ] - self.assertEqual({0: expected}, entries) - - def test_get_entries_instantiated_removal_for_three_subsequent_equal_entries_does_not_destroy_linear_interpolation(self) -> None: - table = TablePulseTemplate({0: [(0, 5), - (2, 5, 'linear'), - (5, 5), - (10, 0, 'linear')]}) - entries = table.get_entries_instantiated(dict()) - - expected = [ - TableEntry(0, 5, HoldInterpolationStrategy()), - TableEntry(5, 5, HoldInterpolationStrategy()), - TableEntry(10, 0, LinearInterpolationStrategy()) - ] - self.assertEqual({0: expected}, entries) - def test_from_array_exceptions(self): with self.assertRaises(ValueError): TablePulseTemplate.from_array(numpy.arange(0), numpy.arange(1), [0]) @@ -278,20 +255,21 @@ def test_from_array_exceptions(self): TablePulseTemplate.from_array(numpy.array(numpy.ndindex((1, 2, 1))), numpy.arange(2), [0]) with self.assertRaises(ValueError): - TablePulseTemplate.from_array(numpy.array(numpy.ndindex((3, 4))), - numpy.array(numpy.ndindex((3, 5))), [3, 4, 5]) + TablePulseTemplate.from_array(numpy.zeros(3), + numpy.zeros((3, 2, 3)), + [3, 4, 5]) with self.assertRaises(ValueError): - TablePulseTemplate.from_array(numpy.array(numpy.ndindex((3, 5))), - numpy.array(numpy.ndindex((3, 4))), [3, 4, 5]) + TablePulseTemplate.from_array(numpy.zeros((4, 2)), + numpy.zeros((3, 4)), [3, 4, 5]) with self.assertRaises(ValueError): - TablePulseTemplate.from_array(numpy.array(numpy.ndindex((2, 4))), - numpy.array(numpy.ndindex((3, 4))), [3, 4, 5]) + TablePulseTemplate.from_array(numpy.zeros((3, 2)), + numpy.array(numpy.ndindex((4, 6))), [3, 4, 5]) with self.assertRaises(ValueError): - TablePulseTemplate.from_array(numpy.array(numpy.ndindex((3, 4))), - numpy.array(numpy.ndindex((2, 4))), [3, 4, 5]) + TablePulseTemplate.from_array(numpy.zeros((3, 5)), + numpy.array(numpy.ndindex((3, 6))), [3, 4, 5]) def test_from_array_1D(self) -> None: times = numpy.array([0, 1, 3]) @@ -366,6 +344,12 @@ def test_from_entry_list(self): self.assertEqual(tpt.entries, entries) self.assertEqual(tpt.identifier, 'tpt') + tpt = TablePulseTemplate.from_entry_list([(0, 9, 8, 7, 'hold'), + (1, 2, 1, 3, 'hold'), + (4, 1, 2, 3, 'linear')], + identifier='tpt') + self.assertEqual(tpt.entries, entries) + entries = {k: entries[i] for k, i in zip('ABC', [0, 1, 2])} tpt = TablePulseTemplate.from_entry_list([(0, 9, 8, 7), @@ -376,6 +360,21 @@ def test_from_entry_list(self): self.assertEqual(tpt.entries, entries) self.assertEqual(tpt.identifier, 'tpt') + entries = {0: [(0, 9, HoldInterpolationStrategy()), + (1, 2, HoldInterpolationStrategy()), + (4, 1, HoldInterpolationStrategy())], + 1: [(0, 8, HoldInterpolationStrategy()), + (1, 1, HoldInterpolationStrategy()), + (4, 2, HoldInterpolationStrategy())], + 2: [(0, 7, HoldInterpolationStrategy()), + (1, 3, HoldInterpolationStrategy()), + (4, 3, HoldInterpolationStrategy())]} + tpt = TablePulseTemplate.from_entry_list([(0, 9, 8, 7), + (1, 2, 1, 3), + (4, 1, 2, 3)], + identifier='tpt') + self.assertEqual(tpt.entries, entries) + def test_add_entry_multi_same_time_param(self) -> None: pulse = TablePulseTemplate({0: [(1, 3), ('foo', 'bar'), @@ -603,6 +602,37 @@ def test_identifier(self) -> None: class TableWaveformTests(unittest.TestCase): + def test_validate_input(self): + with self.assertRaises(ValueError): + TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy())]) + + with self.assertRaises(ValueError): + TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()), + TableWaveformEntry(0.0, 0.3, HoldInterpolationStrategy())]) + + with self.assertRaises(ValueError): + TableWaveform._validate_input([TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy()), + TableWaveformEntry(0.2, 0.2, HoldInterpolationStrategy())]) + + with self.assertRaises(ValueError): + TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()), + TableWaveformEntry(0.2, 0.2, HoldInterpolationStrategy()), + TableWaveformEntry(0.1, 0.2, HoldInterpolationStrategy())]) + + validated = TableWaveform._validate_input([TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()), + TableWaveformEntry(0.1, 0.2, LinearInterpolationStrategy()), + TableWaveformEntry(0.1, 0.3, JumpInterpolationStrategy()), + TableWaveformEntry(0.1, 0.3, HoldInterpolationStrategy()), + TableWaveformEntry(0.2, 0.3, LinearInterpolationStrategy()), + TableWaveformEntry(0.3, 0.3, JumpInterpolationStrategy())]) + + self.assertEqual(validated, (TableWaveformEntry(0.0, 0.2, HoldInterpolationStrategy()), + TableWaveformEntry(0.1, 0.2, LinearInterpolationStrategy()), + TableWaveformEntry(0.1, 0.3, HoldInterpolationStrategy()), + TableWaveformEntry(0.3, 0.3, JumpInterpolationStrategy()))) + + + def test_duration(self) -> None: entries = [TableWaveformEntry(0, 0, HoldInterpolationStrategy()), TableWaveformEntry(5, 1, HoldInterpolationStrategy())] waveform = TableWaveform('A', entries, []) diff --git a/tests/serialization_tests.py b/tests/serialization_tests.py index 3c86f3bac..4548d4cd9 100644 --- a/tests/serialization_tests.py +++ b/tests/serialization_tests.py @@ -1,11 +1,13 @@ import unittest import os.path import json -import abc +import zipfile + from tempfile import TemporaryDirectory from typing import Optional, Dict, Any -from qctoolkit.serialization import FilesystemBackend, Serializer, CachingBackend, Serializable, ExtendedJSONEncoder +from qctoolkit.serialization import FilesystemBackend, Serializer, CachingBackend, Serializable, ExtendedJSONEncoder,\ + ZipFileBackend from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate @@ -104,6 +106,103 @@ def test_get_not_existing(self) -> None: self.backend.get(name) +class ZipFileBackendTests(unittest.TestCase): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def test_init(self): + with TemporaryDirectory() as tmp_dir: + + with self.assertRaises(NotADirectoryError): + ZipFileBackend(os.path.join(tmp_dir, 'fantasie', 'mehr_phantasie')) + + root = os.path.join(tmp_dir, 'root.zip') + + ZipFileBackend(root) + + self.assertTrue(zipfile.is_zipfile(root)) + + def test_init_keeps_data(self): + with TemporaryDirectory() as tmp_dir: + root = os.path.join(tmp_dir, 'root.zip') + with zipfile.ZipFile(root, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file: + zip_file.writestr('test_file.txt', 'chichichi') + + ZipFileBackend(root) + + with zipfile.ZipFile(root, 'r') as zip_file: + ma_string = zip_file.read('test_file.txt') + self.assertEqual(b'chichichi', ma_string) + + def test_path(self): + with TemporaryDirectory() as tmp_dir: + root = os.path.join(tmp_dir, 'root.zip') + be = ZipFileBackend(root) + self.assertEqual(be._path('foo'), 'foo.json') + + def test_exists(self): + with TemporaryDirectory() as tmp_dir: + root = os.path.join(tmp_dir, 'root.zip') + be = ZipFileBackend(root) + + self.assertFalse(be.exists('foo')) + + with zipfile.ZipFile(root, mode='w', compression=zipfile.ZIP_DEFLATED) as zip_file: + zip_file.writestr('foo.json', 'chichichi') + + self.assertTrue(be.exists('foo')) + + def test_put(self): + with TemporaryDirectory() as tmp_dir: + root = os.path.join(tmp_dir, 'root.zip') + + be = ZipFileBackend(root) + + be.put('foo', 'foo_data') + + with zipfile.ZipFile(root, 'r') as zip_file: + ma_string = zip_file.read('foo.json') + self.assertEqual(b'foo_data', ma_string) + + with self.assertRaises(FileExistsError): + be.put('foo', 'bar_data') + with zipfile.ZipFile(root, 'r') as zip_file: + ma_string = zip_file.read('foo.json') + self.assertEqual(b'foo_data', ma_string) + + be.put('foo', 'foo_bar_data', overwrite=True) + with zipfile.ZipFile(root, 'r') as zip_file: + ma_string = zip_file.read('foo.json') + self.assertEqual(b'foo_bar_data', ma_string) + + def test_get(self): + with TemporaryDirectory() as tmp_dir: + root = os.path.join(tmp_dir, 'root.zip') + be = ZipFileBackend(root) + + with self.assertRaises(KeyError): + be.get('foo') + + data = 'foo_data' + with zipfile.ZipFile(root, 'a') as zip_file: + zip_file.writestr('foo.json', data) + + self.assertEqual(be.get('foo'), data) + + def test_update(self): + with TemporaryDirectory() as tmp_dir: + root = os.path.join(tmp_dir, 'root.zip') + be = ZipFileBackend(root) + + be.put('foo', 'foo_data') + be.put('bar', 'bar_data') + + be._update('foo.json', 'foo_bar_data') + + self.assertEqual(be.get('foo'), 'foo_bar_data') + self.assertEqual(be.get('bar'), 'bar_data') + + class CachingBackendTests(unittest.TestCase): def setUp(self) -> None: From 363f6b94388f9646eda09424a61a47ec964437a5 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 22 Aug 2017 18:35:07 +0200 Subject: [PATCH 096/116] Move DummyAWG to tests --- qctoolkit/hardware/awgs/base.py | 78 +++------------------------------ tests/hardware/dummy_devices.py | 77 ++++++++++++++++++++++++++++++++ tests/hardware/setup_tests.py | 4 +- 3 files changed, 84 insertions(+), 75 deletions(-) create mode 100644 tests/hardware/dummy_devices.py diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index 7b5ff6302..9ecf7d0b9 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -31,7 +31,11 @@ class AWG(Comparable): """ def __init__(self, identifier: str): - self.identifier = identifier + self._identifier = identifier + + @property + def identifier(self) -> str: + return self._identifier @abstractproperty def num_channels(self): @@ -101,78 +105,6 @@ def __deepcopy__(self, memodict={}) -> None: raise NotImplementedError() -class DummyAWG(AWG): - """Dummy AWG for debugging purposes.""" - - def __init__(self, - memory: int=100, - sample_rate: float=10, - output_range: Tuple[float, float]=(-5, 5), - num_channels: int=1, - num_markers: int=1) -> None: - """Create a new DummyAWG instance. - - Args: - memory (int): Available memory slots for waveforms. (default = 100) - sample_rate (float): The sample rate of the dummy. (default = 10) - output_range (float, float): A (min,max)-tuple of possible output values. - (default = (-5,5)). - """ - self._programs = {} # contains program names and programs - self._waveform_memory = [None for i in range(memory)] - self._waveform_indices = {} # dict that maps from waveform hash to memory index - self._program_wfs = {} # contains program names and necessary waveforms indices - self._sample_rate = sample_rate - self._output_range = output_range - self._num_channels = num_channels - self._num_markers = num_markers - self._channels = ('default',) - self._armed = None - - def upload(self, name, program, channels, markers, voltage_transformation, force=False) -> None: - if name in self.programs: - if not force: - raise ProgramOverwriteException(name) - else: - self.remove(name) - self.upload(name, program) - else: - self._programs[name] = (program, channels, markers, voltage_transformation) - - def remove(self, name) -> None: - if name in self.programs: - self._programs.pop(name) - self.program_wfs.pop(name) - self.clean() - - def arm(self, name: str) -> None: - self._armed = name - - @property - def programs(self) -> Set[str]: - return frozenset(self._programs.keys()) - - @property - def output_range(self) -> Tuple[float, float]: - return self._output_range - - @property - def identifier(self) -> str: - return "DummyAWG{0}".format(id(self)) - - @property - def sample_rate(self) -> float: - return self._sample_rate - - @property - def num_channels(self): - return self._num_channels - - @property - def num_markers(self): - return self._num_markers - - class ProgramOverwriteException(Exception): def __init__(self, name) -> None: diff --git a/tests/hardware/dummy_devices.py b/tests/hardware/dummy_devices.py new file mode 100644 index 000000000..da6241cc0 --- /dev/null +++ b/tests/hardware/dummy_devices.py @@ -0,0 +1,77 @@ +from typing import Tuple, Set + +from qctoolkit.hardware.awgs.base import AWG, ProgramOverwriteException + + +class DummyAWG(AWG): + """Dummy AWG for debugging purposes.""" + + def __init__(self, + memory: int=100, + sample_rate: float=10, + output_range: Tuple[float, float]=(-5, 5), + num_channels: int=1, + num_markers: int=1) -> None: + """Create a new DummyAWG instance. + + Args: + memory (int): Available memory slots for waveforms. (default = 100) + sample_rate (float): The sample rate of the dummy. (default = 10) + output_range (float, float): A (min,max)-tuple of possible output values. + (default = (-5,5)). + """ + super().__init__(identifier="DummyAWG{0}".format(id(self))) + + self._programs = {} # contains program names and programs + self._waveform_memory = [None for i in range(memory)] + self._waveform_indices = {} # dict that maps from waveform hash to memory index + self._program_wfs = {} # contains program names and necessary waveforms indices + self._sample_rate = sample_rate + self._output_range = output_range + self._num_channels = num_channels + self._num_markers = num_markers + self._channels = ('default',) + self._armed = None + + def upload(self, name, program, channels, markers, voltage_transformation, force=False) -> None: + if name in self.programs: + if not force: + raise ProgramOverwriteException(name) + else: + self.remove(name) + self.upload(name, program) + else: + self._programs[name] = (program, channels, markers, voltage_transformation) + + def remove(self, name) -> None: + if name in self.programs: + self._programs.pop(name) + self.program_wfs.pop(name) + self.clean() + + def arm(self, name: str) -> None: + self._armed = name + + @property + def programs(self) -> Set[str]: + return set(self._programs.keys()) + + @property + def output_range(self) -> Tuple[float, float]: + return self._output_range + + @property + def identifier(self) -> str: + return "DummyAWG{0}".format(id(self)) + + @property + def sample_rate(self) -> float: + return self._sample_rate + + @property + def num_channels(self): + return self._num_channels + + @property + def num_markers(self): + return self._num_markers \ No newline at end of file diff --git a/tests/hardware/setup_tests.py b/tests/hardware/setup_tests.py index 72b45dc7c..0cc79d474 100644 --- a/tests/hardware/setup_tests.py +++ b/tests/hardware/setup_tests.py @@ -3,9 +3,9 @@ from qctoolkit.hardware.setup import HardwareSetup, ChannelID, PlaybackChannel, _SingleChannel, MarkerChannel -from qctoolkit.hardware.awgs.base import DummyAWG -from.program_tests import get_two_chan_test_block, WaveformGenerator +from tests.hardware.dummy_devices import DummyAWG +from tests.hardware.program_tests import get_two_chan_test_block, WaveformGenerator class SingleChannelTests(unittest.TestCase): From 72ad000f213410f4a857b7f8d9c4d5041f2a9549 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 23 Aug 2017 16:47:42 +0200 Subject: [PATCH 097/116] Extend setup tests --- qctoolkit/hardware/dacs/alazar.py | 6 +- qctoolkit/hardware/setup.py | 3 + tests/hardware/dummy_devices.py | 30 +++++- tests/hardware/setup_tests.py | 156 ++++++++++++++++++++++++++++-- 4 files changed, 183 insertions(+), 12 deletions(-) diff --git a/qctoolkit/hardware/dacs/alazar.py b/qctoolkit/hardware/dacs/alazar.py index ab904b8e6..f006f65e3 100644 --- a/qctoolkit/hardware/dacs/alazar.py +++ b/qctoolkit/hardware/dacs/alazar.py @@ -90,6 +90,9 @@ def arm_program(self, program_name: str) -> None: config = self.config config.masks, config.operations, total_record_size = self._registered_programs[program_name] + if len(config.operations) == 0: + raise RuntimeError('No operations configured for program {}'.format(program_name)) + if not config.masks: if config.operations: raise RuntimeError('Invalid configuration. Operations have no masks to work with') @@ -108,8 +111,7 @@ def arm_program(self, program_name: str) -> None: self.__card.startAcquisition(1) def delete_program(self, program_name: str) -> None: - self.__registered_operations.pop(program_name, None) - self.__registered_masks.pop(program_name, None) + self._registered_programs.pop(program_name) @property def mask_prototypes(self): diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index b5a5775e6..7da4f1d93 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -76,6 +76,9 @@ def register_program(self, name: str, instruction_block, run_callback=lambda: No raise TypeError('The provided run_callback is not callable') mcp = MultiChannelProgram(instruction_block) + if mcp.channels - set(self._channel_map.keys()): + raise KeyError('The following channels are unknown to the HardwareSetup: {}'.format( + mcp.channels - set(self._channel_map.keys()))) temp_measurement_windows = defaultdict(deque) for program in mcp.programs.values(): diff --git a/tests/hardware/dummy_devices.py b/tests/hardware/dummy_devices.py index da6241cc0..2574b7249 100644 --- a/tests/hardware/dummy_devices.py +++ b/tests/hardware/dummy_devices.py @@ -1,6 +1,34 @@ -from typing import Tuple, Set +from typing import Tuple, Set, Dict from qctoolkit.hardware.awgs.base import AWG, ProgramOverwriteException +from qctoolkit.hardware.dacs import DAC + +class DummyDAC(DAC): + def __init__(self): + self._measurement_windows = dict() + self._operations = dict() + + self._armed_program = None + + @property + def armed_program(self): + return self._armed_program + + def register_measurement_windows(self, program_name: str, windows: Dict[str, Tuple['numpy.ndarray', + 'numpy.ndarray']]): + self._measurement_windows[program_name] = windows + + def register_operations(self, program_name: str, operations): + self._operations[program_name] = operations + + def arm_program(self, program_name: str): + self._armed_program = program_name + + def delete_program(self, program_name): + if program_name in self._operations: + self._operations.pop(program_name) + if program_name in self._measurement_windows: + self._measurement_windows.pop(program_name) class DummyAWG(AWG): diff --git a/tests/hardware/setup_tests.py b/tests/hardware/setup_tests.py index 0cc79d474..0ec846e7e 100644 --- a/tests/hardware/setup_tests.py +++ b/tests/hardware/setup_tests.py @@ -1,10 +1,14 @@ import unittest import itertools +import numpy as np +from qctoolkit.pulses.instructions import InstructionBlock, EXECInstruction from qctoolkit.hardware.setup import HardwareSetup, ChannelID, PlaybackChannel, _SingleChannel, MarkerChannel -from tests.hardware.dummy_devices import DummyAWG +from tests.pulses.sequencing_dummies import DummyWaveform + +from tests.hardware.dummy_devices import DummyAWG, DummyDAC from tests.hardware.program_tests import get_two_chan_test_block, WaveformGenerator @@ -52,6 +56,13 @@ def test_eq_mark_mark(self): self.assertNotEqual(MarkerChannel(self.awg1, 0), MarkerChannel(self.awg2, 0)) + def test_exceptions(self): + with self.assertRaises(ValueError): + MarkerChannel(self.awg1, 2) + + with self.assertRaises(ValueError): + PlaybackChannel(self.awg1, 2) + class HardwareSetupTests(unittest.TestCase): def __init__(self, *args, **kwargs): @@ -76,15 +87,73 @@ def test_set_channel(self): dict(A={PlaybackChannel(awg2, 1), PlaybackChannel(awg1, 0)}, B={PlaybackChannel(awg2, 0)})) + def test_rm_channel(self): + awg1 = DummyAWG() + awg2 = DummyAWG(num_channels=2) + + setup = HardwareSetup() + + setup.set_channel('A', PlaybackChannel(awg1, 0)) + setup.set_channel('B', PlaybackChannel(awg2, 0)) + + with self.assertRaises(KeyError): + setup.rm_channel('b') + setup.rm_channel('B') + + self.assertEqual(setup.registered_channels(), + dict(A={PlaybackChannel(awg1, 0)})) + + def test_arm_program(self): + wf = DummyWaveform(duration=1.1, defined_channels={'A', 'B'}) + + block = InstructionBlock() + block.add_instruction_exec(wf) + + awg1 = DummyAWG() + awg2 = DummyAWG() + awg3 = DummyAWG() + + dac1 = DummyDAC() + dac2 = DummyDAC() + + setup = HardwareSetup() + + setup.set_channel('A', PlaybackChannel(awg1, 0)) + setup.set_channel('B', MarkerChannel(awg2, 0)) + setup.set_channel('C', MarkerChannel(awg3, 0)) + + setup.register_dac(dac1) + setup.register_dac(dac2) + + setup.register_program('test', block) + + self.assertIsNone(awg1._armed) + self.assertIsNone(awg2._armed) + self.assertIsNone(awg3._armed) + self.assertIsNone(dac1._armed_program) + self.assertIsNone(dac2._armed_program) + + setup.arm_program('test') + + self.assertEqual(awg1._armed, 'test') + self.assertEqual(awg2._armed, 'test') + self.assertIsNone(awg3._armed) + self.assertEqual(dac1._armed_program, 'test') + self.assertEqual(dac2._armed_program, 'test') + def test_register_program(self): awg1 = DummyAWG() awg2 = DummyAWG(num_channels=2, num_markers=5) + dac = DummyDAC() + setup = HardwareSetup() wfg = WaveformGenerator(num_channels=2, duration_generator=itertools.repeat(1)) block = get_two_chan_test_block(wfg) + block.instructions[0].waveform._sub_waveforms[0].measurement_windows_ = [('M', 0.1, 0.2)] + class ProgStart: def __init__(self): self.was_started = False @@ -94,16 +163,20 @@ def __call__(self): program_started = ProgStart() trafo1 = lambda x: x - trafo2 = lambda x: x setup.set_channel('A', PlaybackChannel(awg1, 0, trafo1)) - setup.set_channel('B', PlaybackChannel(awg2, 1, trafo2)) + setup.set_channel('B', MarkerChannel(awg2, 1)) + + setup.register_dac(dac) + setup.register_program('p1', block, program_started) self.assertEqual(tuple(setup.registered_programs.keys()), ('p1',)) self.assertEqual(setup.registered_programs['p1'].run_callback, program_started) self.assertEqual(setup.registered_programs['p1'].awgs_to_upload_to, {awg1, awg2}) + self.assertFalse(program_started.was_started) + self.assertEqual(len(awg1._programs), 1) self.assertEqual(len(awg2._programs), 1) @@ -113,15 +186,80 @@ def __call__(self): self.assertEqual(trafos, (trafo1,)) _, channels, markers, trafos = awg2._programs['p1'] - self.assertEqual(channels, (None, 'B')) - self.assertEqual(markers, (None,)*5) - self.assertEqual(trafos, (None, trafo2)) + self.assertEqual(channels, (None, None)) + self.assertEqual(markers, (None, 'B', None, None, None)) self.assertEqual(awg1._armed, None) self.assertEqual(awg2._armed, None) - setup.arm_program('p1') - self.assertEqual(awg1._armed, 'p1') - self.assertEqual(awg2._armed, 'p1') + np.testing.assert_equal(dac._measurement_windows, + dict(p1=dict(M=(np.array([0.1, 0.1]), np.array([0.2, 0.2]))))) + + def test_run_program(self): + wf = DummyWaveform(duration=1.1, defined_channels={'A', 'B'}) + + block = InstructionBlock() + block.add_instruction_exec(wf) + + awg1 = DummyAWG() + awg2 = DummyAWG() + awg3 = DummyAWG() + + dac1 = DummyDAC() + dac2 = DummyDAC() + + setup = HardwareSetup() + + setup.set_channel('A', PlaybackChannel(awg1, 0)) + setup.set_channel('B', MarkerChannel(awg2, 0)) + setup.set_channel('C', MarkerChannel(awg3, 0)) + + setup.register_dac(dac1) + setup.register_dac(dac2) + + class ProgStart: + def __init__(self): + self.was_started = False + + def __call__(self): + self.was_started = True + program_started = ProgStart() + + setup.register_program('test', block, run_callback=program_started) + + self.assertIsNone(awg1._armed) + self.assertIsNone(awg2._armed) + self.assertIsNone(awg3._armed) + self.assertIsNone(dac1._armed_program) + self.assertIsNone(dac2._armed_program) + + setup.run_program('test') + + self.assertEqual(awg1._armed, 'test') + self.assertEqual(awg2._armed, 'test') + self.assertIsNone(awg3._armed) + self.assertEqual(dac1._armed_program, 'test') + self.assertEqual(dac2._armed_program, 'test') + + self.assertTrue(program_started.was_started) + + def test_register_program_exceptions(self): + setup = HardwareSetup() + wfg = WaveformGenerator(num_channels=2, duration_generator=itertools.repeat(1)) + block = get_two_chan_test_block(wfg) + + with self.assertRaises(TypeError): + setup.register_program('p1', block, 4) + + with self.assertRaises(KeyError): + setup.register_program('p1', block, lambda: None) + + awg = DummyAWG(num_channels=2, num_markers=5) + + setup.set_channel('A', PlaybackChannel(awg, 0, lambda x: x)) + setup.set_channel('B', MarkerChannel(awg, 1)) + + with self.assertRaises(ValueError): + setup.register_program('p1', block, lambda: None) From f5a06d53691521dd71a32b5c984b7724d7b7fe63 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 23 Aug 2017 16:49:17 +0200 Subject: [PATCH 098/116] Extend hardware tests --- qctoolkit/hardware/awgs/tabor.py | 41 +++--- tests/hardware/__init__.py | 12 +- tests/hardware/awg_tests.py | 4 +- tests/hardware/dummy_modules.py | 3 +- tests/hardware/tabor_tests.py | 211 +++++++++++++++++++++++++++---- tests/hardware/util_tests.py | 127 +++++++++++++++---- 6 files changed, 320 insertions(+), 78 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index e9030271a..685953da9 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -158,11 +158,11 @@ def setup_single_sequence_mode(self) -> None: waveform_loop.repetition_count) for waveform_loop in self.program): if waveform in waveforms: - segment_no = waveforms.index(waveform) + 1 + waveform_index = waveforms.index(waveform) else: - segment_no = len(waveforms) + 1 + waveform_index = len(waveforms) waveforms.append(waveform) - sequencer_table.append((repetition_count, segment_no, 0)) + sequencer_table.append((repetition_count, waveform_index, 0)) self._waveforms = waveforms self._sequencer_tables = [sequencer_table] @@ -201,9 +201,8 @@ def check_partial_unroll(program, n): if len(self.program[i]) > max_seq_len: raise TaborException('The algorithm is not smart enough to make sequence tables shorter') elif len(self.program[i]) < min_seq_len: - if self.program[i].repetition_count == 0: - raise TaborException('Invalid repetition count') - elif self.program[i].repetition_count == 1: + assert self.program[i].repetition_count > 0 + if self.program[i].repetition_count == 1: # check if merging with neighbour is possible if i > 0 and check_merge_with_next(self.program, i-1): pass @@ -237,10 +236,8 @@ def check_partial_unroll(program, n): i += 1 for sequence_table in self.program: - if len(sequence_table) < self.__device_properties['min_seq_len']: - raise TaborException('Sequence table is too short') - if len(sequence_table) > self.__device_properties['max_seq_len']: - raise TaborException() + assert len(sequence_table) >= self.__device_properties['min_seq_len'] + assert len(sequence_table) <= self.__device_properties['max_seq_len'] advanced_sequencer_table = [] sequencer_tables = [] @@ -294,7 +291,7 @@ def __init__(self, *args, external_trigger=False, reset=False, **kwargs): self.visa_inst.write(':RES') if external_trigger: - raise NotImplementedError() + raise NotImplementedError() # pragma: no cover def switch_group_off(grp): switch_off_cmd = (":INST:SEL {}; :OUTP OFF; :INST:SEL {}; :OUTP OFF; " @@ -315,8 +312,12 @@ def switch_group_off(grp): self.send_cmd(setup_command) def send_cmd(self, cmd_str, paranoia_level=None) -> Any: - if paranoia_level is not None: - super().send_cmd(cmd_str=cmd_str, paranoia_level=paranoia_level) + """Overwrite send_cmd for paranoia_level > 3""" + if paranoia_level is None: + paranoia_level = self.paranoia_level + + if paranoia_level < 3: + super().send_cmd(cmd_str=cmd_str, paranoia_level=paranoia_level) # pragma: no cover else: cmd_str = cmd_str.rstrip() @@ -325,9 +326,9 @@ def send_cmd(self, cmd_str, paranoia_level=None) -> Any: else: ask_str = '*OPC?; :SYST:ERR?' - answer = self._visa_inst.ask(ask_str).split(';') + *answers, opc, error_code_msg = self._visa_inst.ask(ask_str).split(';') - error_code, error_msg = answer[-1].split(',') + error_code, error_msg = error_code_msg.split(',') error_code = int(error_code) if error_code != 0: _ = self._visa_inst.ask('*CLS; *OPC?') @@ -337,16 +338,12 @@ def send_cmd(self, cmd_str, paranoia_level=None) -> Any: self.send_cmd(cmd_str) else: raise RuntimeError('Cannot execute command: {}\n{}: {}'.format(cmd_str, error_code, error_msg)) - if len(answer) == 2: - return None - elif len(answer) == 3: - return answer[0] - else: - return tuple(answer[:-2]) + + assert len(answers) == 0 @property def is_open(self) -> bool: - return self.visa_inst is not None + return self.visa_inst is not None # pragma: no cover def select_channel(self, channel) -> None: if channel not in (1, 2, 3, 4): diff --git a/tests/hardware/__init__.py b/tests/hardware/__init__.py index 3eacfb940..177682b64 100644 --- a/tests/hardware/__init__.py +++ b/tests/hardware/__init__.py @@ -1,13 +1,11 @@ - -from . import dummy_modules - +from tests.hardware.dummy_modules import import_package use_dummy_tabor = True if use_dummy_tabor: - dummy_modules.import_package('pytabor', dummy_modules.dummy_pytabor) - dummy_modules.import_package('pyvisa', dummy_modules.dummy_pyvisa) - dummy_modules.import_package('teawg', dummy_modules.dummy_teawg) + import_package('pytabor') + import_package('pyvisa') + import_package('teawg') use_dummy_atsaverage = True if use_dummy_atsaverage: - dummy_modules.import_package('atsaverage') + import_package('atsaverage') diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 1365935fa..2f0838b19 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -6,6 +6,8 @@ from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequencing import Sequencer +from tests.hardware.dummy_devices import DummyAWG + class DummyAWGTest(unittest.TestCase): @@ -19,7 +21,7 @@ def setUp(self): self.program = self.sequencer.build() def test_ProgramOverwriteException(self): - dummy = awg.DummyAWG(100) + dummy = DummyAWG(100) dummy.upload('program', self.program, [], [], []) with self.assertRaises(awg.ProgramOverwriteException): dummy.upload('program', self.program, [], [], []) diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 20ed17730..5fac79e7e 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -54,10 +54,11 @@ class dummy_teawg(dummy_package): 'digital_support': False, # is digital-wave supported? } class TEWXAwg: - def __init__(self, *args, **kwargs): + def __init__(self, *args, paranoia_level=1, **kwargs): self.logged_commands = [] self.logged_queries = [] self._visa_inst = dummy_pyvisa.resources.MessageBasedResource() + self.paranoia_level = paranoia_level @property def visa_inst(self): return self._visa_inst diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 125eb0c81..73346740b 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -4,35 +4,38 @@ from copy import copy, deepcopy import numpy as np - from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair,\ - TaborSegment -from qctoolkit.hardware.program import MultiChannelProgram + TaborSegment, TaborSequencing, TaborProgramMemory +from qctoolkit.hardware.program import MultiChannelProgram, Loop from qctoolkit.pulses.instructions import InstructionBlock from qctoolkit.hardware.util import voltage_to_uint16 -from ..hardware import use_dummy_tabor from teawg import model_properties_dict -from .program_tests import LoopTests, WaveformGenerator, MultiChannelTests - -if not use_dummy_tabor: - # fix on your machine - possible_addresses = ('127.0.0.1', ) - for instrument_address in possible_addresses: - instrument = TaborAWGRepresentation(instrument_address, +from tests.pulses.sequencing_dummies import DummyWaveform +from tests.hardware import use_dummy_tabor +from tests.hardware.program_tests import LoopTests, WaveformGenerator, MultiChannelTests + +hardware_instrument = None +def get_instrument(): + if use_dummy_tabor: + instrument = TaborAWGRepresentation('dummy_address', reset=True, paranoia_level=2) + instrument._visa_inst.answers[':OUTP:COUP'] = 'DC' + instrument._visa_inst.answers[':VOLT'] = '1.0' + instrument._visa_inst.answers[':FREQ:RAST'] = '1e9' + instrument._visa_inst.answers[':VOLT:HV'] = '0.7' + return instrument + else: + instrument_address = ('127.0.0.1', ) + if hardware_instrument is None: + hardware_instrument = TaborAWGRepresentation(instrument_address, reset=True, paranoia_level=2) - instrument._visa_inst.timeout = 25000 - if instrument.is_open: - break - if not instrument.is_open: - raise RuntimeError('Could not connect to instrument') -else: - instrument = TaborAWGRepresentation('dummy_address', reset=False, paranoia_level=2) - instrument._visa_inst.answers[':OUTP:COUP'] = 'DC' - instrument._visa_inst.answers[':VOLT'] = '1.0' - instrument._visa_inst.answers[':FREQ:RAST'] = '1e9' + hardware_instrument._visa_inst.timeout = 25000 + + if not hardware_instrument.is_open: + raise RuntimeError('Could not connect to instrument') + return hardware_instrument class TaborSegmentTests(unittest.TestCase): @@ -84,9 +87,125 @@ def test_init(self): with self.assertRaises(TaborException): TaborProgram(prog['A'], self.instr_props, ('A', 'B', 'C'), (None, None)) + def test_depth_0_single_waveform(self): + program = Loop(waveform=DummyWaveform(defined_channels={'A'}), repetition_count=3) - def test_sampled_segments(self): + t_program = TaborProgram(program, channels=(None, 'A'), markers=(None, None), device_properties=self.instr_props) + + self.assertEqual(t_program.waveform_mode, TaborSequencing.SINGLE) + + self.assertEqual(t_program.get_sequencer_tables(), [[(3, 0, 0)]]) + self.assertEqual(t_program.get_advanced_sequencer_table(), [(1, 1, 0)]) + + def test_depth_1_single_waveform(self): + program = Loop(children=[Loop(waveform=DummyWaveform(defined_channels={'A'}), repetition_count=3)], + repetition_count=1) + + t_program = TaborProgram(program, channels=(None, 'A'), markers=(None, None), + device_properties=self.instr_props) + + self.assertEqual(t_program.waveform_mode, TaborSequencing.SINGLE) + + self.assertEqual(t_program.get_sequencer_tables(), [[(3, 0, 0)]]) + self.assertEqual(t_program.get_advanced_sequencer_table(), [(1, 1, 0)]) + + def test_depth_1_single_sequence(self): + program = Loop(children=[Loop(waveform=DummyWaveform(defined_channels={'A'}), repetition_count=3), + Loop(waveform=DummyWaveform(defined_channels={'A'}), repetition_count=4)], + repetition_count=1) + + t_program = TaborProgram(program, channels=(None, 'A'), markers=(None, None), + device_properties=self.instr_props) + + self.assertEqual(t_program.waveform_mode, TaborSequencing.SINGLE) + + self.assertEqual(t_program.get_sequencer_tables(), [[(3, 0, 0), (4, 1, 0)]]) + self.assertEqual(t_program.get_advanced_sequencer_table(), [(1, 1, 0)]) + + def test_depth_1_single_sequence_2(self): + """Use the same wf twice""" + wf_1 = DummyWaveform(defined_channels={'A'}) + wf_2 = DummyWaveform(defined_channels={'A'}) + + program = Loop(children=[Loop(waveform=wf_1, repetition_count=3), + Loop(waveform=wf_2, repetition_count=4), + Loop(waveform=wf_1, repetition_count=1)], + repetition_count=1) + + t_program = TaborProgram(program, channels=(None, 'A'), markers=(None, None), + device_properties=self.instr_props) + + self.assertEqual(t_program.waveform_mode, TaborSequencing.SINGLE) + + self.assertEqual(t_program.get_sequencer_tables(), [[(3, 0, 0), (4, 1, 0), (1, 0, 0)]]) + self.assertEqual(t_program.get_advanced_sequencer_table(), [(1, 1, 0)]) + + def test_depth_1_advanced_sequence_unroll(self): + wf_1 = DummyWaveform(defined_channels={'A'}) + wf_2 = DummyWaveform(defined_channels={'A'}) + + program = Loop(children=[Loop(waveform=wf_1, repetition_count=3), + Loop(waveform=wf_2, repetition_count=4)], + repetition_count=5) + + t_program = TaborProgram(program, channels=(None, 'A'), markers=(None, None), + device_properties=self.instr_props) + + self.assertEqual(t_program.waveform_mode, TaborSequencing.ADVANCED) + + # partial unroll of the last segment + self.assertEqual(t_program.get_sequencer_tables(), [[(3, 0, 0), (3, 1, 0), (1, 1, 0)]]) + self.assertEqual(t_program.get_advanced_sequencer_table(), [(5, 1, 0)]) + + def test_depth_1_advanced_sequence(self): + wf_1 = DummyWaveform(defined_channels={'A'}) + wf_2 = DummyWaveform(defined_channels={'A'}) + + program = Loop(children=[Loop(waveform=wf_1, repetition_count=3), + Loop(waveform=wf_2, repetition_count=4), + Loop(waveform=wf_1, repetition_count=1)], + repetition_count=5) + t_program = TaborProgram(program, channels=(None, 'A'), markers=(None, None), + device_properties=self.instr_props) + + self.assertEqual(t_program.waveform_mode, TaborSequencing.ADVANCED) + + # partial unroll of the last segment + self.assertEqual(t_program.get_sequencer_tables(), [[(3, 0, 0), (4, 1, 0), (1, 0, 0)]]) + self.assertEqual(t_program.get_advanced_sequencer_table(), [(5, 1, 0)]) + + def test_advanced_sequence_exceptions(self): + temp_properties = self.instr_props.copy() + temp_properties['max_seq_len'] = 5 + + program = Loop(children=[Loop(waveform=DummyWaveform(defined_channels={'A'}), repetition_count=1) + for _ in range(temp_properties['max_seq_len']+1)], + repetition_count=2) + with self.assertRaises(TaborException): + TaborProgram(program.copy_tree_structure(), channels=(None, 'A'), markers=(None, None), + device_properties=temp_properties) + + temp_properties['min_seq_len'] = 100 + temp_properties['max_seq_len'] = 120 + with self.assertRaises(TaborException) as exception: + TaborProgram(program.copy_tree_structure(), channels=(None, 'A'), markers=(None, None), + device_properties=temp_properties) + self.assertEqual(str(exception.exception), 'The algorithm is not smart enough ' + 'to make this sequence table longer') + + program = Loop(children=[Loop(children=[Loop(waveform=DummyWaveform(defined_channels={'A'})), + Loop(waveform=DummyWaveform(defined_channels={'A'}))]), + Loop(children=[Loop(waveform=DummyWaveform(defined_channels={'A'})), + Loop(waveform=DummyWaveform(defined_channels={'A'}))]) + ]) + with self.assertRaises(TaborException) as exception: + TaborProgram(program.copy_tree_structure(), channels=(None, 'A'), markers=(None, None), + device_properties=temp_properties) + self.assertEqual(str(exception.exception), 'The algorithm is not smart enough ' + 'to make this sequence table longer') + + def test_sampled_segments(self): def my_gen(gen): alternating_on_off = itertools.cycle((np.ones(192), np.zeros(192))) chan_gen = gen @@ -150,22 +269,66 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def test_sample_rate(self): + instrument = get_instrument() + for ch in (1, 2, 3, 4): self.assertIsInstance(instrument.sample_rate(ch), numbers.Number) with self.assertRaises(TaborException): instrument.sample_rate(0) def test_amplitude(self): + with self.assertRaises(TaborException): + get_instrument().amplitude(6) + for ch in range(1, 5): - self.assertIsInstance(instrument.amplitude(ch), float) + self.assertIsInstance(get_instrument().amplitude(ch), float) + + def test_select_channel(self): + with self.assertRaises(TaborException): + get_instrument().select_channel(6) + + +@unittest.skipIf(not use_dummy_tabor, "Tests only possible with dummy tabor driver module injection") +class TaborAWGRepresentationDummyBasedTests(unittest.TestCase): + def test_send_cmd(self): + inst = get_instrument() + + inst.send_cmd('', paranoia_level=3) + self.assertEqual(inst.visa_inst.logged_asks[-1], (('*OPC?; :SYST:ERR?',), {})) + + inst.visa_inst.answers['enemene'] = '1;2;3;4' + + inst.paranoia_level = 3 + with self.assertRaises(AssertionError): + inst.send_cmd('enemene?') + + inst.visa_inst.default_answer = '-451, bla' + with self.assertRaises(RuntimeError): + inst.send_cmd('') + + def test_trigger(self): + inst = get_instrument() + inst.paranoia_level = 0 + + inst.logged_commands = [] + inst.trigger() + + self.assertEqual(inst.logged_commands, [((), dict(cmd_str=':TRIG', paranoia_level=inst.paranoia_level))]) + + class TaborChannelPairTests(unittest.TestCase): def test_copy(self): - channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + channel_pair = TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 2)) with self.assertRaises(NotImplementedError): copy(channel_pair) with self.assertRaises(NotImplementedError): deepcopy(channel_pair) + def test_init(self): + with self.assertRaises(ValueError): + TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 3)) + + diff --git a/tests/hardware/util_tests.py b/tests/hardware/util_tests.py index 75d4f3ead..f8b7f33f4 100644 --- a/tests/hardware/util_tests.py +++ b/tests/hardware/util_tests.py @@ -1,14 +1,15 @@ import unittest import itertools +import pytabor + import numpy as np from qctoolkit.hardware.awgs.tabor import TaborSegment from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave -from . import dummy_modules -import pytabor +from . import dummy_modules class VoltageToBinaryTests(unittest.TestCase): @@ -30,33 +31,55 @@ def test_voltage_to_uint16(self): self.assertTrue(np.all(expected_data == received_data)) -@unittest.skipIf(pytabor is dummy_modules.dummy_pytabor, "Cannot compare to pytabor results") +def validate_result(tabor_segments, result, fill_value=None): + pos = 0 + for i, tabor_segment in enumerate(tabor_segments): + if i > 0: + if tabor_segment[1] is None: + if fill_value: + np.testing.assert_equal(result[pos:pos + 16], np.full(16, fill_value=fill_value, dtype=np.uint16)) + else: + np.testing.assert_equal(result[pos:pos + 16], np.full(16, tabor_segment[1][0], dtype=np.uint16)) + pos += 16 + + if tabor_segment[0] is None: + if fill_value: + np.testing.assert_equal(result[pos:pos + 16], np.full(16, fill_value=fill_value, dtype=np.uint16)) + else: + np.testing.assert_equal(result[pos:pos + 16], np.full(16, tabor_segment[0][0], dtype=np.uint16)) + pos += 16 + + for j in range(tabor_segment.num_points // 16): + if tabor_segment[1] is None: + if fill_value: + np.testing.assert_equal(result[pos:pos + 16], np.full(16, fill_value=fill_value, dtype=np.uint16)) + else: + np.testing.assert_equal(result[pos:pos + 16], tabor_segment[1][j * 16: (j + 1) * 16]) + pos += 16 + + if tabor_segment[0] is None: + if fill_value: + np.testing.assert_equal(result[pos:pos + 16], np.full(16, fill_value=fill_value, dtype=np.uint16)) + else: + np.testing.assert_equal(result[pos:pos + 16], tabor_segment[0][j * 16: (j + 1) * 16]) + pos += 16 + + class TaborMakeCombinedTest(unittest.TestCase): - def exec_general(self, data_1, data_2): + def exec_general(self, data_1, data_2, fill_value=None): tabor_segments = [TaborSegment(d1, d2) for d1, d2 in zip(data_1, data_2)] expected_length = (sum(segment.num_points for segment in tabor_segments) + 16 * (len(tabor_segments) - 1)) * 2 - offset = 0 - pyte_result = 15000*np.ones(expected_length, dtype=np.uint16) - for i, segment in enumerate(tabor_segments): - offset = pytabor.make_combined_wave(segment[0], segment[1], - dest_array=pyte_result, dest_array_offset=offset, - add_idle_pts=i > 0) - self.assertEqual(expected_length, offset) - - result = make_combined_wave(tabor_segments, fill_value=15000) - np.testing.assert_equal(pyte_result, result) - - dest_array = 15000*np.ones(expected_length, dtype=np.uint16) - result = make_combined_wave(tabor_segments, destination_array=dest_array) - np.testing.assert_equal(pyte_result, result) - # test that the destination array data is not copied - self.assertEqual(dest_array.__array_interface__['data'], - result.__array_interface__['data']) + result = make_combined_wave(tabor_segments, fill_value=fill_value) + self.assertEqual(len(result), expected_length) - with self.assertRaises(ValueError): - make_combined_wave(tabor_segments, destination_array=np.ones(16)) + validate_result(tabor_segments, result, fill_value=fill_value) + destination_array = np.empty(expected_length, dtype=np.uint16) + result = make_combined_wave(tabor_segments, fill_value=fill_value, destination_array=destination_array) + validate_result(tabor_segments, result, fill_value=fill_value) + self.assertEqual(destination_array.__array_interface__['data'], result.__array_interface__['data'], + 'Data was copied') def test_make_comb_both(self): gen = itertools.count() @@ -67,8 +90,11 @@ def test_make_comb_both(self): data_2 = [np.fromiter(gen, count=32, dtype=np.uint16), np.fromiter(gen, count=16, dtype=np.uint16), np.fromiter(gen, count=192, dtype=np.uint16)] + for d in data_2: + d += 1000 self.exec_general(data_1, data_2) + self.exec_general(data_1, data_2, 2000) def test_make_single_chan(self): gen = itertools.count() @@ -80,3 +106,58 @@ def test_make_single_chan(self): self.exec_general(data_1, data_2) self.exec_general(data_2, data_1) + self.exec_general(data_1, data_2, 2000) + self.exec_general(data_2, data_1, 2000) + + def test_empty_segment_list(self): + combined = make_combined_wave([]) + + self.assertIsInstance(combined, np.ndarray) + self.assertIs(combined.dtype, np.dtype('uint16')) + self.assertEqual(len(combined), 0) + + def test_invalid_segment_length(self): + gen = itertools.count() + data_1 = [np.fromiter(gen, count=32, dtype=np.uint16), + np.fromiter(gen, count=15, dtype=np.uint16), + np.fromiter(gen, count=192, dtype=np.uint16)] + + data_2 = [np.fromiter(gen, count=32, dtype=np.uint16), + np.fromiter(gen, count=16, dtype=np.uint16), + np.fromiter(gen, count=193, dtype=np.uint16)] + + tabor_segments = [TaborSegment(d, d) for d in data_1] + with self.assertRaises(ValueError): + make_combined_wave(tabor_segments) + + tabor_segments = [TaborSegment(d, d) for d in data_2] + with self.assertRaises(ValueError): + make_combined_wave(tabor_segments) + + +@unittest.skipIf(pytabor is dummy_modules.dummy_pytabor, "Cannot compare to pytabor results") +class TaborMakeCombinedPyTaborCompareTest(TaborMakeCombinedTest): + def exec_general(self, data_1, data_2, fill_value=None): + tabor_segments = [TaborSegment(d1, d2) for d1, d2 in zip(data_1, data_2)] + expected_length = (sum(segment.num_points for segment in tabor_segments) + 16 * (len(tabor_segments) - 1)) * 2 + + offset = 0 + pyte_result = 15000*np.ones(expected_length, dtype=np.uint16) + for i, segment in enumerate(tabor_segments): + offset = pytabor.make_combined_wave(segment[0], segment[1], + dest_array=pyte_result, dest_array_offset=offset, + add_idle_pts=i > 0) + self.assertEqual(expected_length, offset) + + result = make_combined_wave(tabor_segments, fill_value=15000) + np.testing.assert_equal(pyte_result, result) + + dest_array = 15000*np.ones(expected_length, dtype=np.uint16) + result = make_combined_wave(tabor_segments, destination_array=dest_array) + np.testing.assert_equal(pyte_result, result) + # test that the destination array data is not copied + self.assertEqual(dest_array.__array_interface__['data'], + result.__array_interface__['data']) + + with self.assertRaises(ValueError): + make_combined_wave(tabor_segments, destination_array=np.ones(16)) \ No newline at end of file From b985f9797dc0f09b6077b5788aef704f7f90a1db Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 23 Aug 2017 17:38:03 +0200 Subject: [PATCH 099/116] Add missing alazar tests --- tests/hardware/alazar_tests.py | 67 ++++++++++++++++++++++++++++++++- tests/hardware/dummy_modules.py | 9 ++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/tests/hardware/alazar_tests.py b/tests/hardware/alazar_tests.py index 946eddf28..ea2720607 100644 --- a/tests/hardware/alazar_tests.py +++ b/tests/hardware/alazar_tests.py @@ -8,7 +8,18 @@ import numpy as np -from qctoolkit.hardware.dacs.alazar import AlazarCard +from qctoolkit.hardware.dacs.alazar import AlazarCard, AlazarProgram + +from tests.hardware import use_dummy_atsaverage + + +class AlazarProgramTest(unittest.TestCase): + def test_iter(self): + args = ([], [], 0) + + program = AlazarProgram(*args) + for x, y in zip(program, args): + self.assertIs(x, y) class AlazarTest(unittest.TestCase): @@ -45,8 +56,11 @@ def test_make_mask(self): self.assertEqual(mask.channel, 3) def test_register_measurement_windows(self): + raw_card = dummy_modules.dummy_atsaverage.core.AlazarCard() + card = AlazarCard(raw_card) + + self.assertIs(card.card, raw_card) - card = AlazarCard(dummy_modules.dummy_atsaverage.core.AlazarCard()) card.register_mask_for_channel('A', 3, 'auto') card.register_mask_for_channel('B', 1, 'auto') @@ -71,3 +85,52 @@ def test_register_measurement_windows(self): self.assertEqual(card._registered_programs['otto'].masks[0].channel, 3) self.assertEqual(card._registered_programs['otto'].masks[0].identifier, 'A') + def test_register_operations(self): + card = AlazarCard(None) + + operations = 'this is no operatoin but a string' + card.register_operations('test', operations) + self.assertEqual(len(card._registered_programs), 1) + self.assertIs(card._registered_programs['test'].operations, operations) + + def test_mask_prototypes(self): + card = AlazarCard(None) + self.assertIs(card.mask_prototypes, card._mask_prototypes) + + def test_arm_operation(self): + raw_card = dummy_modules.dummy_atsaverage.core.AlazarCard() + card = AlazarCard(raw_card) + + card.register_mask_for_channel('A', 3, 'auto') + card.register_mask_for_channel('B', 1, 'auto') + + card.register_operations('otto', []) + + card.config = dummy_modules.dummy_atsaverage.config.ScanlineConfiguration() + + with self.assertRaises(RuntimeError): + card.arm_program('otto') + + card.register_operations('otto', ['asd']) + + with self.assertRaises(RuntimeError): + card.arm_program('otto') + + begins = np.arange(100) * 176.5 + lengths = np.ones(100) * 10 * np.pi + card.register_measurement_windows('otto', dict(A=(begins, lengths))) + + card.config.totalRecordSize = 17 + + with self.assertRaises(ValueError): + card.arm_program('otto') + + card.config.totalRecordSize = 0 + card.arm_program('otto') + + self.assertEqual(card.config._apply_calls, [(raw_card, True)]) + self.assertEqual(card.card._startAcquisition_calls, [1]) + + card.arm_program('otto') + self.assertEqual(card.config._apply_calls, [(raw_card, True)]) + self.assertEqual(card.card._startAcquisition_calls, [1, 1]) diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 5fac79e7e..99143b589 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -78,12 +78,19 @@ class core(dummy_package): class AlazarCard: model = 'DUMMY' minimum_record_size = 256 + def __init__(self): + self._startAcquisition_calls = [] + def startAcquisition(self, x: int): + self._startAcquisition_calls.append(x) class config(dummy_package): class CaptureClockConfig: def numeric_sample_rate(self, card): return 10**8 class ScanlineConfiguration: - pass + def __init__(self): + self._apply_calls = [] + def apply(self, card, print_debug_output): + self._apply_calls.append((card, print_debug_output)) ScanlineConfiguration.captureClockConfiguration = CaptureClockConfig() class operations(dummy_package): class OperationDefinition: From bf620bbc8c87585590b0d3c832b629b407cfb273 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 23 Aug 2017 18:08:08 +0200 Subject: [PATCH 100/116] Move tektronix tests to different file --- tests/hardware/awg_tests.py | 52 ------------------------------- tests/hardware/tektronix_tests.py | 29 +++++++++++++++++ 2 files changed, 29 insertions(+), 52 deletions(-) create mode 100644 tests/hardware/tektronix_tests.py diff --git a/tests/hardware/awg_tests.py b/tests/hardware/awg_tests.py index 2f0838b19..e69de29bb 100644 --- a/tests/hardware/awg_tests.py +++ b/tests/hardware/awg_tests.py @@ -1,52 +0,0 @@ -import unittest -import numpy as np - -import qctoolkit.hardware.awgs.base as awg -import qctoolkit.hardware.awgs.tektronix as tek -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate -from qctoolkit.pulses.sequencing import Sequencer - -from tests.hardware.dummy_devices import DummyAWG - - -class DummyAWGTest(unittest.TestCase): - - def setUp(self): - self.pulse_template = TablePulseTemplate({'default': [('value', 5)]}) - - self.sequencer = Sequencer() - for i in range(1, 12): - pars = dict(value=i) - self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) - self.program = self.sequencer.build() - - def test_ProgramOverwriteException(self): - dummy = DummyAWG(100) - dummy.upload('program', self.program, [], [], []) - with self.assertRaises(awg.ProgramOverwriteException): - dummy.upload('program', self.program, [], [], []) - - -class TektronixAWGTest(unittest.TestCase): - - def setUp(self): - self.pulse_template = TablePulseTemplate({'default': [('value', 5)]}) - self.sequencer = Sequencer() - for i in range(1,12): - pars = dict(value=i) - self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) - self.program = self.sequencer.build() - - @unittest.skip - def test_ProgramOverwriteException(self): - dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) - dummy.upload('program', self.program) - with self.assertRaises(awg.ProgramOverwriteException): - dummy.upload('program', self.program) - - @unittest.skip - def test_upload(self): - dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) - dummy.upload('program', self.program) - programs = dummy.programs - self.assertEqual(programs, ['program']) diff --git a/tests/hardware/tektronix_tests.py b/tests/hardware/tektronix_tests.py new file mode 100644 index 000000000..9f0f84d3e --- /dev/null +++ b/tests/hardware/tektronix_tests.py @@ -0,0 +1,29 @@ +import unittest + +from qctoolkit.pulses.table_pulse_template import TablePulseTemplate +from qctoolkit.pulses.sequencing import Sequencer +import qctoolkit.hardware.awgs.tektronix as tek + + +class TektronixAWGTest(unittest.TestCase): + def setUp(self): + self.pulse_template = TablePulseTemplate({'default': [('value', 5)]}) + self.sequencer = Sequencer() + for i in range(1,12): + pars = dict(value=i) + self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) + self.program = self.sequencer.build() + + @unittest.skip + def test_ProgramOverwriteException(self): + dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) + dummy.upload('program', self.program) + with self.assertRaises(awg.ProgramOverwriteException): + dummy.upload('program', self.program) + + @unittest.skip + def test_upload(self): + dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) + dummy.upload('program', self.program) + programs = dummy.programs + self.assertEqual(programs, ['program']) From 87fd09fb9df38dcb9853ebf919a1ffbc97bf0b95 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 23 Aug 2017 18:09:38 +0200 Subject: [PATCH 101/116] Remove Tektronix AWG support for now --- qctoolkit/hardware/awgs/__init__.py | 6 - qctoolkit/hardware/awgs/tektronix.py | 279 --------------------------- tests/hardware/tektronix_tests.py | 29 --- 3 files changed, 314 deletions(-) delete mode 100644 qctoolkit/hardware/awgs/tektronix.py delete mode 100644 tests/hardware/tektronix_tests.py diff --git a/qctoolkit/hardware/awgs/__init__.py b/qctoolkit/hardware/awgs/__init__.py index 90da30620..e292365e2 100644 --- a/qctoolkit/hardware/awgs/__init__.py +++ b/qctoolkit/hardware/awgs/__init__.py @@ -5,9 +5,3 @@ __all__.extend(["TaborAWGRepresentation", "TaborChannelPair"]) except ImportError: pass - -try: - from qctoolkit.hardware.awgs.tektronix import TektronixAWG - __all__.extend("TektronixAWG") -except ImportError: - pass diff --git a/qctoolkit/hardware/awgs/tektronix.py b/qctoolkit/hardware/awgs/tektronix.py deleted file mode 100644 index c1900f872..000000000 --- a/qctoolkit/hardware/awgs/tektronix.py +++ /dev/null @@ -1,279 +0,0 @@ -from functools import reduce -import socket -import select -from itertools import chain, repeat -from typing import Iterable, Any, Set, Tuple - -import numpy as np -import warnings - -try: - import visa - hasVisa = True -except ImportError: - warnings.warn("pyVISA not available, Tektronix AWGs only available in simulation mode!") - hasVisa = False - -from qctoolkit.hardware.awgs.base import AWG, ProgramOverwriteException, Program -from qctoolkit.pulses.instructions import EXECInstruction, Waveform - - -__all__ = ['TektronixAWG', 'AWGSocket', 'EchoTestServer'] - - -class EchoTestServer(): - - def __init__(self, port: int) -> None: - self.port = port - - def run(self) -> None: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind(('', self.port)) - s.listen(5) - while True: - client, address = s.accept() - data = client.recv(65535) - if data: - client.send(data) - client.close() - - -def grouper(n, iterable, padvalue=None) -> Iterable[Any]: - return zip(*[chain(iterable, repeat(padvalue, n-1))]*n) - - -class AWGSocket(): - - def __init__(self, ip: str, port: int, buffersize: int=1024, timeout: float=5) -> None: - self.__ip = ip - self.__port = port - self.__buffersize = buffersize - self.__timeout = timeout - - def _cleanstring(self, bytestring: bytes) -> bytes: - # accept strings as well, but encode them to bytes - if not isinstance(bytestring, bytes): - bytestring = bytestring.encode() - # make sure the message is delimited by b'\n' - if not bytestring.endswith(b'\n'): - bytestring = bytestring + b'\n' - return bytestring - - def send(self, bytestring: bytes) -> None: - """Sends a command to the AWG with no answer.""" - bytestring = self._cleanstring(bytestring) - s = socket.create_connection((self.__ip, self.__port)) - for chunk in grouper(self.__buffersize, bytestring): - s.send(bytestring) - s.close() - - def query(self, bytestring: bytes) -> None: - """Queries the AWG and returns its answer.""" - bytestring = self._cleanstring(bytestring) - if not bytestring.endswith(b'?\n'): - raise ValueError("Invalid query, does not end with '?'.") - # create socket and make query - s = socket.create_connection((self.__ip, self.__port)) - s.send(bytestring) - # receive answer, terminated by '\n' - chunks = [] - bytes_recvd = 0 - while True: - ready_to_read, _, _ = select.select([s],[],[], self.__timeout) - if ready_to_read: - s = ready_to_read[0] - data = s.recv(self.__buffersize) - if not data: - break # opposite side closed the connection (probably) - if b'\n' in data: # opposite side has sent message delimiter - chunks.append(data) - break - else: - chunks.append(data) - s.close() - answer = b''.join(chunks) - return answer - -class TektronixAWGSimulator(): - def query(self, *args) -> str: - string = ' '.join(map(str, args)) - if string.startswith('*IDN='): - return "Tektronix Simulator" - elif string.startswith('SEQUENCE:LENGTH?'): - return 0 - else: - return "QUERY: %s" % string - - def write(self, string, *args, **kwargs) -> None: - pass - - def write_binary_values(self, string, *args, **kwargs) -> None: - pass - - -class TektronixAWG(AWG): - - def __init__(self, ip: str, port: int, sample_rate: float, first_index=None, simulation=False) -> None: - self.__programs = {} # holds names and programs - self.__program_indices = {} # holds programs and their first index (for jumping to it) - self.__waveform_memory = set() #map sequence indices to waveforms and vice versa - self.__program_waveforms = {} # maps program names to set of waveforms used by the program - self.__ip = ip - self.__port = port - self.__simulation = simulation - if simulation: - warnings.warn("Using Tektronix AWG driver in simulation mode!") - self.inst = TektronixAWGSimulator() - else: - self.__rm = visa.ResourceManager() - self.inst = self.__rm.open_resource('TCPIP::{0}::INSTR'.format(self.__ip, self.__port)) - self.__identifier = self.inst.query('*IDN?\n') - self.inst.write('AWGCONTROL:RMODE SEQUENCE') # play sequence - self.__sample_rate = sample_rate - self.__scale = 2. - self.__offset = 1. - self.__channel_template = '{0:s}_{1:d}' - self.__channels = [1] # use 2 channels. TODO: fix this - if not first_index: - # query how long the sequence already is - sequence_length_before = int(self.inst.query('SEQUENCE:LENGTH?')) - # add 400 (?, arbitrary) more slots - self.inst.query('SEQUENCE:LENGTH', sequence_length_before + 400) # magic number - self.__first_index = sequence_length_before - else: - self.__first_index = first_index - self.__current_index = self.__first_index + 1 - # TODO: load dummy pulse to first_index - - @property - def num_channels(self): - raise NotImplementedError() - - @property - def num_markers(self): - raise NotImplementedError() - - @property - def arm(self, name: str): - raise NotImplementedError() - - @property - def output_range(self) -> Tuple[float, float]: - return (-1, 1) - - @property - def identifier(self) -> str: - return self.__identifier - - @property - def programs(self) -> Set[str]: - return list(self.__programs.keys()) - - @property - def sample_rate(self) -> float: - return self.__sample_rate - - def rescale(self, voltages: np.ndarray) -> np.ndarray: - """Converts an array of voltages to an array of unsigned integers for upload.""" - data = (voltages + self.__offset) / self.__scale + 1 - # scale data to uint14 range - data = (data * (2**13 - 1)) - data[data > 2**14 - 1] = 2**14 - 1 - data = data.astype(np.uint16) - # data = data + marker.astype(np.uint16) * 2**14 - return data - - def waveform2name(self, waveform: Waveform) -> str: - return str(hash(waveform)) - - def add_waveform(self, waveform: Waveform, offset: float) -> None: - """Samples a Waveform object to actual data and sends it to the AWG.""" - # check if waveform is on the AWG already - if waveform in self.__waveform_memory: - pass - else: - # first sample the waveform to get an array of data - ts = np.arange(waveform.duration * self.__sample_rate) * 1/self.sample_rate - data = waveform.sample(ts, offset) - wf_name = self.waveform2name(waveform) - - # now create a new waveform on the awg - total = len(data) - for ic, c in enumerate(self.__channels): - name = self.__channel_template.format(wf_name, c) - self.inst.write('WLIST:WAVEFORM:NEW "{0}", {1:d}, INT'.format(name, total)) - chunksize = 65536 - data = self.rescale(data[:,ic]) - n_chunks = np.ceil(total/chunksize) - for i, chunk in enumerate(np.array_split(data, n_chunks)): - header ='#{0:d}{1:d}'.format(len(str(len(chunk))), len(chunk)) - self.inst.write_binary_values('WLIST:WAVEFORM:DATA "{0:s}", {1:d}, {2:d}, '.format(name, i*chunksize, len(chunk)), chunk, datatype='H') - - self.__waveform_memory.add(waveform) - - def build_sequence(self, name: str) -> None: - '''Puts a new sequence in the AWG sequence memory''' - length = len(self.__programs[name]) - 1 # - 1 for the stop instruction in the end - program = self.__programs[name] - for i in range(length): - waveform = program[i] - wf_name = self.waveform2name(waveform) - # set i'th sequence element - # TODO: for multiple channels write the following line for each channel, respectively - for c in self.__channels: - name = self.__channel_template.format(wf_name, c) - self.inst.write('SEQUENCE:ELEMENT{0:d}:WAVEFORM{1:d} "{2:s}"'.format(self.__current_index + i + 1, c, name)) - # have sequence go back to index 1 after playback of index N - self.inst.write('SEQUENCE:ELEMENT{0:d}:GOTO:STATE ON'.format(length)) - - - def upload(self, name: str, program: Program, force: bool=False) -> None: - '''Uploads all necessary waveforms for the program to the AWG and create a corresponding sequence.''' - if name in self.programs: - if not force: - raise ProgramOverwriteException(name) - else: - self.remove(name) - self.upload(name, program) - else: - self.__programs[name] = program - exec_blocks = list(filter(lambda x: type(x) == EXECInstruction, program)) - offset = 0 - for block in exec_blocks: - self.add_waveform(block.waveform, offset) - offset += block.waveform.duration - used_waveforms = frozenset([block.waveform for block in exec_blocks]) - self.__program_waveforms[name] = used_waveforms - self.__program_indices[name] = self.__current_index - self.__current_index = self.__current_index + len(program) - - self.build_sequence(name) - - def run(self, name: str, autorun: bool=False) -> None: - # there is only one sequence per devicee, so program the sequence first - self.inst.write('SEQUENCE:ELEMENT1:GOTO:STATE ON') - self.inst.write('SEQUENCE:ELEMENT1:GOTO:INDEX', self.__program_indices[name]) - self.inst.write('AWGCONTROL:RUN') - - def remove(self, name: str) -> None: - if name in self.programs: - self.__programs.pop(name) - self.__program_waveforms.pop(name) - self.clean() - - def clean(self) -> int: - necessary_wfs = reduce(lambda acc, s: acc.union(s), self.__program_waveforms.values(), set()) - all_wfs = self.__waveform_memory - delete = all_wfs - necessary_wfs - for waveform in delete: - wf_name = self.waveform2name(waveform) - for c in self.__channels: - name = self.__channel_template.format(wf_name, c) - self.inst.write('WLIST:WAVEFORM:DELETE "{0:s}"'.format(name)) - self.__waveform_memory.remove(waveform) - print('Deleted {0:d} waveforms'.format(len(delete))) - - # reset sequence - self.inst.write('SEQUENCE:LENGTH', self.__first_index) - self.inst.write('SEQUENCE:LENGTH', self.__first_index + 400) - return len(delete) diff --git a/tests/hardware/tektronix_tests.py b/tests/hardware/tektronix_tests.py deleted file mode 100644 index 9f0f84d3e..000000000 --- a/tests/hardware/tektronix_tests.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest - -from qctoolkit.pulses.table_pulse_template import TablePulseTemplate -from qctoolkit.pulses.sequencing import Sequencer -import qctoolkit.hardware.awgs.tektronix as tek - - -class TektronixAWGTest(unittest.TestCase): - def setUp(self): - self.pulse_template = TablePulseTemplate({'default': [('value', 5)]}) - self.sequencer = Sequencer() - for i in range(1,12): - pars = dict(value=i) - self.sequencer.push(self.pulse_template, pars, channel_mapping=dict(default='default')) - self.program = self.sequencer.build() - - @unittest.skip - def test_ProgramOverwriteException(self): - dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) - dummy.upload('program', self.program) - with self.assertRaises(awg.ProgramOverwriteException): - dummy.upload('program', self.program) - - @unittest.skip - def test_upload(self): - dummy = tek.TektronixAWG('127.0.0.1', 8000, 100000, simulation=True) - dummy.upload('program', self.program) - programs = dummy.programs - self.assertEqual(programs, ['program']) From c57d7496df7a6c8e2e61746cc7e1d6e7b7b73a48 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 24 Aug 2017 19:10:59 +0200 Subject: [PATCH 102/116] Extend tabor tests significantly --- qctoolkit/hardware/awgs/tabor.py | 190 ++++++---- qctoolkit/hardware/util.py | 17 +- tests/hardware/dummy_modules.py | 16 +- tests/hardware/tabor_tests.py | 571 ++++++++++++++++++++++++++++++- tests/hardware/util_tests.py | 14 +- 5 files changed, 733 insertions(+), 75 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 685953da9..b1afdf9f2 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -1,7 +1,7 @@ """""" import fractions import sys -from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any, Sequence +from typing import List, Tuple, Set, NamedTuple, Callable, Optional, Any, Sequence, cast from enum import Enum # Provided by Tabor electronics for python 2.7 @@ -13,7 +13,7 @@ from qctoolkit import ChannelID from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from qctoolkit.hardware.program import Loop -from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave +from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave, find_positions from qctoolkit.hardware.awgs.base import AWG @@ -402,11 +402,12 @@ def guarding_method(channel_pair: 'TaborChannelPair', *args, **kwargs) -> Any: channel_pair._enter_config_mode() channel_pair._configuration_guard_count += 1 - function_object(channel_pair, *args, **kwargs) - - channel_pair._configuration_guard_count -= 1 - if channel_pair._configuration_guard_count == 0: - channel_pair._exit_config_mode() + try: + return function_object(channel_pair, *args, **kwargs) + finally: + channel_pair._configuration_guard_count -= 1 + if channel_pair._configuration_guard_count == 0: + channel_pair._exit_config_mode() return guarding_method @@ -478,7 +479,7 @@ def _free_points_in_total(self) -> int: @property def _free_points_at_end(self) -> int: reserved_index = np.flatnonzero(self._segment_reserved) - if reserved_index: + if len(reserved_index): return self.total_capacity - np.sum(self._segment_capacity[:reserved_index[-1]]) else: return self.total_capacity @@ -486,9 +487,9 @@ def _free_points_at_end(self) -> int: @with_configuration_guard def upload(self, name: str, program: Loop, - channels: List[ChannelID], - markers: List[ChannelID], - voltage_transformation: List[Callable], + channels: Tuple[Optional[ChannelID], Optional[ChannelID]], + markers: Tuple[Optional[ChannelID], Optional[ChannelID]], + voltage_transformation: Tuple[Callable, Callable], force: bool=False) -> None: """Upload a program to the AWG. @@ -519,67 +520,116 @@ def upload(self, name: str, sample_rate = self._device.sample_rate(self._channels[0]) voltage_amplitudes = (self._device.amplitude(self._channels[0]), self._device.amplitude(self._channels[1])) - voltage_offsets = (self._device.offset(self._channels[0]), - self._device.offset(self._channels[1])) + voltage_offsets = (0, 0) segments, segment_lengths = tabor_program.sampled_segments(sample_rate=sample_rate, voltage_amplitude=voltage_amplitudes, voltage_offset=voltage_offsets, voltage_transformation=voltage_transformation) - segment_hashes = np.fromiter((hash(segment) for segment in segments), count=len(segments), dtype=np.uint64) - - known_waveforms = np.in1d(segment_hashes, self._segment_hashes, assume_unique=True) - to_upload_size = np.sum(segment_lengths[~known_waveforms] + 16) - - waveform_to_segment = np.full(len(segments), -1, dtype=int) - waveform_to_segment[known_waveforms] = np.flatnonzero( - np.in1d(self._segment_hashes, segment_hashes[known_waveforms])) - - to_amend = ~known_waveforms - to_insert = [] - - if name not in self._known_programs: - if self._free_points_in_total < to_upload_size: - raise MemoryError('Not enough free memory') - if self._free_points_at_end < to_upload_size: - reserved_indices = np.flatnonzero(self._segment_reserved) - if len(reserved_indices) == 0: - raise MemoryError('Fragmentation does not allow upload.') - - last_reserved = reserved_indices[-1] if reserved_indices else 0 - free_segments = np.flatnonzero(self._segment_references[:last_reserved] == 0)[ - np.argsort(self._segment_capacity[:last_reserved])[::-1]] - - for wf_index in np.argsort(segment_lengths[~known_waveforms])[::-1]: - if segment_lengths[wf_index] <= self._segment_capacity[free_segments[0]]: - to_insert.append((wf_index, free_segments[0])) - free_segments = free_segments[1:] - to_amend[wf_index] = False - - if np.sum(segment_lengths[to_amend] + 16) > self._free_points_at_end: - raise MemoryError('Fragmentation does not allow upload.') + waveform_to_segment, to_amend, to_insert = self._find_place_for_segments_in_memory(segments, + segment_lengths) except: if to_restore: - self._known_programs[name] = to_restore - self._segment_reserved[to_restore.segment_indices] += 1 + self.upload(name, to_restore) raise self._segment_references[waveform_to_segment[waveform_to_segment >= 0]] += 1 - if to_insert: - for wf_index, segment_index in to_insert: - self._upload_segment(segment_index, segments[wf_index]) - waveform_to_segment[wf_index] = segment_index + for wf_index in np.flatnonzero(to_insert > 0): + segment_index = to_insert[wf_index] + self._upload_segment(to_insert[wf_index], segments[wf_index]) + waveform_to_segment[wf_index] = segment_index if np.any(to_amend): segments_to_amend = segments[to_amend] self._amend_segments(segments_to_amend) - waveform_to_segment[to_amend] = np.arange(len(self._segment_capacity) - np.sum(to_amend), - len(self._segment_capacity), dtype=int) + 1 + waveform_to_segment[to_amend] = self._amend_segments(segments_to_amend) self._known_programs[name] = TaborProgramMemory(segment_indices=waveform_to_segment, program=tabor_program) + def _find_place_for_segments_in_memory(self, segments: Sequence, segment_lengths: Sequence) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: + """ + 1. Find known segments + 2. Find empty spaces with fitting length + 3. Find empty spaces with bigger length + 4. Amend remaining segments + :param segments: + :param segment_lengths: + :return: + """ + segment_hashes = np.fromiter((hash(segment) for segment in segments), count=len(segments), dtype=np.int64) + + waveform_to_segment = find_positions(self._segment_hashes, segment_hashes) + + # separate into known and unknown + unknown = (waveform_to_segment == -1) + known = ~unknown + + known_pos_in_memory = waveform_to_segment[known] + + assert len(known_pos_in_memory) == 0 or np.all(self._segment_hashes[known_pos_in_memory] == segment_hashes[known]) + + new_reference_counter = self._segment_references.copy() + new_reference_counter[known_pos_in_memory] += 1 + + to_upload_size = np.sum(segment_lengths[unknown] + 16) + free_points_in_total = self.total_capacity - np.sum(self._segment_capacity[self._segment_references > 0]) + if free_points_in_total < to_upload_size: + raise MemoryError('Not enough free memory', + free_points_in_total, + to_upload_size, + self._free_points_in_total) + + to_amend = cast(np.ndarray, unknown) + to_insert = np.full(len(segments), fill_value=-1, dtype=np.int64) + + reserved_indices = np.flatnonzero(new_reference_counter > 0) + last_reserved = reserved_indices[-1] if len(reserved_indices) else 0 + + free_segments = new_reference_counter[:last_reserved] == 0 + free_segment_count = np.sum(free_segments) + + # look for a free segment place with the same length + for segment_idx in np.flatnonzero(to_amend): + if free_segment_count == 0: + break + + pos_of_same_length = np.logical_and(free_segments, segment_lengths[segment_idx] == self._segment_capacity[:last_reserved]) + idx_same_length = np.argmax(pos_of_same_length) + if pos_of_same_length[idx_same_length]: + free_segments[idx_same_length] = False + free_segment_count -= 1 + + to_amend[segment_idx] = False + to_insert[segment_idx] = idx_same_length + + # try to find places that are larger than the segments to fit in starting with the large segments and large + # free spaces + segment_indices = np.flatnonzero(to_amend)[np.argsort(segment_lengths[to_amend])[::-1]] + for segment_idx in segment_indices: + free_capacities = self._segment_capacity[free_segments] + free_segments_indices = np.flatnonzero(free_segments)[np.argsort(free_capacities)[::-1]] + + if len(free_segments_indices) == 0: + break + + fitting_segment = np.argmax((free_capacities >= segment_lengths[segment_idx])[::-1]) + fitting_segment = free_segments_indices[fitting_segment] + if self._segment_capacity[fitting_segment] >= segment_lengths[segment_idx]: + free_segments[fitting_segment] = False + to_amend[segment_idx] = False + to_insert[segment_idx] = fitting_segment + + free_points_at_end = self.total_capacity - np.sum(self._segment_capacity[:last_reserved]) + if np.sum(segment_lengths[to_amend] + 16) > free_points_at_end: + raise MemoryError('Fragmentation does not allow upload.', + np.sum(segment_lengths[to_amend] + 16), + free_points_at_end, + self._free_points_at_end) + + return waveform_to_segment, to_amend, to_insert + @with_configuration_guard def _upload_segment(self, segment_index: int, segment: TaborSegment) -> None: if self._segment_references[segment_index] > 0: @@ -587,13 +637,14 @@ def _upload_segment(self, segment_index: int, segment: TaborSegment) -> None: if segment.num_points > self._segment_capacity[segment_index]: raise ValueError('Cannot upload segment here.') + segment_no = segment_index + 1 - self._device.send_cmd(':TRAC:DEF {}, {}'.format(segment_index, segment.num_points)) + self._device.send_cmd(':TRAC:DEF {}, {}'.format(segment_no, segment.num_points)) self._segment_lengths[segment_index] = segment.num_points - self._device.send_cmd(':TRAC:SEL {}'.format(segment_index)) + self._device.send_cmd(':TRAC:SEL {}'.format(segment_no)) - self._device.send_cmd('TRAC:MODE COMB') + self._device.send_cmd(':TRAC:MODE COMB') wf_data = segment.get_as_binary() self._device.send_binary_data(pref=':TRAC:DATA', bin_dat=wf_data) @@ -601,19 +652,19 @@ def _upload_segment(self, segment_index: int, segment: TaborSegment) -> None: self._segment_hashes[segment_index] = hash(segment) @with_configuration_guard - def _amend_segments(self, segments: List[TaborSegment]) -> None: + def _amend_segments(self, segments: List[TaborSegment]) -> np.ndarray: new_lengths = np.asarray([s.num_points for s in segments], dtype=np.uint32) wf_data = make_combined_wave(segments) trac_len = len(wf_data) // 2 segment_index = len(self._segment_capacity) + 1 - self._device.send_cmd(':TRAC:DEF {}, {}'.format(segment_index, trac_len)) + self._device.send_cmd(':TRAC:DEF {},{}'.format(segment_index, trac_len)) self._device.send_cmd(':TRAC:SEL {}'.format(segment_index)) - self._device.send_cmd('TRAC:MODE COMB') + self._device.send_cmd(':TRAC:MODE COMB') self._device.send_binary_data(pref=':TRAC:DATA', bin_dat=wf_data) - old_to_update = np.count_nonzero(self._segment_capacity != self._segment_lengths and self._segment_reserved) + old_to_update = np.count_nonzero(self._segment_capacity != self._segment_lengths) segment_capacity = np.concatenate((self._segment_capacity, new_lengths)) segment_lengths = np.concatenate((self._segment_lengths, new_lengths)) segment_references = np.concatenate((self._segment_references, np.ones(len(segments), dtype=int))) @@ -621,29 +672,31 @@ def _amend_segments(self, segments: List[TaborSegment]) -> None: if len(segments) < old_to_update: for i, segment in enumerate(segments): current_segment = segment_index + i - self._device.send_cmd(':TRAC:DEF {}, {}'.format(current_segment, segment.num_points)) + self._device.send_cmd(':TRAC:DEF {},{}'.format(current_segment, segment.num_points)) else: # flush the capacity self._device.download_segment_lengths(segment_capacity) # update non fitting lengths - for i in np.flatnonzero(np.logical_and(segment_capacity != segment_lengths, segment_references > 0)): - self._device.send_cmd(':TRAC:DEF {},{}'.format(i, segment_lengths[i])) + for i in np.flatnonzero(segment_capacity != segment_lengths): + self._device.send_cmd(':TRAC:DEF {},{}'.format(i+1, segment_lengths[i])) self._segment_capacity = segment_capacity self._segment_lengths = segment_lengths self._segment_hashes = segment_hashes self._segment_references = segment_references + return segment_index + np.arange(len(segments), dtype=np.int64) - 1 + def cleanup(self) -> None: """Discard all segments after the last which is still referenced""" reserved_indices = np.flatnonzero(self._segment_references > 0) - new_end = reserved_indices[-1]+1 if reserved_indices else 0 + new_end = reserved_indices[-1]+1 if len(reserved_indices) else 0 self._segment_lengths = self._segment_lengths[:new_end] self._segment_capacity = self._segment_capacity[:new_end] - self._segment_hashes = self._segment_capacity[:new_end] - self._segment_references = self._segment_capacity[:new_end] + self._segment_hashes = self._segment_hashes[:new_end] + self._segment_references = self._segment_references[:new_end] def remove(self, name: str) -> None: """Remove a program from the AWG. @@ -694,6 +747,7 @@ def change_armed_program(self, name: str) -> None: assert len(sequencer_tables) == 2 while len(sequencer_tables[1]) < self._device.dev_properties['min_seq_len']: + assert advanced_sequencer_table[0][0] == 1 sequencer_tables[1].append((1, 1, 0)) # insert idle sequence in advanced sequence table @@ -702,7 +756,7 @@ def change_armed_program(self, name: str) -> None: while len(advanced_sequencer_table) < self._device.dev_properties['min_aseq_len']: advanced_sequencer_table.append((1, 1, 0)) - #download all sequence tables + # download all sequence tables for i, sequencer_table in enumerate(sequencer_tables): if i >= len(self._sequencer_tables) or self._sequencer_tables[i] != sequencer_table: self._device.send_cmd('SEQ:SEL {}'.format(i+1)) @@ -726,7 +780,7 @@ def run_current_program(self) -> None: @property def programs(self) -> Set[str]: """The set of program names that can currently be executed on the hardware AWG.""" - raise set(program.name for program in self._known_programs.keys()) + return set(program.name for program in self._known_programs.keys()) @property def sample_rate(self) -> float: diff --git a/qctoolkit/hardware/util.py b/qctoolkit/hardware/util.py index d9d6f6684..255c968e1 100644 --- a/qctoolkit/hardware/util.py +++ b/qctoolkit/hardware/util.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Sequence import numpy as np @@ -66,3 +66,18 @@ def make_combined_wave(segments: List['TaborSegment'], destination_array=None, f current_quantum += segment_quanta return destination_array.ravel() + +def find_positions(data: Sequence, to_find: Sequence) -> np.ndarray: + """Find indices of the first occurrence of the elements of to_find in data. Elements that are not in data result in + -1""" + data_sorter = np.argsort(data) + + pos_left = np.searchsorted(data, to_find, side='left', sorter=data_sorter) + pos_right = np.searchsorted(data, to_find, side='right', sorter=data_sorter) + + found = pos_left < pos_right + + positions = np.full_like(to_find, fill_value=-1, dtype=np.int64) + positions[found] = data_sorter[pos_left[found]] + + return positions diff --git a/tests/hardware/dummy_modules.py b/tests/hardware/dummy_modules.py index 99143b589..e0731c70b 100644 --- a/tests/hardware/dummy_modules.py +++ b/tests/hardware/dummy_modules.py @@ -59,6 +59,12 @@ def __init__(self, *args, paranoia_level=1, **kwargs): self.logged_queries = [] self._visa_inst = dummy_pyvisa.resources.MessageBasedResource() self.paranoia_level = paranoia_level + self.dev_properties = dummy_teawg.model_properties_dict + + self._download_segment_lengths_calls = [] + self._send_binary_data_calls = [] + self._download_adv_seq_table_calls = [] + self._download_sequencer_table_calls = [] @property def visa_inst(self): return self._visa_inst @@ -66,8 +72,14 @@ def send_cmd(self, *args, **kwargs): self.logged_commands.append((args, kwargs)) def send_query(self, *args, **kwargs): return self._visa_inst.ask(*args, **kwargs) - send_binary_data = send_cmd - download_sequencer_table = send_cmd + def download_segment_lengths(self, seg_len_list, pref='dummy_pref', paranoia_level='dummy_paranoia'): + self._download_segment_lengths_calls.append((seg_len_list, pref, paranoia_level)) + def send_binary_data(self, pref, bin_dat, paranoia_level='dummy_paranoia'): + self._send_binary_data_calls.append((pref, bin_dat, paranoia_level)) + def download_adv_seq_table(self, advanced_sequencer_table): + self._download_adv_seq_table_calls.append(advanced_sequencer_table) + def download_sequencer_table(self, sequencer_table): + self._download_sequencer_table_calls.append(sequencer_table) class dummy_atsaverage(dummy_package): class atsaverage(dummy_package): diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 73346740b..8ba0401f2 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -3,12 +3,14 @@ import itertools from copy import copy, deepcopy import numpy as np +import sys +from qctoolkit.pulses.table_pulse_template import TableWaveform, HoldInterpolationStrategy from qctoolkit.hardware.awgs.tabor import TaborAWGRepresentation, TaborException, TaborProgram, TaborChannelPair,\ - TaborSegment, TaborSequencing, TaborProgramMemory + TaborSegment, TaborSequencing, TaborProgramMemory, with_configuration_guard from qctoolkit.hardware.program import MultiChannelProgram, Loop from qctoolkit.pulses.instructions import InstructionBlock -from qctoolkit.hardware.util import voltage_to_uint16 +from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave from teawg import model_properties_dict @@ -87,6 +89,12 @@ def test_init(self): with self.assertRaises(TaborException): TaborProgram(prog['A'], self.instr_props, ('A', 'B', 'C'), (None, None)) + def test_markers(self): + self.assertEqual(TaborProgram(self.root_loop, self.instr_props, ('A', None), (None, 'B')).markers, (None, 'B')) + + def test_channels(self): + self.assertEqual(TaborProgram(self.root_loop, self.instr_props, ('A', None), (None, 'B')).channels, ('A', None)) + def test_depth_0_single_waveform(self): program = Loop(waveform=DummyWaveform(defined_channels={'A'}), repetition_count=3) @@ -315,7 +323,110 @@ def test_trigger(self): self.assertEqual(inst.logged_commands, [((), dict(cmd_str=':TRIG', paranoia_level=inst.paranoia_level))]) + def test_reset(self): + inst = get_instrument() + inst.paranoia_level = 0 + inst.logged_commands = [] + inst.reset() + + expected_commands = [':RES', + ':INST:SEL 1; :INIT:GATE OFF; :INIT:CONT ON; ' + ':INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS', + ':INST:SEL 3; :INIT:GATE OFF; :INIT:CONT ON; ' + ':INIT:CONT:ENAB ARM; :INIT:CONT:ENAB:SOUR BUS'] + expected_log = [((), dict(cmd_str=cmd, paranoia_level=inst.paranoia_level)) + for cmd in expected_commands] + self.assertEqual(inst.logged_commands, expected_log) + + def test_enable(self): + inst = get_instrument() + inst.paranoia_level = 0 + + inst.logged_commands = [] + inst.enable() + + expected_commands = [':ENAB'] + expected_log = [((), dict(cmd_str=cmd, paranoia_level=inst.paranoia_level)) + for cmd in expected_commands] + self.assertEqual(inst.logged_commands, expected_log) + + +class ConfigurationGuardTest(unittest.TestCase): + class DummyChannelPair: + def __init__(self, test_obj: unittest.TestCase): + self.test_obj = test_obj + self._configuration_guard_count = 0 + self.is_in_config_mode = False + + def _enter_config_mode(self): + self.test_obj.assertFalse(self.is_in_config_mode) + self.test_obj.assertEqual(self._configuration_guard_count, 0) + self.is_in_config_mode = True + + def _exit_config_mode(self): + self.test_obj.assertTrue(self.is_in_config_mode) + self.test_obj.assertEqual(self._configuration_guard_count, 0) + self.is_in_config_mode = False + + @with_configuration_guard + def guarded_method(self, counter=5, throw=False): + self.test_obj.assertTrue(self.is_in_config_mode) + if counter > 0: + return self.guarded_method(counter - 1, throw) + 1 + if throw: + raise RuntimeError() + return 0 + + def test_config_guard(self): + channel_pair = ConfigurationGuardTest.DummyChannelPair(self) + + for i in range(5): + self.assertEqual(channel_pair.guarded_method(i), i) + + with self.assertRaises(RuntimeError): + channel_pair.guarded_method(1, True) + + self.assertFalse(channel_pair.is_in_config_mode) + + +class DummyTaborProgramClass: + def __init__(self, segments=None, segment_lengths=None, + sequencer_tables=None, advanced_sequencer_table=None, waveform_mode=None): + self.program = None + self.device_properties = None + self.channels = None + self.markers = None + + self.segment_lengths = segment_lengths + self.segments = segments + + self.sequencer_tables = sequencer_tables + self.advanced_sequencer_table = advanced_sequencer_table + self.waveform_mode = waveform_mode + + self.created = [] + + def __call__(self, program: Loop, device_properties, channels, markers): + self.program = program + self.device_properties = device_properties + self.channels = channels + self.markers = markers + + class DummyTaborProgram: + def __init__(self, class_obj: DummyTaborProgramClass): + self.sampled_segments_calls = [] + self.class_obj = class_obj + self.waveform_mode = class_obj.waveform_mode + def sampled_segments(self, sample_rate, voltage_amplitude, voltage_offset, voltage_transformation): + self.sampled_segments_calls.append((sample_rate, voltage_amplitude, voltage_offset, voltage_transformation)) + return self.class_obj.segments, self.class_obj.segment_lengths + def get_sequencer_tables(self): + return self.class_obj.sequencer_tables + def get_advanced_sequencer_table(self): + return self.class_obj.advanced_sequencer_table + self.created.append(DummyTaborProgram(self)) + return self.created[-1] class TaborChannelPairTests(unittest.TestCase): @@ -330,5 +441,461 @@ def test_init(self): with self.assertRaises(ValueError): TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 3)) + def test_free_program(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + + with self.assertRaises(KeyError): + channel_pair.free_program('test') + + program = TaborProgramMemory(np.array([1, 2], dtype=np.int64), None) + + channel_pair._segment_references = np.array([1, 3, 1, 0]) + channel_pair._known_programs['test'] = program + self.assertIs(channel_pair.free_program('test'), program) + + np.testing.assert_equal(channel_pair._segment_references, np.array([1, 2, 0, 0])) + + + def test_upload_exceptions(self): + wv = TableWaveform(1, [(0, 0.1, HoldInterpolationStrategy()), + (192, 0.1, HoldInterpolationStrategy())], []) + + channel_pair = TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 2)) + + program = Loop(waveform=wv) + with self.assertRaises(ValueError): + channel_pair.upload('test', program, (1, 2, 3), (5, 6), (lambda x: x, lambda x: x)) + with self.assertRaises(ValueError): + channel_pair.upload('test', program, (1, 2), (5, 6, 'a'), (lambda x: x, lambda x: x)) + with self.assertRaises(ValueError): + channel_pair.upload('test', program, (1, 2), (3, 4), (lambda x: x,)) + + channel_pair._known_programs['test'] = TaborProgramMemory(np.array([0]), None) + with self.assertRaises(ValueError): + channel_pair.upload('test', program, (1, 2), (3, 4), (lambda x: x, lambda x: x)) + + def test_upload(self): + segments = np.array([1, 2, 3, 4, 5]) + segment_lengths = np.array([0, 16, 0, 16, 0], dtype=np.uint16) + + segment_references = np.array([1, 1, 2, 0, 1], dtype=np.uint32) + + w2s = np.array([-1, -1, 1, 2, -1], dtype=np.int64) + ta = np.array([True, False, False, False, True]) + ti = np.array([-1, 3, -1, -1, -1]) + + to_restore = sys.modules['qctoolkit.hardware.awgs.tabor'].TaborProgram + my_class = DummyTaborProgramClass(segments=segments, segment_lengths=segment_lengths) + sys.modules['qctoolkit.hardware.awgs.tabor'].TaborProgram = my_class + try: + program = Loop() + + channel_pair = TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 2)) + channel_pair._segment_references = segment_references + + def dummy_find_place(segments_, segement_lengths_): + self.assertIs(segments_, segments) + self.assertIs(segment_lengths, segement_lengths_) + return w2s, ta, ti + + def dummy_upload_segment(segment_index, segment): + self.assertEqual(segment_index, 3) + self.assertEqual(segment, 2) + + def dummy_amend_segments(segments_): + np.testing.assert_equal(segments_, np.array([1, 5])) + return np.array([5, 6], dtype=np.int64) + + channel_pair._find_place_for_segments_in_memory = dummy_find_place + channel_pair._upload_segment = dummy_upload_segment + channel_pair._amend_segments = dummy_amend_segments + + channel_pair.upload('test', program, (1, None), (None, None), (lambda x: x, lambda x: x)) + + self.assertIs(my_class.program, program) + + # the other references are increased in amend and upload segment method + np.testing.assert_equal(channel_pair._segment_references, np.array([1, 2, 3, 0, 1])) + + self.assertEqual(len(channel_pair._known_programs), 1) + np.testing.assert_equal(channel_pair._known_programs['test'].segment_indices, + np.array([5, 3, 1, 2, 6], dtype=np.int64)) + self.assertIs(channel_pair._known_programs['test'].program, my_class.created[0]) + + finally: + sys.modules['qctoolkit.hardware.awgs.tabor'].TaborProgram = to_restore + + def test_find_place_for_segments_in_memory(self): + def hash_based_on_dir(ch): + hash_list = [] + for d in dir(ch): + o = getattr(ch, d) + if isinstance(o, np.ndarray): + hash_list.append(hash(o.tobytes())) + else: + try: + hash_list.append(hash(o)) + except TypeError: + pass + return hash(tuple(hash_list)) + + channel_pair = TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 2)) + + # empty + segments = np.asarray([-5, -6, -7, -8, -9]) + segment_lengths = 192 + np.asarray([32, 16, 64, 32, 16]) + + hash_before = hash_based_on_dir(channel_pair) + + w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(segments, segment_lengths) + self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(ta.tolist(), [True, True, True, True, True]) + self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) + + # all new segments + channel_pair._segment_capacity = 192 + np.asarray([0, 16, 32, 16, 0], dtype=np.uint32) + channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5], dtype=np.int64) + channel_pair._segment_references = np.asarray([1, 1, 1, 2, 1], dtype=np.int32) + hash_before = hash_based_on_dir(channel_pair) + + w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(segments, segment_lengths) + self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(ta.tolist(), [True, True, True, True, True]) + self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) + + # some known segments + channel_pair._segment_capacity = 192 + np.asarray([0, 16, 32, 64, 0, 16], dtype=np.uint32) + channel_pair._segment_hashes = np.asarray([1, 2, 3, -7, 5, -9], dtype=np.int64) + channel_pair._segment_references = np.asarray([1, 1, 1, 2, 1, 3], dtype=np.int32) + hash_before = hash_based_on_dir(channel_pair) + + w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(segments, segment_lengths) + self.assertEqual(w2s.tolist(), [-1, -1, 3, -1, 5]) + self.assertEqual(ta.tolist(), [True, True, False, True, False]) + self.assertEqual(ti.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) + + # insert some segments with same length + channel_pair._segment_capacity = 192 + np.asarray([0, 16, 32, 64, 0, 16], dtype=np.uint32) + channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5, 6], dtype=np.int64) + channel_pair._segment_references = np.asarray([1, 0, 1, 0, 1, 3], dtype=np.int32) + hash_before = hash_based_on_dir(channel_pair) + + w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(segments, segment_lengths) + self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(ta.tolist(), [True, False, False, True, True]) + self.assertEqual(ti.tolist(), [-1, 1, 3, -1, -1]) + self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) + + # insert some segments with smaller length + channel_pair._segment_capacity = 192 + np.asarray([0, 80, 32, 64, 96, 16], dtype=np.uint32) + channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, 5, 6], dtype=np.int64) + channel_pair._segment_references = np.asarray([1, 0, 1, 1, 0, 3], dtype=np.int32) + hash_before = hash_based_on_dir(channel_pair) + + w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(segments, segment_lengths) + self.assertEqual(w2s.tolist(), [-1, -1, -1, -1, -1]) + self.assertEqual(ta.tolist(), [True, True, False, False, True]) + self.assertEqual(ti.tolist(), [-1, -1, 4, 1, -1]) + self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) + + # mix everything + segments = np.asarray([-5, -6, -7, -8, -9, -10, -11]) + segment_lengths = 192 + np.asarray([32, 16, 64, 32, 16, 0, 0]) + + channel_pair._segment_capacity = 192 + np.asarray([0, 80, 32, 64, 32, 16], dtype=np.uint32) + channel_pair._segment_hashes = np.asarray([1, 2, 3, 4, -8, 6], dtype=np.int64) + channel_pair._segment_references = np.asarray([1, 0, 1, 0, 1, 0], dtype=np.int32) + hash_before = hash_based_on_dir(channel_pair) + + w2s, ta, ti = channel_pair._find_place_for_segments_in_memory(segments, segment_lengths) + self.assertEqual(w2s.tolist(), [-1, -1, -1, 4, -1, -1, -1]) + self.assertEqual(ta.tolist(), [False, True, False, False, True, True, True]) + self.assertEqual(ti.tolist(), [1, -1, 3, -1, -1, -1, -1]) + self.assertEqual(hash_before, hash_based_on_dir(channel_pair)) + + def test_upload_segment(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + channel_pair._segment_references = np.array([1, 2, 0, 1], dtype=np.uint32) + channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32], dtype=np.uint32) + channel_pair._segment_lengths = channel_pair._segment_capacity.copy() + + channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64) + + # prevent entering and exiting configuration mode + channel_pair._configuration_guard_count = 2 + + segment = TaborSegment(np.ones(192+16, dtype=np.uint16), np.zeros(192+16, dtype=np.uint16)) + segment_binary = segment.get_as_binary() + with self.assertRaises(ValueError): + channel_pair._upload_segment(3, segment) + + with self.assertRaises(ValueError): + channel_pair._upload_segment(0, segment) + + channel_pair._upload_segment(2, segment) + np.testing.assert_equal(channel_pair._segment_capacity, 192 + np.array([0, 16, 32, 32], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_lengths, 192 + np.array([0, 16, 16, 32], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_hashes, np.array([1, 2, hash(segment), 4], dtype=np.int64)) + + expected_commands = [':TRAC:DEF 3, 208', + ':TRAC:SEL 3', + ':TRAC:MODE COMB'] + expected_log = [((), dict(cmd_str=cmd, paranoia_level=instrument.paranoia_level)) + for cmd in expected_commands] + self.assertEqual(instrument.logged_commands, expected_log) + + expected_send_binary_data_log = [(':TRAC:DATA', segment_binary, 'dummy_paranoia')] + np.testing.assert_equal(instrument._send_binary_data_calls, expected_send_binary_data_log) + + def test_amend_segments_flush(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + # prevent entering and exiting configuration mode + channel_pair._configuration_guard_count = 2 + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + channel_pair._segment_references = np.array([1, 2, 0, 1], dtype=np.uint32) + channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32], dtype=np.uint32) + channel_pair._segment_lengths = 192 + np.array([0, 16, 16, 32], dtype=np.uint32) + + channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64) + + data = np.ones(192, dtype=np.uint16) + segments = [TaborSegment(0*data, 1*data), + TaborSegment(1*data, 2*data)] + + channel_pair._amend_segments(segments) + + expected_references = np.array([1, 2, 0, 1, 1, 1], dtype=np.uint32) + expected_capacities = 192 + np.array([0, 16, 32, 32, 0, 0], dtype=np.uint32) + expected_lengths = 192 + np.array([0, 16, 16, 32, 0, 0], dtype=np.uint32) + expected_hashes = np.array([1, 2, 3, 4, hash(segments[0]), hash(segments[1])], dtype=np.int64) + + np.testing.assert_equal(channel_pair._segment_references, expected_references) + np.testing.assert_equal(channel_pair._segment_capacity, expected_capacities) + np.testing.assert_equal(channel_pair._segment_lengths, expected_lengths) + np.testing.assert_equal(channel_pair._segment_hashes, expected_hashes) + + expected_commands = [':TRAC:DEF 5,{}'.format(2 * 192 + 16), + ':TRAC:SEL 5', + ':TRAC:MODE COMB', + ':TRAC:DEF 3,208'] + expected_log = [((), dict(cmd_str=cmd, paranoia_level=instrument.paranoia_level)) + for cmd in expected_commands] + self.assertEqual(expected_log, instrument.logged_commands) + + expected_download_segment_calls = [(expected_capacities, 'dummy_pref', 'dummy_paranoia')] + np.testing.assert_equal(instrument._download_segment_lengths_calls, expected_download_segment_calls) + + expected_bin_blob = make_combined_wave(segments) + expected_send_binary_data_log = [(':TRAC:DATA', expected_bin_blob, 'dummy_paranoia')] + np.testing.assert_equal(instrument._send_binary_data_calls, expected_send_binary_data_log) + + def test_amend_segments_iter(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + # prevent entering and exiting configuration mode + channel_pair._configuration_guard_count = 2 + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + channel_pair._segment_references = np.array([1, 2, 0, 1], dtype=np.uint32) + channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32], dtype=np.uint32) + channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16], dtype=np.uint32) + + channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64) + + data = np.ones(192, dtype=np.uint16) + segments = [TaborSegment(0*data, 1*data), + TaborSegment(1*data, 2*data)] + + indices = channel_pair._amend_segments(segments) + + expected_references = np.array([1, 2, 0, 1, 1, 1], dtype=np.uint32) + expected_capacities = 192 + np.array([0, 16, 32, 32, 0, 0], dtype=np.uint32) + expected_lengths = 192 + np.array([0, 0, 16, 16, 0, 0], dtype=np.uint32) + expected_hashes = np.array([1, 2, 3, 4, hash(segments[0]), hash(segments[1])], dtype=np.int64) + + np.testing.assert_equal(channel_pair._segment_references, expected_references) + np.testing.assert_equal(channel_pair._segment_capacity, expected_capacities) + np.testing.assert_equal(channel_pair._segment_lengths, expected_lengths) + np.testing.assert_equal(channel_pair._segment_hashes, expected_hashes) + + np.testing.assert_equal(indices, np.array([4, 5], dtype=np.int64)) + + expected_commands = [':TRAC:DEF 5,{}'.format(2 * 192 + 16), + ':TRAC:SEL 5', + ':TRAC:MODE COMB', + ':TRAC:DEF 5,192', + ':TRAC:DEF 6,192'] + expected_log = [((), dict(cmd_str=cmd, paranoia_level=instrument.paranoia_level)) + for cmd in expected_commands] + self.assertEqual(expected_log, instrument.logged_commands) + + expected_download_segment_calls = [] + self.assertEqual(instrument._download_segment_lengths_calls, expected_download_segment_calls) + + expected_bin_blob = make_combined_wave(segments) + expected_send_binary_data_log = [(':TRAC:DATA', expected_bin_blob, 'dummy_paranoia')] + np.testing.assert_equal(instrument._send_binary_data_calls, expected_send_binary_data_log) + + def test_cleanup(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + channel_pair._segment_references = np.array([1, 2, 0, 1], dtype=np.uint32) + channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32], dtype=np.uint32) + channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16], dtype=np.uint32) + channel_pair._segment_hashes = np.array([1, 2, 3, 4], dtype=np.int64) + + channel_pair.cleanup() + np.testing.assert_equal(channel_pair._segment_references, np.array([1, 2, 0, 1], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_capacity, 192 + np.array([0, 16, 32, 32], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_lengths, 192 + np.array([0, 0, 16, 16], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_hashes, np.array([1, 2, 3, 4], dtype=np.int64)) + + channel_pair._segment_references = np.array([1, 2, 0, 1, 0], dtype=np.uint32) + channel_pair._segment_capacity = 192 + np.array([0, 16, 32, 32, 32], dtype=np.uint32) + channel_pair._segment_lengths = 192 + np.array([0, 0, 16, 16, 0], dtype=np.uint32) + channel_pair._segment_hashes = np.array([1, 2, 3, 4, 5], dtype=np.int64) + + channel_pair.cleanup() + np.testing.assert_equal(channel_pair._segment_references, np.array([1, 2, 0, 1], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_capacity, 192 + np.array([0, 16, 32, 32], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_lengths, 192 + np.array([0, 0, 16, 16], dtype=np.uint32)) + np.testing.assert_equal(channel_pair._segment_hashes, np.array([1, 2, 3, 4], dtype=np.int64)) + + def test_remove(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + + calls = [] + + program_name = 'test' + def dummy_free_program(name): + self.assertIs(name, program_name) + calls.append('free_program') + + def dummy_cleanup(): + calls.append('cleanup') + + channel_pair.cleanup = dummy_cleanup + channel_pair.free_program = dummy_free_program + + channel_pair.remove(program_name) + self.assertEqual(calls, ['free_program', 'cleanup']) + + def test_change_armed_program_single_sequence(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + # prevent entering and exiting configuration mode + channel_pair._configuration_guard_count = 2 + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + advanced_sequencer_table = [(2, 1, 0)] + sequencer_tables = [[(3, 1, 0), (2, 2, 0), (1, 1, 0), (1, 3, 0), (1, 4, 0)]] + w2s = np.array([2, 5, 3, 1]) + + expected_sequencer_table = [(3, 2, 0), (2, 5, 0), (1, 2, 0), (1, 3, 0), (1, 1, 0)] + idle_sequencer_table = [(1, 1, 0), (1, 1, 0), (1, 1, 0)] + + program = DummyTaborProgramClass(advanced_sequencer_table=advanced_sequencer_table, + sequencer_tables=sequencer_tables, + waveform_mode=TaborSequencing.SINGLE)(None, None, None, None) + + channel_pair._known_programs['test'] = TaborProgramMemory(w2s, program) + + channel_pair.change_armed_program('test') + + self.assertEqual(instrument._download_adv_seq_table_calls, [[(1, 1, 1), (2, 2, 0), (1, 1, 0)]]) + self.assertEqual(instrument._download_sequencer_table_calls, [idle_sequencer_table, + expected_sequencer_table]) + + def test_change_armed_program_single_waveform(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + # prevent entering and exiting configuration mode + channel_pair._configuration_guard_count = 2 + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + advanced_sequencer_table = [(1, 1, 0)] + sequencer_tables = [[(10, 1, 0)]] + w2s = np.array([4]) + + expected_sequencer_table = [(10, 4, 0), (1, 1, 0), (1, 1, 0)] + idle_sequencer_table = [(1, 1, 0), (1, 1, 0), (1, 1, 0)] + + program = DummyTaborProgramClass(advanced_sequencer_table=advanced_sequencer_table, + sequencer_tables=sequencer_tables, + waveform_mode=TaborSequencing.SINGLE)(None, None, None, None) + + channel_pair._known_programs['test'] = TaborProgramMemory(w2s, program) + + channel_pair.change_armed_program('test') + + self.assertEqual(instrument._download_adv_seq_table_calls, [[(1, 1, 1), (1, 2, 0), (1, 1, 0)]]) + self.assertEqual(instrument._download_sequencer_table_calls, [idle_sequencer_table, + expected_sequencer_table]) + + def test_change_armed_program_advanced_sequence(self): + instrument = get_instrument() + channel_pair = TaborChannelPair(instrument, identifier='asd', channels=(1, 2)) + # prevent entering and exiting configuration mode + channel_pair._configuration_guard_count = 2 + + instrument.paranoia_level = 0 + instrument.logged_commands = [] + instrument.logged_queries = [] + instrument._send_binary_data_calls = [] + + advanced_sequencer_table = [(2, 1, 0), (3, 2, 0)] + sequencer_tables = [[(3, 1, 0), (2, 2, 0), (1, 1, 0), (1, 3, 0), (1, 4, 0)], + [(4, 2, 0), (2, 2, 0), (1, 1, 0), (1, 3, 0), (1, 4, 0)]] + w2s = np.array([2, 5, 3, 1]) + + idle_sequencer_table = [(1, 1, 0), (1, 1, 0), (1, 1, 0)] + expected_sequencer_tables = [idle_sequencer_table, + [(3, 2, 0), (2, 5, 0), (1, 2, 0), (1, 3, 0), (1, 1, 0)], + [(4, 5, 0), (2, 5, 0), (1, 2, 0), (1, 3, 0), (1, 1, 0)]] + + program = DummyTaborProgramClass(advanced_sequencer_table=advanced_sequencer_table, + sequencer_tables=sequencer_tables, + waveform_mode=TaborSequencing.ADVANCED)(None, None, None, None) + + channel_pair._known_programs['test'] = TaborProgramMemory(w2s, program) + channel_pair.change_armed_program('test') + self.assertEqual(instrument._download_adv_seq_table_calls, [[(1, 1, 1), (2, 2, 0), (3, 3, 0)]]) + self.assertEqual(instrument._download_sequencer_table_calls, expected_sequencer_tables) \ No newline at end of file diff --git a/tests/hardware/util_tests.py b/tests/hardware/util_tests.py index f8b7f33f4..bd97d2efe 100644 --- a/tests/hardware/util_tests.py +++ b/tests/hardware/util_tests.py @@ -6,7 +6,7 @@ import numpy as np from qctoolkit.hardware.awgs.tabor import TaborSegment -from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave +from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave, find_positions from . import dummy_modules @@ -160,4 +160,14 @@ def exec_general(self, data_1, data_2, fill_value=None): result.__array_interface__['data']) with self.assertRaises(ValueError): - make_combined_wave(tabor_segments, destination_array=np.ones(16)) \ No newline at end of file + make_combined_wave(tabor_segments, destination_array=np.ones(16)) + + +class FindPositionTest(unittest.TestCase): + def test_find_position(self): + data = [2, 6, -24, 65, 46, 5, -10, 9] + to_find = [54, 12, 5, -10, 45, 6, 2] + + positions = find_positions(data, to_find) + + self.assertEqual(positions.tolist(), [-1, -1, 5, 6, -1, 1, 0]) From 03661001ba8626f027e4acb9f08852108fbe2d88 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 24 Aug 2017 22:04:54 +0200 Subject: [PATCH 103/116] Possible fix for failing test that works locally --- qctoolkit/hardware/awgs/tabor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index b1afdf9f2..9ba724ae0 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -607,8 +607,9 @@ def _find_place_for_segments_in_memory(self, segments: Sequence, segment_lengths # try to find places that are larger than the segments to fit in starting with the large segments and large # free spaces segment_indices = np.flatnonzero(to_amend)[np.argsort(segment_lengths[to_amend])[::-1]] + capacities = self._segment_capacity[:last_reserved] for segment_idx in segment_indices: - free_capacities = self._segment_capacity[free_segments] + free_capacities = capacities[free_segments] free_segments_indices = np.flatnonzero(free_segments)[np.argsort(free_capacities)[::-1]] if len(free_segments_indices) == 0: From a8cb49eced5c732aee8b06300e47778cc2f13167 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Wed, 30 Aug 2017 10:14:24 +0200 Subject: [PATCH 104/116] Remove outdated information from concepts --- doc/source/concepts/branching.rst | 4 ++-- doc/source/concepts/pulsetemplates.rst | 16 +++++++++++----- doc/source/concepts/sequencing.rst | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/doc/source/concepts/branching.rst b/doc/source/concepts/branching.rst index 5def89543..44d1f46a0 100644 --- a/doc/source/concepts/branching.rst +++ b/doc/source/concepts/branching.rst @@ -7,6 +7,6 @@ The qctoolkit was designed to model conditional branching in pulse execution usi Future Work ^^^^^^^^^^^ -Currently, there is no detailed concept on hardware abstraction and thus no meaningful representation of triggers and no hardware driver implementation that configures any device. This is still an open task. +Currently, the implemented hardware support does not support any triggers or conditional branching. This is still an open task. -It is quite common for hardware to allow triggers that are represented not only a boolean signal but, e.g., any 8-bit signal, thus enabling more than two branching options. While this could still be represented by the current classes by nesting :class:`.BranchPulseTemplate` objects and configuring triggers appropriately, the implementation of a template class which acts like a C-style switch statement might be a worthwhile consideration. \ No newline at end of file +It is possible for hardware to allow triggers that are represented not only a boolean signal but, e.g., any 8-bit signal, thus enabling more than two branching options. While this could still be represented by the current classes by nesting :class:`.BranchPulseTemplate` objects and configuring triggers appropriately, the implementation of a template class which acts like a C-style switch statement might be a worthwhile consideration. \ No newline at end of file diff --git a/doc/source/concepts/pulsetemplates.rst b/doc/source/concepts/pulsetemplates.rst index 2204543be..fd48539b0 100644 --- a/doc/source/concepts/pulsetemplates.rst +++ b/doc/source/concepts/pulsetemplates.rst @@ -3,18 +3,24 @@ Pulse Templates --------------- -The qctoolkit represents pulses as abstract pulse templates. A pulse template can be understood as a class of pulses that share a similar structure but differ in the concrete amplitude or duration of voltage levels. To this end, pulse templates are parametrizable. Pulse templates are also designed to feature easy reusability of existing templates and conditional execution based on hardware triggers, if supported by the devices. +The qctoolkit represents pulses as abstract pulse templates. A pulse template can be understood as a class of pulses that share a similar structure but differ in the concrete amplitude or duration of voltage levels. To this end, pulse templates are parametrizable. Pulse templates are also designed to feature easy reusability of existing templates and conditional execution based on hardware triggers, if supported by the devices. The process of plugging in values for a pluse templates parameters is called instantiation. -There are 6 types of different pulse template classes, briefly explained in the following. :class:`.TablePulseTemplate` and :class:`.FunctionPulseTemplate` are used to define the atomic building blocks of pulses in the following ways: :class:`.TablePulseTemplate` allows the user to specify pairs of time and voltage values and choose an interpolation strategy between neighbouring points. :class:`.FunctionPulseTemplate` will accept any mathematical function that maps time to voltage values. All other pulse template variants are then used to construct arbitrarily complex pulses by combining existing ones into new structures: :class:`.SequencePulseTemplate` enables the user to specify a sequence of existing pulse templates (subtemplates) and modify parameter values using a mapping function. :class:`.RepetitionPulseTemplate` is used to simply repeat one existing pulse template a given (constant) number of times. :class:`.BranchPulseTemplate` and :class:`.LoopPulseTemplate` implement conditional execution if supported. All of these pulse template variants can be similarly accessed through the common interface declared by :class:`.PulseTemplate`. [#tree]_ [#pattern]_ +There are multiple types of different pulse template classes, briefly explained in the following. :class:`.TablePulseTemplate`, :class:`.PointPulseTemplate`: and :class:`.FunctionPulseTemplate` are used to define the atomic building blocks of pulses in the following ways: :class:`.TablePulseTemplate` and :class:`.PointPulseTemplate` allow the user to specify pairs of time and voltage values and choose an interpolation strategy between neighbouring points. Both templates support multiple channels but :class:`.TablePulseTemplate` allows for different time values for different channels meaning that the channels can change their voltages at different times. :class:`.PointPulseTemplate` restricts this to switches at the same time by interpreting the voltage as an vector and provides a more convenient interface for this case. +:class:`.FunctionPulseTemplate` will accept any mathematical function that maps time to voltage values. Internally it uses :class:`.Expression` for function evaluation. +All other pulse template variants are then used to construct arbitrarily complex pulses by combining existing ones into new structures: :class:`.SequencePulseTemplate` enables the user to specify a sequence of existing pulse templates (subtemplates) and modify parameter values using a mapping function. :class:`.RepetitionPulseTemplate` is used to simply repeat one existing pulse template a given (constant) number of times. :class:`.ForLoopPulseTemplate` is similar but allows a parametrization of the loop body with the loop index. In the future there will be pulse templates that allow conditional execution like :class:`.BranchPulseTemplate` and :class:`.WhileLoopPulseTemplate`. All of these pulse template variants can be similarly accessed through the common interface declared by :class:`.PulseTemplate`. [#tree]_ [#pattern]_ One special pulse template is the :class:`.MappingPulseTemplate` which allows the renaming of channels and measurements as well as mapping parameters by mathematical expressions. + +As the class names are quite long the recommended way for abbreviation is to use the aliases defined in :py:mod:`~qctoolkit.pulses`. For example :class:`.FunctionPulseTemplate` is aliased as :class:`.FunctionPT` Each pulse template can be stored persistently in a human-readable JSON file. :ref:`Read more about serialization `. Parameters ^^^^^^^^^^ -As mentioned above, all pulse templates may contain parameters. :class:`.TablePulseTemplate` allows parameter references as table entries on the time and voltage domain. These are represented as :class:`.ParameterDeclaration` objects which are identified by a unique name and can impose lower and upper boundaries to the expected parameter value as well as a default value. :class:`.SequencePulseTemplate` allows to specify a set of new parameter declarations and a mapping of these to the parameter declarations of its subtemplates. This allows renaming of parameters, e.g., to avoid name clashes if several subtemplates declare similarly named parameters. The mapping also allows mathematical transformation of parameter values, such that values that are passed to subtemplates can be obtained by deriving them from one or more other parameter values passed to the :class:`.SequencePulseTemplate`. :class:`.RepetitionPulseTemplate`, :class:`.LoopPulseTemplate` and :class:`BranchPulseTemplate` will simply pass parameters to their subtemplates without modifying them in any way. +As mentioned above, all pulse templates may depend on parameters. During pulse template initialization the parameters simply are the free variables of expressions that occur in the pulse template. For example the :class:`.FunctionPulseTemplate` has expressions for its duration and the voltage time dependency. Some pulse templates provided means to constrain parameters by accepting a list of :class:`.ParameterConstraint` which are expressions which must be true. + +The mathematical expressions (for parameter transformation or as the function of the :class:`.FunctionPulseTemplate`) are encapsulated into an :class:`.Expression` class which wraps `sympy `_ for string evaluation. -The mathematical expressions (for parameter transformation or as the function of the :class:`.FunctionPulseTemplate`) are encapsulated into an :class:`.Expression` class which wraps existing python packages that are able to parse and evaluate expressions given as strings such as `py_expression_eval `_ and `numexpr `_. +In the future, it will be possible to have parameters dependent on measurement outcomes or other events. This is the reason :class:`.Parameter` objects are passed through on pulse instantiation. Obtaining a Concrete Pulse ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -22,7 +28,7 @@ Obtaining a Concrete Pulse To obtain a pulse ready for execution on the hardware from a pulse template, the user has to specify parameter values (if parameters were used in the pulse templates in question). In the simplest case, parameters are constant values that can be provided as plain float values. Other cases may require parameter values to be computed based on some measurement values obtained during preceding executions. If so, a subclass of the :class:`.Parameter` class which performs this computations when queried for a value can be provided. In order to translate the object structures that encode the pulse template in the software into a sequential representation of the concrete pulse with the given parameter values that is understandable by the hardware, the sequencing process has to be invoked. During this process, all parameter values are checked for consistency with the boundaries declared by the parameter declarations and the process is aborted if any violation occurs. :ref:`Read more about the sequencing process `. Relevant Examples -^^^^^^^^ +^^^^^^^^^^^^^^^^^ Examples demonstrating the use of pulse templates and parameters are :ref:`examples/00SimpleTablePulse.ipynb`, :ref:`examples/01SequencePulse.ipynb` and :ref:`examples/02FunctionPulse.ipynb`. diff --git a/doc/source/concepts/sequencing.rst b/doc/source/concepts/sequencing.rst index c159101f2..cfabcb749 100644 --- a/doc/source/concepts/sequencing.rst +++ b/doc/source/concepts/sequencing.rst @@ -6,7 +6,7 @@ Sequencing Overview and Usage ^^^^^^^^^^^^^^^^^^ -Defining pulses using the :ref:`pulse template definition class structures ` yields a tree structure of :class:`.PulseTemplate` objects. To obtain a concrete pulse that can be executed, parameters have to be replaced by corresponding values and the object structure has to be converted into a sequence of waveforms (and possibly triggered jump annotations) that the hardware drivers can comprehend. This process is called *sequencing* in the qctoolkit. It converts :class:`.TablePulseTemplate` and :class:`.FunctionPulseTemplate` objects into a waveform representation by sampling voltage values along the time domain using the specified interpolation rules between table values or evaluating the defining function respectively. The tree structure arising from the use of :class:`.SequencePulseTemplate`, :class:`.RepetitionPulseTemplate`, :class:`.BranchPulseTemplate` and :class:`.LoopPulseTemplate` is converted into an intermediate instruction language consisting of four instructions: Execute a waveform (:class:`.EXECInstruction`), an unconditional goto to another instruction (:class:`.GOTOInstruction`), a conditional jump to another instruction (:class:`.JMPInstruction`) and a stop command, halting execution (:class:`.STOPInstruction`). +Defining pulses using the :ref:`pulse template definition class structures ` yields a tree structure of :class:`.PulseTemplate` objects. To obtain a concrete pulse that can be executed, parameters have to be replaced by corresponding values and the object structure has to be converted into a sequence of waveforms (and possibly triggered jump annotations) that the hardware drivers can comprehend. This process is called *sequencing* in the qctoolkit. It converts atomic pulse templates like :class:`.TablePulseTemplate` and :class:`.FunctionPulseTemplate` into a waveform representation by sampling voltage values along the time domain using the specified interpolation rules between table values or evaluating the defining function respectively. The tree structure arising from the use of :class:`.SequencePulseTemplate`, :class:`.RepetitionPulseTemplate`, :class:`.ForLoopPulseTemplate`, :class:`.BranchPulseTemplate` and :class:`.LoopPulseTemplate` is converted into an intermediate instruction language consisting of four instructions: Execute a waveform (:class:`.EXECInstruction`), an unconditional goto to another instruction (:class:`.GOTOInstruction`), a conditional jump to another instruction (:class:`.JMPInstruction`) and a stop command, halting execution (:class:`.STOPInstruction`). This approach was inspired by translation of syntax trees to machine instructions in modern compilers and necessitated by the fact that the hardware requires sequential commands rather than convoluted object structures. The output of the sequencing process is a set of waveforms and a sequence of instructions. These will later be interpreted by hardware drivers which will configure the specific devices accordingly (assuming these are capable of the required functionality). If :class:`.BranchPulseTemplate` and :class:`.LoopPulseTemplate` are not used, the compiled instruction sequence will consist only of execution instructions followed by a stop instruction, which represents a simple sequence of waveforms to play back. From b47d46717e14c051cafb8e0d5979e9b42f572c87 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 10:59:25 +0200 Subject: [PATCH 105/116] Add automated waveform concatenation --- qctoolkit/hardware/awgs/tabor.py | 6 +- qctoolkit/hardware/program.py | 94 +++++++++++++++++++++++++++++++- qctoolkit/utils/__init__.py | 6 +- tests/hardware/program_tests.py | 65 +++++++++++++++++++++- 4 files changed, 167 insertions(+), 4 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 9ba724ae0..8c12c5dc5 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -12,7 +12,7 @@ from qctoolkit import ChannelID from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform -from qctoolkit.hardware.program import Loop +from qctoolkit.hardware.program import Loop, make_compatible from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave, find_positions from qctoolkit.hardware.awgs.base import AWG @@ -502,6 +502,10 @@ def upload(self, name: str, if len(voltage_transformation) != self.num_channels: raise ValueError('Wrong number of voltage transformations') + # adjust program to fit criteria + sample_rate = self._device.sample_rate(self._channels[0]) + make_compatible(program, minimal_waveform_length=192, waveform_quantum=16, sample_rate=sample_rate) + # helper to restore previous state if upload is impossible to_restore = None if name in self._known_programs: diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index 27ecace82..a5f3dca75 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -2,6 +2,7 @@ from typing import Union, Dict, Set, Iterable, FrozenSet, Tuple, cast from collections import deque, defaultdict from copy import deepcopy +from enum import Enum import numpy as np @@ -9,9 +10,12 @@ from qctoolkit.pulses.instructions import AbstractInstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction, Waveform from qctoolkit.comparable import Comparable from qctoolkit.utils.tree import Node, is_tree_circular +from qctoolkit.utils import checked_int_cast, is_integer +from qctoolkit.pulses.sequence_pulse_template import SequenceWaveform +from qctoolkit.pulses.repetition_pulse_template import RepetitionWaveform -__all__ = ['Loop', 'MultiChannelProgram', ''] +__all__ = ['Loop', 'MultiChannelProgram', 'make_compatible'] class Loop(Comparable, Node): @@ -315,3 +319,91 @@ def __getitem__(self, item: Union[ChannelID, Set[ChannelID], FrozenSet[ChannelID if item.issubset(channels): return program raise KeyError(item) + + +def to_waveform(program: Loop) -> Waveform: + if program.is_leaf(): + if program.repetition_count == 1: + return program.waveform + else: + return RepetitionWaveform(program.waveform, program.repetition_count) + else: + if len(program) == 1: + sequenced_waveform = to_waveform(cast(Loop, program[0])) + else: + sequenced_waveform = SequenceWaveform([to_waveform(cast(Loop, sub_program)) + for sub_program in program]) + if program.repetition_count > 1: + return RepetitionWaveform(sequenced_waveform, program.repetition_count) + else: + return sequenced_waveform + + +class _CompatibilityLevel(Enum): + compatible = 0 + action_required = 1 + incompatible = 2 + + +def _is_compatible(program: Loop, min_len: int, quantum: int, sample_rate: float) -> _CompatibilityLevel: + try: + program_duration = checked_int_cast(program.duration * sample_rate) + except ValueError: + return _CompatibilityLevel.incompatible + + if program_duration < min_len or program_duration % quantum > 0: + return _CompatibilityLevel.incompatible + + if program.is_leaf(): + waveform_duration = program.waveform.duration * sample_rate + if not is_integer(waveform_duration / quantum) or waveform_duration < min_len: + return _CompatibilityLevel.action_required + else: + return _CompatibilityLevel.compatible + else: + if all(_is_compatible(cast(Loop, sub_program), min_len, quantum, sample_rate) == _CompatibilityLevel.compatible + for sub_program in program): + return _CompatibilityLevel.compatible + else: + return _CompatibilityLevel.action_required + + +def _make_compatible(program: Loop, min_len: int, quantum: int, sample_rate: float) -> None: + + if program.is_leaf(): + program.waveform = to_waveform(program.copy_tree_structure()) + program.repetition_count = 1 + + else: + comp_levels = np.array([_is_compatible(cast(Loop, sub_program), min_len, quantum, sample_rate) + for sub_program in program]) + incompatible = comp_levels == _CompatibilityLevel.incompatible + if np.any(incompatible): + single_run = program.duration * sample_rate / program.repetition_count + if is_integer(single_run / quantum) and single_run >= min_len: + new_repetition_count = program.repetition_count + else: + new_repetition_count = 1 + program.repetition_count = 1 + program.waveform = to_waveform(program.copy_tree_structure()) + program.repetition_count = new_repetition_count + program[:] = [] + return + else: + for sub_program, comp_level in zip(program, comp_levels): + if comp_level == _CompatibilityLevel.action_required: + _make_compatible(sub_program, min_len, quantum, sample_rate) + + +def make_compatible(program: Loop, minimal_waveform_length: int, waveform_quantum: int, sample_rate: float): + comp_level = _is_compatible(program, + min_len=minimal_waveform_length, + quantum=waveform_quantum, + sample_rate=sample_rate) + if comp_level == _CompatibilityLevel.incompatible: + raise ValueError('The program cannot be made compatible to restrictions') + elif comp_level == _CompatibilityLevel.action_required: + _make_compatible(program, + min_len=minimal_waveform_length, + quantum=waveform_quantum, + sample_rate=sample_rate) diff --git a/qctoolkit/utils/__init__.py b/qctoolkit/utils/__init__.py index 10795e6ee..e51b7f517 100644 --- a/qctoolkit/utils/__init__.py +++ b/qctoolkit/utils/__init__.py @@ -2,7 +2,7 @@ import numpy -__all__ = ["checked_int_cast"] +__all__ = ["checked_int_cast", "is_integer"] def checked_int_cast(x: Union[float, int, numpy.ndarray], epsilon: float=1e-10) -> int: @@ -15,3 +15,7 @@ def checked_int_cast(x: Union[float, int, numpy.ndarray], epsilon: float=1e-10) if abs(x - int_x) > epsilon: raise ValueError('No integer', x) return int_x + + +def is_integer(x: Union[float, int], epsilon: float=1e-10): + return abs(x - int(round(x))) < epsilon diff --git a/tests/hardware/program_tests.py b/tests/hardware/program_tests.py index 5aaba2411..879dab3c7 100644 --- a/tests/hardware/program_tests.py +++ b/tests/hardware/program_tests.py @@ -6,7 +6,7 @@ from string import Formatter, ascii_uppercase -from qctoolkit.hardware.program import Loop, MultiChannelProgram +from qctoolkit.hardware.program import Loop, MultiChannelProgram, make_compatible, _make_compatible, _is_compatible, _CompatibilityLevel, RepetitionWaveform, SequenceWaveform from qctoolkit.pulses.instructions import REPJInstruction, InstructionBlock, ImmutableInstructionBlock from tests.pulses.sequencing_dummies import DummyWaveform from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform @@ -412,3 +412,66 @@ def test_via_repr(self): for loop in root_loopB.get_depth_first_iterator() if loop.is_leaf())) self.assertEqual(root_loopA.__repr__(), reprA) self.assertEqual(root_loopB.__repr__(), reprB) + + +class ProgramWaveformCompatibilityTest(unittest.TestCase): + def test_is_compatible_incompatible(self): + wf = DummyWaveform(duration=1.1) + + self.assertEqual(_is_compatible(Loop(waveform=wf), min_len=1, quantum=1, sample_rate=1.), + _CompatibilityLevel.incompatible) + + self.assertEqual(_is_compatible(Loop(waveform=wf, repetition_count=10), min_len=20, quantum=1, sample_rate=1.), + _CompatibilityLevel.incompatible) + + self.assertEqual(_is_compatible(Loop(waveform=wf, repetition_count=10), min_len=10, quantum=3, sample_rate=1.), + _CompatibilityLevel.incompatible) + + def test_is_compatible_leaf(self): + self.assertEqual(_is_compatible(Loop(waveform=DummyWaveform(duration=1.1), repetition_count=10), + min_len=11, quantum=1, sample_rate=1.), + _CompatibilityLevel.action_required) + + self.assertEqual(_is_compatible(Loop(waveform=DummyWaveform(duration=1.1), repetition_count=10), + min_len=11, quantum=1, sample_rate=10.), + _CompatibilityLevel.compatible) + + def test_is_compatible_node(self): + program = Loop(children=[Loop(waveform=DummyWaveform(duration=1.5), repetition_count=2), + Loop(waveform=DummyWaveform(duration=2.0))]) + + self.assertEqual(_is_compatible(program, min_len=1, quantum=1, sample_rate=2.), + _CompatibilityLevel.compatible) + + self.assertEqual(_is_compatible(program, min_len=1, quantum=1, sample_rate=1.), + _CompatibilityLevel.action_required) + + def test_make_compatible(self): + wf1 = DummyWaveform(duration=1.5) + wf2 = DummyWaveform(duration=2.0) + + program = Loop(children=[Loop(waveform=wf1, repetition_count=2), + Loop(waveform=wf2)]) + + _make_compatible(program, min_len=1, quantum=1, sample_rate=1.) + + self.assertIsNone(program.waveform) + self.assertEqual(len(program), 2) + self.assertIsInstance(program[0].waveform, RepetitionWaveform) + self.assertIs(program[0].waveform._body, wf1) + self.assertEqual(program[0].waveform._repetition_count, 2) + self.assertIs(program[1].waveform, wf2) + + program = Loop(children=[Loop(waveform=wf1, repetition_count=2), + Loop(waveform=wf2)], repetition_count=2) + _make_compatible(program, min_len=5, quantum=1, sample_rate=1.) + + self.assertIsInstance(program.waveform, SequenceWaveform) + self.assertEqual(program.children, []) + self.assertEqual(program.repetition_count, 2) + + self.assertEqual(len(program.waveform._sequenced_waveforms), 2) + self.assertIsInstance(program.waveform._sequenced_waveforms[0], RepetitionWaveform) + self.assertIs(program.waveform._sequenced_waveforms[0]._body, wf1) + self.assertEqual(program.waveform._sequenced_waveforms[0]._repetition_count, 2) + self.assertIs(program.waveform._sequenced_waveforms[1], wf2) From dbef65b6774565f926e5180f3923243b21da6cf7 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 11:08:11 +0200 Subject: [PATCH 106/116] Restructure some imports Fix some outdated __all__ Update doc strings Add more imports for annotation usage with autodoc Rename MappingTemplate to MappingPulseTemplate --- doc/source/conf.py | 22 +++- doc/source/qctoolkit.qcmatlab.rst | 30 ------ doc/source/qctoolkit.rst | 1 - qctoolkit/__init__.py | 8 +- qctoolkit/hardware/awgs/base.py | 19 ++-- qctoolkit/hardware/awgs/tabor.py | 3 +- qctoolkit/hardware/dacs/__init__.py | 2 + qctoolkit/hardware/program.py | 2 +- qctoolkit/hardware/setup.py | 2 +- qctoolkit/pulses/__init__.py | 9 +- qctoolkit/pulses/conditions.py | 100 +++++++++--------- qctoolkit/pulses/function_pulse_template.py | 39 +++---- qctoolkit/pulses/instructions.py | 54 +++++----- qctoolkit/pulses/loop_pulse_template.py | 32 ++++-- qctoolkit/pulses/measurement.py | 3 +- .../pulses/multi_channel_pulse_template.py | 20 ++-- qctoolkit/pulses/parameters.py | 11 +- qctoolkit/pulses/plotting.py | 12 +-- qctoolkit/pulses/point_pulse_template.py | 8 +- qctoolkit/pulses/pulse_template.py | 29 ++--- .../pulse_template_parameter_mapping.py | 68 +++++++----- qctoolkit/pulses/repetition_pulse_template.py | 17 ++- qctoolkit/pulses/sequence_pulse_template.py | 29 +++-- qctoolkit/pulses/sequencing.py | 32 +++--- qctoolkit/pulses/table_pulse_template.py | 30 ++++-- qctoolkit/serialization.py | 18 ++-- qctoolkit/utils/types.py | 36 +++++++ tests/hardware/tabor_tests.py | 2 +- .../multi_channel_pulse_template_tests.py | 6 +- .../pulse_template_parameter_mapping_tests.py | 72 ++++++------- tests/pulses/pulse_template_tests.py | 2 +- tests/pulses/sequence_pulse_template_tests.py | 8 +- tests/pulses/sequencing_dummies.py | 2 +- ...e_sequence_sequencer_intergration_tests.py | 6 +- 34 files changed, 420 insertions(+), 314 deletions(-) delete mode 100644 doc/source/qctoolkit.qcmatlab.rst create mode 100644 qctoolkit/utils/types.py diff --git a/doc/source/conf.py b/doc/source/conf.py index eb9d62ca4..dbe55403e 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -33,16 +33,20 @@ extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.napoleon', + 'sphinx_autodoc_typehints', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.mathjax', 'sphinx.ext.ifconfig', 'sphinx.ext.viewcode', - 'sphinx.ext.autodoc', 'nbsphinx' ] +autodoc_default_flags = ['members', 'undoc-members', 'show-inheritance'] # 'private-members', 'special-members', 'inherited-members' +autoclass_content = 'both' +napoleon_include_init_with_doc = True + # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] @@ -299,3 +303,19 @@ # Example configuration for intersphinx: refer to the Python standard library. intersphinx_mapping = {'https://docs.python.org/': None} + + +def skip(app, what, name, obj, skip, options): + if name == "__init__" and hasattr(obj, '__doc__') and isinstance(obj.__doc__, str) and len(obj.__doc__): + return True + return skip + +def change_property_rtype_to_type(app, what, name, obj, options, lines): + if what == 'attribute': + for i, line in enumerate(lines): + print(line) + lines[i] = line.replace(':rtype: :py:class:', ':type:') + +def setup(app): + app.connect('autodoc-skip-member', skip) + #app.connect('autodoc-process-docstring', change_property_rtype_to_type) diff --git a/doc/source/qctoolkit.qcmatlab.rst b/doc/source/qctoolkit.qcmatlab.rst deleted file mode 100644 index 048718de7..000000000 --- a/doc/source/qctoolkit.qcmatlab.rst +++ /dev/null @@ -1,30 +0,0 @@ -qctoolkit.qcmatlab package -========================== - -Submodules ----------- - -qctoolkit.qcmatlab.manager module ---------------------------------- - -.. automodule:: qctoolkit.qcmatlab.manager - :members: - :undoc-members: - :show-inheritance: - -qctoolkit.qcmatlab.pulse_control module ---------------------------------------- - -.. automodule:: qctoolkit.qcmatlab.pulse_control - :members: - :undoc-members: - :show-inheritance: - - -Module contents ---------------- - -.. automodule:: qctoolkit.qcmatlab - :members: - :undoc-members: - :show-inheritance: diff --git a/doc/source/qctoolkit.rst b/doc/source/qctoolkit.rst index 23ed3055b..df60a9fa8 100644 --- a/doc/source/qctoolkit.rst +++ b/doc/source/qctoolkit.rst @@ -8,7 +8,6 @@ Subpackages qctoolkit.hardware qctoolkit.pulses - qctoolkit.qcmatlab qctoolkit.utils Submodules diff --git a/qctoolkit/__init__.py b/qctoolkit/__init__.py index 87b745791..1d25667f6 100644 --- a/qctoolkit/__init__.py +++ b/qctoolkit/__init__.py @@ -1,6 +1,4 @@ -import typing +from qctoolkit.utils.types import MeasurementWindow, ChannelID +from . import pulses -__all__ = ["MeasurementWindow", "ChannelID"] - -MeasurementWindow = typing.Tuple[str, float, float] -ChannelID = typing.Union[str, int] +__all__ = ["MeasurementWindow", "ChannelID", "pulses"] diff --git a/qctoolkit/hardware/awgs/base.py b/qctoolkit/hardware/awgs/base.py index 9ecf7d0b9..f3d7a79f7 100644 --- a/qctoolkit/hardware/awgs/base.py +++ b/qctoolkit/hardware/awgs/base.py @@ -10,12 +10,12 @@ from abc import abstractmethod, abstractproperty from typing import Set, Tuple, List, Callable, Optional -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID from qctoolkit.hardware.program import Loop from qctoolkit.comparable import Comparable from qctoolkit.pulses.instructions import InstructionSequence -__all__ = ["AWG", "Program", "DummyAWG", "ProgramOverwriteException", +__all__ = ["AWG", "Program", "ProgramOverwriteException", "OutOfWaveformMemoryException"] Program = InstructionSequence @@ -27,6 +27,7 @@ class AWG(Comparable): It represents a set of channels that have to have(hardware enforced) the same: -control flow -sample rate + It keeps track of the AWG state and manages waveforms and programs on the hardware. """ @@ -60,13 +61,13 @@ def upload(self, name: str, for syncing. Programs that are uploaded should be fast(~1 sec) to arm. Args: - name (str): A name for the program on the AWG. - program (Loop): The program (a sequence of instructions) to upload. - channels (List): Tuple of length num_channels that ChannelIDs of in the program to use. Position in the list corresponds to the AWG channel - markers (List): List of channels in the program to use. Position in the List in the list corresponds to the AWG channel - voltage_transformation (List): transformations applied to the waveforms extracted rom the program. Position + name: A name for the program on the AWG. + program: The program (a sequence of instructions) to upload. + channels: Tuple of length num_channels that ChannelIDs of in the program to use. Position in the list corresponds to the AWG channel + markers: List of channels in the program to use. Position in the List in the list corresponds to the AWG channel + voltage_transformation: transformations applied to the waveforms extracted rom the program. Position in the list corresponds to the AWG channel - force (bool): If a different sequence is already present with the same name, it is + force: If a different sequence is already present with the same name, it is overwritten if force is set to True. (default = False) """ @@ -77,7 +78,7 @@ def remove(self, name: str) -> None: Also discards all waveforms referenced only by the program identified by name. Args: - name (str): The name of the program to remove. + name: The name of the program to remove. """ @abstractmethod diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 8c12c5dc5..3c029c243 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -10,7 +10,7 @@ import teawg import numpy as np -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform from qctoolkit.hardware.program import Loop, make_compatible from qctoolkit.hardware.util import voltage_to_uint16, make_combined_wave, find_positions @@ -521,7 +521,6 @@ def upload(self, name: str, channels=tuple(channels), markers=markers, device_properties=self._device.dev_properties) - sample_rate = self._device.sample_rate(self._channels[0]) voltage_amplitudes = (self._device.amplitude(self._channels[0]), self._device.amplitude(self._channels[1])) voltage_offsets = (0, 0) diff --git a/qctoolkit/hardware/dacs/__init__.py b/qctoolkit/hardware/dacs/__init__.py index 5bc0f5297..d57926869 100644 --- a/qctoolkit/hardware/dacs/__init__.py +++ b/qctoolkit/hardware/dacs/__init__.py @@ -1,6 +1,8 @@ from abc import ABCMeta, abstractmethod from typing import Dict, Tuple +import numpy + __all__ = ['DAC'] diff --git a/qctoolkit/hardware/program.py b/qctoolkit/hardware/program.py index a5f3dca75..365ed8423 100644 --- a/qctoolkit/hardware/program.py +++ b/qctoolkit/hardware/program.py @@ -6,7 +6,7 @@ import numpy as np -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID from qctoolkit.pulses.instructions import AbstractInstructionBlock, EXECInstruction, REPJInstruction, GOTOInstruction, STOPInstruction, InstructionPointer, CHANInstruction, Waveform from qctoolkit.comparable import Comparable from qctoolkit.utils.tree import Node, is_tree_circular diff --git a/qctoolkit/hardware/setup.py b/qctoolkit/hardware/setup.py index 7da4f1d93..a07a26d82 100644 --- a/qctoolkit/hardware/setup.py +++ b/qctoolkit/hardware/setup.py @@ -4,7 +4,7 @@ from qctoolkit.hardware.awgs.base import AWG from qctoolkit.hardware.program import MultiChannelProgram -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID import numpy as np diff --git a/qctoolkit/pulses/__init__.py b/qctoolkit/pulses/__init__.py index e13185201..253e1c63f 100644 --- a/qctoolkit/pulses/__init__.py +++ b/qctoolkit/pulses/__init__.py @@ -1,12 +1,17 @@ +"""This is the central package for defining pulses. All :class:`~qctoolkit.pulses.pulse_template.PulseTemplate` +subclasses that are final and ready to be used are imported here with their recommended abbreviation as an alias.""" + from qctoolkit.pulses.function_pulse_template import FunctionPulseTemplate as FunctionPT from qctoolkit.pulses.loop_pulse_template import ForLoopPulseTemplate as ForLoopPT from qctoolkit.pulses.multi_channel_pulse_template import AtomicMultiChannelPulseTemplate as AtomicMultiChannelPT -from qctoolkit.pulses.pulse_template_parameter_mapping import MappingTemplate as MappingPT +from qctoolkit.pulses.pulse_template_parameter_mapping import MappingPulseTemplate as MappingPT from qctoolkit.pulses.repetition_pulse_template import RepetitionPulseTemplate as RepetitionPT from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate as SequencePT from qctoolkit.pulses.table_pulse_template import TablePulseTemplate as TablePT +from qctoolkit.pulses.point_pulse_template import PointPulseTemplate as PointPT from qctoolkit.pulses.sequencing import Sequencer __all__ = ["FunctionPT", "ForLoopPT", "AtomicMultiChannelPT", "MappingPT", "RepetitionPT", "SequencePT", "TablePT", - "Sequencer"] + "Sequencer", "PointPT"] + diff --git a/qctoolkit/pulses/conditions.py b/qctoolkit/pulses/conditions.py index c2ba8e9d6..25aaab1ac 100644 --- a/qctoolkit/pulses/conditions.py +++ b/qctoolkit/pulses/conditions.py @@ -11,8 +11,9 @@ from abc import ABCMeta, abstractmethod from typing import Dict, Optional, Callable +from qctoolkit.utils.types import ChannelID from qctoolkit.pulses.parameters import Parameter -from qctoolkit.pulses.sequencing import Sequencer, SequencingElement +from . import sequencing from qctoolkit.pulses.instructions import InstructionBlock, InstructionPointer, Trigger __all__ = ["Condition", "ConditionEvaluationException", "ConditionMissingException", @@ -41,27 +42,27 @@ def requires_stop(self) -> bool: @abstractmethod def build_sequence_loop(self, - delegator: SequencingElement, - body: SequencingElement, - sequencer: Sequencer, + delegator: sequencing.SequencingElement, + body: sequencing.SequencingElement, + sequencer: sequencing.Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: """Translate a looping SequencingElement using this Condition into an instruction sequence for the given instruction block using sequencer and the given parameter sets. Args: - delegator (SequencingElement): The SequencingElement which has delegated the invocation + delegator: The SequencingElement which has delegated the invocation of its build_sequence method to this Condition object. - body (SequencingElement): The SequencingElement representing the loops body. - sequencer (Sequencer): The Sequencer object coordinating the current sequencing process. - parameters (Dict(str -> Parameter): A mapping of parameter names to Parameter objects + body: The SequencingElement representing the loops body. + sequencer: The Sequencer object coordinating the current sequencing process. + parameters: A mapping of parameter names to Parameter objects which will be passed to the loop body. - conditions (Dict(str -> Conditions): A mapping of condition identifier to Condition + conditions: A mapping of condition identifier to Condition objects which will be passed to the loop body. - instruction_block (InstructionBlock): The instruction block into which instructions + instruction_block: The instruction block into which instructions resulting from the translation of this Condition object will be placed. See Also: SequencingElement.build_sequence() @@ -69,31 +70,32 @@ def build_sequence_loop(self, @abstractmethod def build_sequence_branch(self, - delegator: SequencingElement, - if_branch: SequencingElement, - else_branch: SequencingElement, - sequencer: Sequencer, + delegator: sequencing.SequencingElement, + if_branch: sequencing.SequencingElement, + else_branch: sequencing.SequencingElement, + sequencer: sequencing.Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, 'Condition'], measurement_mapping: Dict[str, str], channel_mapping: Dict['ChannelID', 'ChannelID'], instruction_block: InstructionBlock) -> None: """Translate a branching SequencingElement using this Condition into an instruction sequence - for the given instruction block using sequencer and the given parameter sets. + for the given instruction block using sequencer and the given parameter sets. + + Args: + delegator: The SequencingElement which has delegated the invocation + of its build_sequence method to this Condition object. + if_branch: The SequencingElement representing the branch executed + if the condition holds. + else_branch: The SequencingElement representing the branch executed + if the condition does not hold. + parameters: A mapping of parameter names to Parameter objects + which will be passed to the loop body. + conditions: A mapping of condition identifier to Condition + objects which will be passed to the loop body. + instruction_block: The instruction block into which instructions + resulting from the translation of this Condition object will be placed. - Args: - delegator (SequencingElement): The SequencingElement which has delegated the invocation - of its build_sequence method to this Condition object. - if_branch (SequencingElement): The SequencingElement representing the branch executed - if the condition holds. - else_branch (SequencingElement): The SequencingElement representing the branch executed - if the condition does not hold. - parameters (Dict(str -> Parameter): A mapping of parameter names to Parameter objects - which will be passed to the loop body. - conditions (Dict(str -> Conditions): A mapping of condition identifier to Condition - objects which will be passed to the loop body. - instruction_block (InstructionBlock): The instruction block into which instructions - resulting from the translation of this Condition object will be placed. See Also: SequencingElement.build_sequence() """ @@ -113,21 +115,21 @@ def __init__(self, trigger: Trigger) -> None: """Create a new HardwareCondition instance. Args: - trigger (Trigger): The trigger handle of the corresponding hardware device.""" + trigger: The trigger handle of the corresponding hardware device.""" super().__init__() - self.__trigger = trigger # type: Trigger + self.__trigger = trigger # type: Trigger def requires_stop(self) -> bool: return False def build_sequence_loop(self, - delegator: SequencingElement, - body: SequencingElement, - sequencer: Sequencer, + delegator: sequencing.SequencingElement, + body: sequencing.SequencingElement, + sequencer: sequencing.Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: body_block = InstructionBlock() body_block.return_ip = InstructionPointer(instruction_block, @@ -139,14 +141,14 @@ def build_sequence_loop(self, target_block=body_block) def build_sequence_branch(self, - delegator: SequencingElement, - if_branch: SequencingElement, - else_branch: SequencingElement, - sequencer: Sequencer, + delegator: sequencing.SequencingElement, + if_branch: sequencing.SequencingElement, + else_branch: sequencing.SequencingElement, + sequencer: sequencing.Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: if_block = InstructionBlock() else_block = InstructionBlock() @@ -201,13 +203,13 @@ def requires_stop(self) -> bool: return evaluation_result is None def build_sequence_loop(self, - delegator: SequencingElement, - body: SequencingElement, - sequencer: Sequencer, + delegator: sequencing.SequencingElement, + body: sequencing.SequencingElement, + sequencer: sequencing.Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: evaluation_result = self.__callback(self.__loop_iteration) @@ -223,14 +225,14 @@ def build_sequence_loop(self, self.__loop_iteration += 1 # next time, evaluate for next iteration def build_sequence_branch(self, - delegator: SequencingElement, - if_branch: SequencingElement, - else_branch: SequencingElement, - sequencer: Sequencer, + delegator: sequencing.SequencingElement, + if_branch: sequencing.SequencingElement, + else_branch: sequencing.SequencingElement, + sequencer: sequencing.Sequencer, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: evaluation_result = self.__callback(self.__loop_iteration) diff --git a/qctoolkit/pulses/function_pulse_template.py b/qctoolkit/pulses/function_pulse_template.py index 73a7820db..99b7faa29 100644 --- a/qctoolkit/pulses/function_pulse_template.py +++ b/qctoolkit/pulses/function_pulse_template.py @@ -15,8 +15,9 @@ from qctoolkit.expressions import Expression from qctoolkit.serialization import Serializer -from qctoolkit import MeasurementWindow, ChannelID -from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer +from qctoolkit.utils.types import MeasurementWindow, ChannelID +from qctoolkit.pulses.conditions import Condition +from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer, ParameterConstraint from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.measurement import MeasurementDefiner @@ -44,19 +45,21 @@ def __init__(self, identifier: Optional[str] = None, *, measurements: Optional[List[MeasurementDeclaration]]=None, - parameter_constraints: Optional[List[Union[str, 'ParameterConstraint']]]=None) -> None: - """Create a new FunctionPulseTemplate instance. - + parameter_constraints: Optional[List[Union[str, ParameterConstraint]]]=None) -> None: + """ Args: - expression (str or Expression): The function represented by this FunctionPulseTemplate + expression: The function represented by this FunctionPulseTemplate as a mathematical expression where 't' denotes the time variable and other variables will be parameters of the pulse. - duration_expression (str or Expression): A mathematical expression which reliably + duration_expression: A mathematical expression which reliably computes the duration of an instantiation of this FunctionPulseTemplate from provided parameter values. - measurement (bool): True, if this FunctionPulseTemplate shall define a measurement - window. (optional, default = False) - identifier (str): A unique identifier for use in serialization. (optional) + channel: The channel this pulse template is defined on. + identifier: A unique identifier for use in serialization. + measurements: A list of measurement declarations forwarded to the + :class:`~qctoolkit.pulses.measurement.MeasurementDefiner` superclass + parameter_constraints: A list of parameter constraints forwarded to the + :class:`~`qctoolkit.pulses.measurement.ParameterConstrainer superclass """ AtomicPulseTemplate.__init__(self, identifier=identifier) MeasurementDefiner.__init__(self, measurements=measurements) @@ -89,7 +92,7 @@ def is_interruptable(self) -> bool: return False @property - def defined_channels(self) -> Set['ChannelID']: + def defined_channels(self) -> Set[ChannelID]: return {self.__channel} @property @@ -115,7 +118,7 @@ def build_waveform(self, def requires_stop(self, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition']) -> bool: + conditions: Dict[str, Condition]) -> bool: return any( parameters[name].requires_stop for name in parameters.keys() if (name in self.parameter_names) @@ -131,7 +134,7 @@ def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: ) @staticmethod - def deserialize(serializer: 'Serializer', + def deserialize(serializer: Serializer, expression: str, duration_expression: str, channel: 'ChannelID', @@ -158,11 +161,11 @@ def __init__(self, expression: Expression, """Creates a new FunctionWaveform instance. Args: - expression (Expression): The function represented by this FunctionWaveform - as a mathematical expression where 't' denotes the time variable. It may not have other variables - duration (float): A mathematical expression which reliably - computes the duration of this FunctionPulseTemplate. - measurement_windows (List): A list of measurement windows + expression: The function represented by this FunctionWaveform + as a mathematical expression where 't' denotes the time variable. It must not have other variables + duration: The duration of the waveform + measurement_windows: A list of measurement windows + channel: The channel this waveform is played on """ super().__init__() if set(expression.variables) - set('t'): diff --git a/qctoolkit/pulses/instructions.py b/qctoolkit/pulses/instructions.py index f4eee6986..a5aaf5b3d 100644 --- a/qctoolkit/pulses/instructions.py +++ b/qctoolkit/pulses/instructions.py @@ -22,9 +22,8 @@ import numpy -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID, MeasurementWindow from qctoolkit.comparable import Comparable -from qctoolkit import MeasurementWindow __all__ = ["Waveform", "Trigger", "InstructionPointer", "Instruction", "CJMPInstruction", "EXECInstruction", @@ -54,11 +53,11 @@ def unsafe_sample(self, monotonously increasing and lie in the range of [0, waveform.duration] Args: - numpy.ndarray sample_times: Times at which this Waveform will be sampled. - numpy.ndarray output_array: Has to be either None or an array of the same size and type as sample_times. If - not None, the sampled values will be written here and this array will be returned + sample_times: Times at which this Waveform will be sampled. + output_array: Has to be either None or an array of the same size and type as sample_times. If + not None, the sampled values will be written here and this array will be returned Result: - numpy.ndarray of the sampled values of this Waveform at the provided sample times. Has the same number of + The sampled values of this Waveform at the provided sample times. Has the same number of elements as sample_times. """ @@ -70,12 +69,12 @@ def get_sampled(self, unsafe_sample expects and caches the result to save memory. Args/Result: - numpy.ndarray sample_times: Times at which this Waveform will be sampled. - numpy.ndarray output_array: Has to be either None or an array of the same size and type as sample_times. - If None, a new array will be created and cached to save memory. - If not None, the sampled values will be written here and this array will be returned. + sample_times: Times at which this Waveform will be sampled. + output_array: Has to be either None or an array of the same size and type as sample_times. + - If None, a new array will be created and cached to save memory. + - If not None, the sampled values will be written here and this array will be returned. Result: - A numpy.ndarray of the sampled values of this Waveform at the provided sample times. + The sampled values of this Waveform at the provided sample times. """ if numpy.any(sample_times[:-1] >= sample_times[1:]): raise ValueError('The sample times are not monotonously increasing') @@ -102,22 +101,29 @@ def get_sampled(self, @abstractproperty def defined_channels(self) -> Set[ChannelID]: - """""" + """The channels this waveform should played on. Use + :func:`~qctoolkit.pulses.instructions.get_measurement_windows` to get a waveform for a subset of these.""" @abstractmethod def get_measurement_windows(self) -> Iterable[MeasurementWindow]: - """This function will in must cases return a generator to fill the measurement windows in a more efficient + """This function will in most cases return a generator to fill the measurement windows in a more efficient data structure like a dict.""" @abstractmethod def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': - """""" + """Unsafe version of :func:`~qctoolkit.pulses.instructions.get_measurement_windows`.""" def get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': - """ + """Get a waveform that only describes the channels contained in `channels`. + + Args: + channels: A channel set the return value should confine to. - :param channels: - :return: + Raises: + KeyError: If `channels` is not a subset of the waveform's defined channels. + + Returns: + A waveform with waveform.defined_channels == `channels` """ if not channels <= self.defined_channels: raise KeyError('Channels not defined on waveform: {}'.format(channels)) @@ -151,11 +157,11 @@ def __init__(self, block: 'AbstractInstructionBlock', offset: int=0) -> None: """Create a new InstructionPointer instance. Args: - block (AbstractInstructionBlock): The instruction block the referenced instruction + block: The instruction block the referenced instruction resides in. - offset (int): The position/offset of the referenced instruction in its block. + offset: The position/offset of the referenced instruction in its block. Raises: - ValueError, if offset is negative + ValueError: If offset is negative """ super().__init__() if offset < 0: @@ -276,17 +282,17 @@ def __str__(self) -> str: class EXECInstruction(Instruction): """An instruction to execute/play back a waveform.""" - def __init__(self, waveform: 'MultiChannelWaveform') -> None: + def __init__(self, waveform: Waveform) -> None: """Create a new EXECInstruction object. Args: - waveform (MultiChannelWaveform): The waveform that will be executed by this instruction. + waveform: The waveform that will be executed by this instruction. """ super().__init__() self.waveform = waveform @property - def compare_key(self) -> Any: + def compare_key(self) -> Waveform: return self.waveform def __str__(self) -> str: @@ -442,7 +448,7 @@ def add_instruction(self, instruction: Instruction) -> None: """ self.__instruction_list.append(instruction) - def add_instruction_exec(self, waveform: 'MultiChannelWaveform') -> None: + def add_instruction_exec(self, waveform: Waveform) -> None: """Create and append a new EXECInstruction object for the given waveform at the end of this instruction block. diff --git a/qctoolkit/pulses/loop_pulse_template.py b/qctoolkit/pulses/loop_pulse_template.py index f19a1f7f1..19202ad45 100644 --- a/qctoolkit/pulses/loop_pulse_template.py +++ b/qctoolkit/pulses/loop_pulse_template.py @@ -15,11 +15,11 @@ from qctoolkit.pulses.sequencing import Sequencer from qctoolkit.pulses.sequence_pulse_template import SequenceWaveform as ForLoopWaveform -__all__ = ['WhileLoopPulseTemplate', 'ConditionMissingException'] +__all__ = ['ForLoopPulseTemplate', 'LoopPulseTemplate', 'LoopIndexNotUsedException'] class LoopPulseTemplate(PulseTemplate): - """Base class for loop based pulse templates""" + """Base class for loop based pulse templates. This class is still abstract and cannot be instantiated.""" def __init__(self, body: PulseTemplate, identifier: Optional[str]=None): super().__init__(identifier=identifier) self.__body = body @@ -42,8 +42,17 @@ def is_interruptable(self): class ParametrizedRange: - """Parametrized range """ + """Like the builtin python range but with parameters.""" def __init__(self, *args, **kwargs): + """Positional and keyword arguments cannot be mixed. + + Args: + *args: Interpreted as ``(start, )`` or ``(start, stop[, step])`` + **kwargs: Expected to contain ``start``, ``stop`` and ``step`` + Raises: + TypeError: If positional and keyword arguments are mixed + KeyError: If keyword arguments but one of ``start``, ``stop`` or ``step`` is missing + """ if args and kwargs: raise TypeError('ParametrizedRange only takes either positional or keyword arguments') elif kwargs: @@ -81,6 +90,9 @@ def parameter_names(self) -> Set[str]: class ForLoopPulseTemplate(LoopPulseTemplate): + """This pulse template allows looping through an parametrized integer range and provides the loop index as a + parameter to the body. If you do not need the index in the pulse template, consider using + :class:`~qctoolkit.pulses.repetition_pulse_template.RepetitionPulseTemplate`""" def __init__(self, body: PulseTemplate, loop_index: str, @@ -91,6 +103,13 @@ def __init__(self, Tuple[Any, Any, Any], ParametrizedRange], identifier: Optional[str]=None): + """ + Args: + body: The loop body. It is expected to have `loop_index` as an parameter + loop_index: Loop index of the for loop + loop_range: Range to loop through + identifier: Used for serialization + """ super().__init__(body=body, identifier=identifier) if isinstance(loop_range, ParametrizedRange): @@ -151,7 +170,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: for local_parameters in self._body_parameter_generator(parameters, forward=False): sequencer.push(self.body, @@ -191,10 +210,11 @@ def deserialize(serializer: Serializer, loop_index=loop_index) + class WhileLoopPulseTemplate(LoopPulseTemplate): """Conditional looping in a pulse. - A LoopPulseTemplate is a PulseTemplate which body (subtemplate) is repeated + A LoopPulseTemplate is a PulseTemplate whose body is repeated during execution as long as a certain condition holds. """ @@ -242,7 +262,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: self.__obtain_condition_object(conditions).build_sequence_loop(self, self.body, diff --git a/qctoolkit/pulses/measurement.py b/qctoolkit/pulses/measurement.py index c72fe425a..aa147ab71 100644 --- a/qctoolkit/pulses/measurement.py +++ b/qctoolkit/pulses/measurement.py @@ -1,9 +1,8 @@ -from typing import Optional, List, Tuple, Union, Dict, Set, Iterable +from typing import Optional, List, Tuple, Union, Dict, Set from numbers import Real import itertools from qctoolkit.expressions import Expression -from qctoolkit.pulses.parameters import ParameterConstraint, ParameterConstraintViolation, Parameter MeasurementDeclaration = Tuple[str, Union[Expression, str, Real], Union[Expression, str, Real]] diff --git a/qctoolkit/pulses/multi_channel_pulse_template.py b/qctoolkit/pulses/multi_channel_pulse_template.py index d44405807..286756059 100644 --- a/qctoolkit/pulses/multi_channel_pulse_template.py +++ b/qctoolkit/pulses/multi_channel_pulse_template.py @@ -7,7 +7,7 @@ - MultiChannelWaveform: A waveform defined for several channels by combining waveforms """ -from typing import Dict, List, Optional, Any, Iterable, Union, Set +from typing import Dict, List, Optional, Any, Iterable, Union, Set, Sequence import itertools import numbers @@ -16,10 +16,10 @@ from qctoolkit.serialization import Serializer -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.utils.types import MeasurementWindow, ChannelID from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.pulse_template import PulseTemplate, AtomicPulseTemplate -from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingTemplate,\ +from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, MappingPulseTemplate,\ MissingParameterDeclarationException, MappingTuple from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer from qctoolkit.pulses.conditions import Condition @@ -140,24 +140,24 @@ def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform' class AtomicMultiChannelPulseTemplate(AtomicPulseTemplate, ParameterConstrainer): def __init__(self, - *subtemplates: Union[AtomicPulseTemplate, MappingTuple, MappingTemplate], + *subtemplates: Union[AtomicPulseTemplate, MappingTuple, MappingPulseTemplate], external_parameters: Optional[Set[str]]=None, identifier: Optional[str]=None, parameter_constraints: Optional[List]=None) -> None: AtomicPulseTemplate.__init__(self, identifier=identifier) ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) - self._subtemplates = [st if isinstance(st, PulseTemplate) else MappingTemplate.from_tuple(st) for st in + self._subtemplates = [st if isinstance(st, PulseTemplate) else MappingPulseTemplate.from_tuple(st) for st in subtemplates] for subtemplate in self._subtemplates: if isinstance(subtemplate, AtomicPulseTemplate): continue - elif isinstance(subtemplate, MappingTemplate): + elif isinstance(subtemplate, MappingPulseTemplate): if isinstance(subtemplate.template, AtomicPulseTemplate): continue else: - raise TypeError('Non atomic subtemplate of MappingTemplate: {}'.format(subtemplate.template)) + raise TypeError('Non atomic subtemplate of MappingPulseTemplate: {}'.format(subtemplate.template)) else: raise TypeError('Non atomic subtemplate: {}'.format(subtemplate)) @@ -186,7 +186,7 @@ def __init__(self, raise MissingParameterDeclarationException(self, missing.pop()) remaining -= self.constrained_parameters if remaining: - raise MissingMappingException(subtemplate, remaining.pop()) + raise MissingMappingException(self, remaining.pop()) duration = self._subtemplates[0].duration for subtemplate in self._subtemplates[1:]: @@ -205,7 +205,7 @@ def parameter_names(self) -> Set[str]: return set.union(*(st.parameter_names for st in self._subtemplates)) | self.constrained_parameters @property - def subtemplates(self) -> List[AtomicPulseTemplate]: + def subtemplates(self) -> Sequence[AtomicPulseTemplate]: return self._subtemplates @property @@ -253,4 +253,4 @@ def __init__(self, obj1, obj2, intersect_set): self.obj2 = obj2 def __str__(self) -> str: - return 'Channels {chs} defined in {} and {}'.format(self.intersect_set, self.obj1, self.obj2) \ No newline at end of file + return 'Channels {chs} defined in {o1} and {o2}'.format(chs=self.intersect_set, o1=self.obj1, o2=self.obj2) \ No newline at end of file diff --git a/qctoolkit/pulses/parameters.py b/qctoolkit/pulses/parameters.py index 052ee7e8b..80ade52b9 100644 --- a/qctoolkit/pulses/parameters.py +++ b/qctoolkit/pulses/parameters.py @@ -4,7 +4,6 @@ - Parameter: A base class representing a single pulse parameter. - ConstantParameter: A single parameter with a constant value. - MappedParameter: A parameter whose value is mathematically computed from another parameter. - - ParameterDeclaration: The declaration of a parameter within a pulse template. - ParameterNotProvidedException. - ParameterValueIllegalException. """ @@ -19,7 +18,7 @@ from qctoolkit.expressions import Expression from qctoolkit.comparable import Comparable -__all__ = ["make_parameter", "ParameterDict", "Parameter", "ConstantParameter", +__all__ = ["Parameter", "ConstantParameter", "ParameterNotProvidedException", "ParameterConstraintViolation"] @@ -132,7 +131,7 @@ def get_value(self) -> Real: @property def requires_stop(self) -> bool: try: - return any([p.requires_stop for p in self.__collect_dependencies().values()]) + return any(p.requires_stop for p in self.__collect_dependencies().values()) except: raise @@ -151,6 +150,7 @@ def deserialize(serializer: Serializer, expression: str) -> 'MappedParameter': class ParameterConstraint(Comparable): + """A parameter constraint like 't_2 < 2.7' that can be used to set bounds to parameters.""" def __init__(self, relation: Union[str, sympy.Expr]): super().__init__() if isinstance(relation, str) and '==' in relation: @@ -187,6 +187,7 @@ def __str__(self) -> str: class ParameterConstrainer: + """A class that implements the testing of parameter constraints. It is used by the subclassing pulse templates.""" def __init__(self, *, parameter_constraints: Optional[Iterable[Union[str, ParameterConstraint]]]) -> None: if parameter_constraints is None: @@ -201,6 +202,10 @@ def parameter_constraints(self) -> List[ParameterConstraint]: return self._parameter_constraints def validate_parameter_constraints(self, parameters: [str, Union[Parameter, Real]]) -> None: + """Raises a ParameterConstraintViolation exception if one of the constraints is violated. + :param parameters: These parameters are checked. + :return: + """ for constraint in self._parameter_constraints: constraint_parameters = {k: v.get_value() if isinstance(v, Parameter) else v for k, v in parameters.items()} if not constraint.is_fulfilled(constraint_parameters): diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index 00f9b7b6f..5fbcc1d9e 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -7,11 +7,11 @@ - plot: Plot a pulse using matplotlib. """ -from typing import Dict, Tuple +from typing import Dict, Tuple, Any import numpy as np -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.parameters import Parameter from qctoolkit.pulses.sequencing import Sequencer @@ -73,7 +73,7 @@ def get_waveform_generator(instruction_block): def plot(pulse: PulseTemplate, parameters: Dict[str, Parameter]=None, - sample_rate: int=10) -> 'plt.Figure': # pragma: no cover + sample_rate: int=10) -> Any: # pragma: no cover """Plot a pulse using matplotlib. The given pulse will first be sequenced using the Sequencer class. The resulting @@ -81,10 +81,10 @@ def plot(pulse: PulseTemplate, arrays are then plotted in a matplotlib figure. Args: - pulse (PulseTemplate): The pulse to be plotted. - parameters (Dict(str -> Parameter)): An optional mapping of parameter names to Parameter + pulse: The pulse to be plotted. + parameters: An optional mapping of parameter names to Parameter objects. - sample_rate (int): The rate with which the waveforms are sampled for the plot in + sample_rate: The rate with which the waveforms are sampled for the plot in samples per time unit. (default = 10) Returns: matplotlib.pyplot.Figure instance in which the pulse is rendered diff --git a/qctoolkit/pulses/point_pulse_template.py b/qctoolkit/pulses/point_pulse_template.py index e278bb9ad..9f29ebfd8 100644 --- a/qctoolkit/pulses/point_pulse_template.py +++ b/qctoolkit/pulses/point_pulse_template.py @@ -5,8 +5,10 @@ import numpy as np -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID from qctoolkit.expressions import Expression +from qctoolkit.pulses.conditions import Condition +from qctoolkit.pulses.instructions import Waveform from qctoolkit.pulses.parameters import Parameter, ParameterNotProvidedException, ParameterConstraint,\ ParameterConstrainer from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration @@ -60,7 +62,7 @@ def defined_channels(self) -> Set[ChannelID]: def build_waveform(self, parameters: Dict[str, Real], measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional[Waveform]: self.validate_parameter_constraints(parameters) if self.duration.evaluate_numeric(**parameters) == 0: @@ -125,7 +127,7 @@ def parameter_names(self) -> Set[str]: def requires_stop(self, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition']) -> bool: + conditions: Dict[str, Condition]) -> bool: try: return any( parameters[name].requires_stop diff --git a/qctoolkit/pulses/pulse_template.py b/qctoolkit/pulses/pulse_template.py index 70a48d546..f6c4cb655 100644 --- a/qctoolkit/pulses/pulse_template.py +++ b/qctoolkit/pulses/pulse_template.py @@ -11,13 +11,14 @@ import itertools from numbers import Real -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID, DocStringABCMeta from qctoolkit.serialization import Serializable from qctoolkit.expressions import Expression +from qctoolkit.pulses.conditions import Condition from qctoolkit.pulses.parameters import Parameter -from qctoolkit.pulses.sequencing import SequencingElement, InstructionBlock - +from qctoolkit.pulses.sequencing import Sequencer, SequencingElement, InstructionBlock +from qctoolkit.pulses.instructions import Waveform __all__ = ["PulseTemplate", "AtomicPulseTemplate", "DoubleParameterNameException"] @@ -27,7 +28,7 @@ Union[Real, str, Expression]] -class PulseTemplate(Serializable, SequencingElement, metaclass=ABCMeta): +class PulseTemplate(Serializable, SequencingElement, metaclass=DocStringABCMeta): """A PulseTemplate represents the parametrized general structure of a pulse. A PulseTemplate described a pulse in an abstract way: It defines the structure of a pulse @@ -48,7 +49,7 @@ def parameter_names(self) -> Set[str]: @abstractproperty def measurement_names(self) -> Set[str]: - """The set of measurement identifiers in this pulse template""" + """The set of measurement identifiers in this pulse template.""" @abstractproperty def is_interruptable(self) -> bool: @@ -58,18 +59,20 @@ def is_interruptable(self) -> bool: @property @abstractmethod def duration(self) -> Expression: - """An expression for the duration""" + """An expression for the duration of this PulseTemplate.""" - @abstractproperty + @property + @abstractmethod def defined_channels(self) -> Set['ChannelID']: """Returns the number of hardware output channels this PulseTemplate defines.""" @property def num_channels(self) -> int: + """The number of channels this PulseTemplate defines""" return len(self.defined_channels) - def __matmul__(self, other: 'PulseTemplate') -> 'SequencePulseTemplate': - """This method enables us to use the @-operator (intended for matrix multiplication) for + def __matmul__(self, other: 'PulseTemplate') -> 'PulseTemplate': + """This method enables using the @-operator (intended for matrix multiplication) for concatenating pulses. If one of the pulses is a SequencePulseTemplate the other pulse gets merged into it""" from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate @@ -103,11 +106,11 @@ def atomicity(self) -> bool: return True def build_sequence(self, - sequencer: 'Sequencer', + sequencer: Sequencer, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition'], + conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: parameters = {parameter_name: parameter_value.get_value() for parameter_name, parameter_value in parameters.items() @@ -122,7 +125,7 @@ def build_sequence(self, def build_waveform(self, parameters: Dict[str, Real], measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> Optional['Waveform']: + channel_mapping: Dict[ChannelID, ChannelID]) -> Optional[Waveform]: """Translate this PulseTemplate into a waveform according to the given parameters. Args: diff --git a/qctoolkit/pulses/pulse_template_parameter_mapping.py b/qctoolkit/pulses/pulse_template_parameter_mapping.py index a97c70d8c..9ee22ed0b 100644 --- a/qctoolkit/pulses/pulse_template_parameter_mapping.py +++ b/qctoolkit/pulses/pulse_template_parameter_mapping.py @@ -1,18 +1,21 @@ """This module defines PulseTemplateParameterMapping, a helper class for pulse templates that offer mapping of parameters of subtemplates.""" -from typing import Optional, Set, Dict, Union, Iterable, List, Any, Tuple +from typing import Optional, Set, Dict, Union, List, Any, Tuple import itertools import numbers -from qctoolkit import ChannelID +from qctoolkit.utils.types import ChannelID from qctoolkit.expressions import Expression from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.parameters import Parameter, MappedParameter, ParameterNotProvidedException, ParameterConstrainer - +from qctoolkit.pulses.sequencing import Sequencer +from qctoolkit.pulses.instructions import InstructionBlock, Waveform +from qctoolkit.pulses.conditions import Condition +from qctoolkit.serialization import Serializer __all__ = [ - "MappingTemplate", + "MappingPulseTemplate", "MissingMappingException", "MissingParameterDeclarationException", "UnnecessaryMappingException", @@ -25,7 +28,10 @@ Tuple[PulseTemplate, Dict, Dict, Dict]] -class MappingTemplate(PulseTemplate, ParameterConstrainer): +class MappingPulseTemplate(PulseTemplate, ParameterConstrainer): + """This class can be used to remap parameters, the names of measurement windows and the names of channels. Besides + the standard constructor, there is a static member function from_tuple for convenience. The class also allows + constraining parameters by deriving from ParameterConstrainer""" def __init__(self, template: PulseTemplate, *, identifier: Optional[str]=None, parameter_mapping: Optional[Dict[str, str]]=None, @@ -33,12 +39,22 @@ def __init__(self, template: PulseTemplate, *, channel_mapping: Optional[Dict[ChannelID, ChannelID]] = None, parameter_constraints: Optional[List[str]]=None, allow_partial_parameter_mapping=False): - """TODO mention nested mappings + """Standard constructor for the MappingPulseTemplate. + + Mappings that are not specified are defaulted to identity mappings. Channels and measurement names of the + encapsulated template can be mapped partially by default. F.i. if channel_mapping only contains one of two + channels the other channel name is mapped to itself. + However, if a parameter mapping is specified and one or more parameters are not mapped a MissingMappingException + is raised. To allow partial mappings and enable the same behaviour as for the channel and measurement name + mapping allow_partial_parameter_mapping must be set to True. + Furthermore parameter constrains can be specified. - :param template: + :param template: The encapsulated pulse template whose parameters, measurement names and channels are mapped :param parameter_mapping: if not none, mappings for all parameters must be specified :param measurement_mapping: mappings for other measurement names are inserted :param channel_mapping: mappings for other channels are auto inserted + :param parameter_constraints: + :param allow_partial_parameter_mapping: """ PulseTemplate.__init__(self, identifier=identifier) ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) @@ -76,7 +92,7 @@ def __init__(self, template: PulseTemplate, *, channel_mapping = dict(itertools.chain(((name, name) for name in missing_channel_mappings), channel_mapping.items())) - if isinstance(template, MappingTemplate) and template.identifier is None: + if isinstance(template, MappingPulseTemplate) and template.identifier is None: # avoid nested mappings parameter_mapping = {p: expr.evaluate_symbolic(parameter_mapping) for p, expr in template.parameter_mapping.items()} @@ -94,8 +110,12 @@ def __init__(self, template: PulseTemplate, *, self.__channel_mapping = channel_mapping @staticmethod - def from_tuple(mapping_tuple: MappingTuple) -> 'MappingTemplate': - """Auto detect what mappings are meant""" + def from_tuple(mapping_tuple: MappingTuple) -> 'MappingPulseTemplate': + """Construct a MappingPulseTemplate from a tuple of mappings. The mappings are automatically assigned to the + mapped elements based on their content. + :param mapping_tuple: A tuple of mappings + :return: Constructed MappingPulseTemplate + """ template, *mappings = mapping_tuple parameter_mapping = None @@ -132,10 +152,10 @@ def from_tuple(mapping_tuple: MappingTuple) -> 'MappingTemplate': channel_mapping = mapping else: raise ValueError('Could not match mapping to mapped objects: {}'.format(mapping)) - return MappingTemplate(template, - parameter_mapping=parameter_mapping, - measurement_mapping=measurement_mapping, - channel_mapping=channel_mapping) + return MappingPulseTemplate(template, + parameter_mapping=parameter_mapping, + measurement_mapping=measurement_mapping, + channel_mapping=channel_mapping) @property def template(self) -> PulseTemplate: @@ -173,7 +193,7 @@ def defined_channels(self) -> Set[ChannelID]: def duration(self) -> Expression: return self.__template.duration.evaluate_symbolic(self.__parameter_mapping) - def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: + def get_serialization_data(self, serializer: Serializer) -> Dict[str, Any]: parameter_mapping_dict = dict((key, str(expression)) for key, expression in self.__parameter_mapping.items()) return dict(template=serializer.dictify(self.template), parameter_mapping=parameter_mapping_dict, @@ -181,10 +201,10 @@ def get_serialization_data(self, serializer: 'Serializer') -> Dict[str, Any]: channel_mapping=self.__channel_mapping) @staticmethod - def deserialize(serializer: 'Serializer', - template: Union[str, Dict[str, Any]], **kwargs) -> 'MappingTemplate': - return MappingTemplate(template=serializer.deserialize(template), - **kwargs) + def deserialize(serializer: Serializer, + template: Union[str, Dict[str, Any]], **kwargs) -> 'MappingPulseTemplate': + return MappingPulseTemplate(template=serializer.deserialize(template), + **kwargs) def map_parameters(self, parameters: Dict[str, Union[Parameter, numbers.Real]]) -> Dict[str, Parameter]: @@ -218,12 +238,12 @@ def get_updated_channel_mapping(self, channel_mapping: Dict[ChannelID, ChannelID return {inner_ch: channel_mapping[outer_ch] for inner_ch, outer_ch in self.__channel_mapping.items()} def build_sequence(self, - sequencer: "Sequencer", + sequencer: Sequencer, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition'], + conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], channel_mapping: Dict[ChannelID, ChannelID], - instruction_block: 'InstructionBlock') -> None: + instruction_block: InstructionBlock) -> None: self.template.build_sequence(sequencer, parameters=self.map_parameters(parameters), conditions=conditions, @@ -234,7 +254,7 @@ def build_sequence(self, def build_waveform(self, parameters: Dict[str, numbers.Real], measurement_mapping: Dict[str, str], - channel_mapping: Dict[ChannelID, ChannelID]) -> 'Waveform': + channel_mapping: Dict[ChannelID, ChannelID]) -> Waveform: """This gets called if the parent is atomic""" return self.template.build_waveform( parameters=self.map_parameters(parameters), @@ -243,7 +263,7 @@ def build_waveform(self, def requires_stop(self, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition']) -> bool: + conditions: Dict[str, Condition]) -> bool: return self.template.requires_stop( self.map_parameters(parameters), conditions diff --git a/qctoolkit/pulses/repetition_pulse_template.py b/qctoolkit/pulses/repetition_pulse_template.py index b10513f3c..ae81029f9 100644 --- a/qctoolkit/pulses/repetition_pulse_template.py +++ b/qctoolkit/pulses/repetition_pulse_template.py @@ -2,12 +2,13 @@ represents the n-times repetition of another PulseTemplate.""" from typing import Dict, List, Set, Optional, Union, Any, Iterable, Tuple +from numbers import Real import numpy as np from qctoolkit.serialization import Serializer -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.utils.types import MeasurementWindow, ChannelID from qctoolkit.expressions import Expression from qctoolkit.utils import checked_int_cast from qctoolkit.pulses.pulse_template import PulseTemplate @@ -24,10 +25,6 @@ class RepetitionWaveform(Waveform): """This class allows putting multiple PulseTemplate together in one waveform on the hardware.""" def __init__(self, body: Waveform, repetition_count: int): - """ - - :param subwaveforms: All waveforms must have the same defined channels - """ self._body = body self._repetition_count = repetition_count if repetition_count < 1 or not isinstance(repetition_count, int): @@ -47,8 +44,8 @@ def unsafe_sample(self, for i in range(self._repetition_count): indices = slice(*np.searchsorted(sample_times, (i*body_duration, (i+1)*body_duration))) self._body.unsafe_sample(channel=channel, - sample_times=sample_times[indices], - output_array=output_array[indices]) + sample_times=sample_times[indices], + output_array=output_array[indices]) return output_array @property @@ -108,7 +105,7 @@ def repetition_count(self) -> Expression: """The amount of repetitions. Either a constant integer or a ParameterDeclaration object.""" return self._repetition_count - def get_repetition_count_value(self, parameters: Dict[str, 'Real']) -> int: + def get_repetition_count_value(self, parameters: Dict[str, Real]) -> int: value = self._repetition_count.evaluate_numeric(**parameters) try: return checked_int_cast(value) @@ -136,7 +133,7 @@ def build_sequence(self, parameters: Dict[str, Parameter], conditions: Dict[str, Condition], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: self.validate_parameter_constraints(parameters=parameters) @@ -169,7 +166,7 @@ def deserialize(serializer: Serializer, repetition_count: Union[str, int], body: Dict[str, Any], parameter_constraints: List[str], - identifier: Optional[str]=None) -> 'Serializable': + identifier: Optional[str]=None) -> 'RepetitionPulseTemplate': body = serializer.deserialize(body) return RepetitionPulseTemplate(body, repetition_count, identifier=identifier, parameter_constraints=parameter_constraints) diff --git a/qctoolkit/pulses/sequence_pulse_template.py b/qctoolkit/pulses/sequence_pulse_template.py index 72ade7114..242aa586a 100644 --- a/qctoolkit/pulses/sequence_pulse_template.py +++ b/qctoolkit/pulses/sequence_pulse_template.py @@ -7,13 +7,13 @@ from qctoolkit.serialization import Serializer -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.utils.types import MeasurementWindow, ChannelID from qctoolkit.pulses.pulse_template import PulseTemplate from qctoolkit.pulses.parameters import Parameter, ParameterConstrainer from qctoolkit.pulses.sequencing import InstructionBlock, Sequencer from qctoolkit.pulses.conditions import Condition from qctoolkit.pulses.pulse_template_parameter_mapping import \ - MissingMappingException, MappingTemplate, MissingParameterDeclarationException, MappingTuple + MissingMappingException, MappingPulseTemplate, MissingParameterDeclarationException, MappingTuple from qctoolkit.pulses.instructions import Waveform from qctoolkit.expressions import Expression @@ -35,13 +35,13 @@ def __init__(self, subwaveforms: List[Waveform]): def flattened_sub_waveforms(): for sub_waveform in subwaveforms: if isinstance(sub_waveform, SequenceWaveform): - yield from sub_waveform.__sequenced_waveforms + yield from sub_waveform._sequenced_waveforms else: yield sub_waveform - self.__sequenced_waveforms = tuple(flattened_sub_waveforms()) - self.__duration = sum(waveform.duration for waveform in self.__sequenced_waveforms) - if not all(waveform.defined_channels == self.defined_channels for waveform in self.__sequenced_waveforms[1:]): + self._sequenced_waveforms = tuple(flattened_sub_waveforms()) + self._duration = sum(waveform.duration for waveform in self._sequenced_waveforms) + if not all(waveform.defined_channels == self.defined_channels for waveform in self._sequenced_waveforms[1:]): raise ValueError( "SequenceWaveform cannot be constructed from waveforms of different" "defined channels." @@ -49,7 +49,7 @@ def flattened_sub_waveforms(): @property def defined_channels(self) -> Set[ChannelID]: - return self.__sequenced_waveforms[0].defined_channels + return self._sequenced_waveforms[0].defined_channels def unsafe_sample(self, channel: ChannelID, @@ -58,7 +58,7 @@ def unsafe_sample(self, if output_array is None: output_array = np.empty(len(sample_times)) time = 0 - for subwaveform in self.__sequenced_waveforms: + for subwaveform in self._sequenced_waveforms: # before you change anything here, make sure to understand the difference between basic and advanced # indexing in numpy and their copy/reference behaviour indices = slice(*np.searchsorted(sample_times, (time, time+subwaveform.duration), 'left')) @@ -70,11 +70,11 @@ def unsafe_sample(self, @property def compare_key(self) -> Tuple[Waveform]: - return self.__sequenced_waveforms + return self._sequenced_waveforms @property def duration(self) -> float: - return self.__duration + return self._duration def get_measurement_windows(self) -> Iterable[MeasurementWindow]: def updated_measurement_window_generator(sequenced_waveforms): @@ -83,12 +83,12 @@ def updated_measurement_window_generator(sequenced_waveforms): for (name, begin, length) in sub_waveform.get_measurement_windows(): yield (name, begin+offset, length) offset += sub_waveform.duration - return updated_measurement_window_generator(self.__sequenced_waveforms) + return updated_measurement_window_generator(self._sequenced_waveforms) def unsafe_get_subset_for_channels(self, channels: Set[ChannelID]) -> 'Waveform': return SequenceWaveform( sub_waveform.unsafe_get_subset_for_channels(channels & sub_waveform.defined_channels) - for sub_waveform in self.__sequenced_waveforms if sub_waveform.defined_channels & channels) + for sub_waveform in self._sequenced_waveforms if sub_waveform.defined_channels & channels) class SequencePulseTemplate(PulseTemplate, ParameterConstrainer): @@ -138,10 +138,9 @@ def __init__(self, PulseTemplate.__init__(self, identifier=identifier) ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) - self.__subtemplates = [MappingTemplate.from_tuple(st) if isinstance(st, tuple) else st + self.__subtemplates = [MappingPulseTemplate.from_tuple(st) if isinstance(st, tuple) else st for st in subtemplates] - # check that all subtemplates live on the same channels defined_channels = self.__subtemplates[0].defined_channels for subtemplate in self.__subtemplates[1:]: @@ -168,7 +167,7 @@ def parameter_names(self) -> Set[str]: return set.union(*(st.parameter_names for st in self.__subtemplates)) @property - def subtemplates(self) -> List[MappingTemplate]: + def subtemplates(self) -> List[MappingPulseTemplate]: return self.__subtemplates @property diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index ed35b89c5..47814cbd3 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -7,11 +7,14 @@ - Sequencer: Controller of the sequencing/translation process. """ -from abc import ABCMeta, abstractmethod, abstractproperty -from typing import Tuple, Dict, Union, Optional +from abc import ABCMeta, abstractmethod +from typing import Tuple, Dict, Union, Optional, List import numbers -from qctoolkit.pulses.instructions import InstructionBlock, ImmutableInstructionBlock +from qctoolkit.utils.types import ChannelID + +from . import conditions +from qctoolkit.pulses.instructions import InstructionBlock, ImmutableInstructionBlock, Waveform from qctoolkit.pulses.parameters import Parameter, ConstantParameter @@ -34,11 +37,11 @@ def atomicity(self) -> bool: @abstractmethod def build_sequence(self, - sequencer: "Sequencer", + sequencer: 'Sequencer', parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition'], + conditions: Dict[str, 'conditions.Condition'], measurement_mapping: Dict[str, str], - channel_mapping: Dict['ChannelID', 'ChannelID'], + channel_mapping: Dict[ChannelID, ChannelID], instruction_block: InstructionBlock) -> None: """Translate this SequencingElement into an instruction sequence for the given instruction_block using sequencer and the given parameter and condition sets. @@ -59,7 +62,7 @@ def build_sequence(self, @abstractmethod def requires_stop(self, parameters: Dict[str, Parameter], - conditions: Dict[str, 'Condition']) -> bool: + conditions: Dict[str, 'conditions.Condition']) -> bool: """Return True if this SequencingElement cannot be translated yet. Sequencer will check requires_stop() before calling build_sequence(). If requires_stop() @@ -106,18 +109,18 @@ class Sequencer: def __init__(self) -> None: """Create a Sequencer.""" super().__init__() - self.__waveforms = dict() #type: Dict[int, Waveform] + self.__waveforms = dict() # type: Dict[int, Waveform] self.__main_block = InstructionBlock() self.__sequencing_stacks = \ - {self.__main_block: []} #type: Dict[InstructionBlock, List[StackElement]] + {self.__main_block: []} # type: Dict[InstructionBlock, List[Sequencer.StackElement]] def push(self, sequencing_element: SequencingElement, parameters: Optional[Dict[str, Union[Parameter, float]]]=None, - conditions: Optional[Dict[str, 'Condition']]=None, + conditions: Optional[Dict[str, 'conditions.Condition']]=None, *, - window_mapping: Optional[Dict[str,str]]=None, - channel_mapping: Optional[Dict['ChannelID','ChannelID']]=None, + window_mapping: Optional[Dict[str, str]]=None, + channel_mapping: Optional[Dict[ChannelID, ChannelID]]=None, target_block: Optional[InstructionBlock]=None) -> None: """Add an element to the translation stack of the target_block with the given set of parameters. @@ -163,9 +166,10 @@ def push(self, if target_block not in self.__sequencing_stacks: self.__sequencing_stacks[target_block] = [] - self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping, channel_mapping)) + self.__sequencing_stacks[target_block].append((sequencing_element, parameters, conditions, window_mapping, + channel_mapping)) - def build(self) -> InstructionBlock: + def build(self) -> ImmutableInstructionBlock: """Start the translation process. Translate all elements currently on the translation stacks into an InstructionBlock hierarchy. diff --git a/qctoolkit/pulses/table_pulse_template.py b/qctoolkit/pulses/table_pulse_template.py index afe514194..780ebfb90 100644 --- a/qctoolkit/pulses/table_pulse_template.py +++ b/qctoolkit/pulses/table_pulse_template.py @@ -7,20 +7,18 @@ declared parameters. """ -from typing import Union, Dict, List, Set, Optional, NamedTuple, Any, Iterable, Tuple, Sequence +from typing import Union, Dict, List, Set, Optional, Any, Iterable, Tuple, Sequence import numbers import itertools import warnings -from operator import itemgetter -from collections import namedtuple import numpy as np import sympy -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.utils.types import MeasurementWindow, ChannelID from qctoolkit.serialization import Serializer from qctoolkit.pulses.parameters import Parameter, \ - ParameterNotProvidedException, ParameterConstraint, ParameterConstraintViolation, ParameterConstrainer + ParameterNotProvidedException, ParameterConstraint, ParameterConstrainer from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration from qctoolkit.pulses.interpolation import InterpolationStrategy, LinearInterpolationStrategy, \ HoldInterpolationStrategy, JumpInterpolationStrategy @@ -174,18 +172,36 @@ def instantiate(self, parameters: Dict[str, numbers.Real]) -> TableWaveformEntry class TablePulseTemplate(AtomicPulseTemplate, ParameterConstrainer, MeasurementDefiner): - """TablePulseTemplate""" + """The TablePulseTemplate class implements pulses described by a table with time, voltage and interpolation strategy + inputs. The interpolation strategy describes how the voltage between the entries is interpolated(see also + InterpolationStrategy.) It can define multiple channels of which each has a separate table. If they do not have the + same length the shorter channels are extended to the longest duration. + + If the time entries of all channels are equal it is more convenient to use the :paramrefPointPulseTemplate`.""" interpolation_strategies = {'linear': LinearInterpolationStrategy(), 'hold': HoldInterpolationStrategy(), 'jump': JumpInterpolationStrategy(), 'default': HoldInterpolationStrategy()} - def __init__(self, entries: Dict[ChannelID, List[EntryInInit]], + def __init__(self, entries: Dict[ChannelID, Sequence[EntryInInit]], identifier: Optional[str]=None, *, parameter_constraints: Optional[List[Union[str, ParameterConstraint]]]=None, measurements: Optional[List[MeasurementDeclaration]]=None, consistency_check=True): + """ + Construct a `TablePulseTemplate` from a dict which maps channels to their entries. By default the consistency + of the provided entries is checked. There are two static functions for convenience construction: from_array and + from_entry_list. + + Args: + entries: A dictionary that maps channel ids to a list of entries. An entry is a + (time, voltage[, interpolation strategy]) tuple or a TableEntry + identifier: Used for serialization + parameter_constraints: Constraint list that is forwarded to the ParameterConstrainer superclass + measurements: Measurement declaration list that is forwarded to the MeasurementDefiner superclass + consistency_check: If True the consistency of the times will be checked on construction as far as possible + """ AtomicPulseTemplate.__init__(self, identifier=identifier) ParameterConstrainer.__init__(self, parameter_constraints=parameter_constraints) MeasurementDefiner.__init__(self, measurements=measurements) diff --git a/qctoolkit/serialization.py b/qctoolkit/serialization.py index f783bbc64..a0550ae15 100644 --- a/qctoolkit/serialization.py +++ b/qctoolkit/serialization.py @@ -16,7 +16,7 @@ import tempfile import json -__all__ = ["StorageBackend", "FilesystemBackend", "ZipFileBackend" "CachingBackend", "Serializable", "Serializer"] +__all__ = ["StorageBackend", "FilesystemBackend", "ZipFileBackend", "CachingBackend", "Serializable", "Serializer"] class StorageBackend(metaclass=ABCMeta): @@ -74,10 +74,10 @@ def __init__(self, root: str='.') -> None: """Create a new FilesystemBackend. Args: - root (str): The path of the directory in which all data files are located. (default: ".", + root: The path of the directory in which all data files are located. (default: ".", i.e. the current directory) Raises: - NotADirectoryError if root is not a valid directory path. + NotADirectoryError: if root is not a valid directory path. """ if not os.path.isdir(root): raise NotADirectoryError() @@ -280,12 +280,12 @@ def deserialize(serializer: 'Serializer', **kwargs) -> 'Serializable': """Reconstruct the Serializable object from a dictionary. Implementation hint: - For greater clarity, implementations of this method should be precise in their return value, - i.e., give their exact class name, and also replace the **kwargs argument by a list of - arguments required, i.e., those returned by get_serialization_data. - If this Serializable contains complex objects which are itself Serializables, their - dictionary representations MUST be converted into objects using serializers deserialize() - method. + For greater clarity, implementations of this method should be precise in their return value, + i.e., give their exact class name, and also replace the **kwargs argument by a list of + arguments required, i.e., those returned by get_serialization_data. + If this Serializable contains complex objects which are itself Serializables, their + dictionary representations MUST be converted into objects using serializers deserialize() + method. Args: serializer (Serializer): A serializer instance used when deserializing subelements. diff --git a/qctoolkit/utils/types.py b/qctoolkit/utils/types.py new file mode 100644 index 000000000..89781dedc --- /dev/null +++ b/qctoolkit/utils/types.py @@ -0,0 +1,36 @@ +import typing +import abc +import inspect + +__all__ = ["MeasurementWindow", "ChannelID"] + +MeasurementWindow = typing.Tuple[str, float, float] +ChannelID = typing.Union[str, int] + + +class DocStringABCMeta(abc.ABCMeta): + def __new__(mcls, classname, bases, cls_dict): + cls = super().__new__(mcls, classname, bases, cls_dict) + + abstract_bases = tuple(base + for base in reversed(inspect.getmro(cls)) + if hasattr(base, '__abstractmethods__'))[:-1] + + for name, member in cls_dict.items(): + if not getattr(member, '__doc__'): + if isinstance(member, property): + member_type = ':py:attr:' + else: + member_type = ':func:' + + for base in abstract_bases: + if name in base.__dict__ and name in base.__abstractmethods__: + base_member = getattr(base, name) + + if member is base_member or not base_member.__doc__: + continue + + base_member_name = '.'.join([base.__module__, base.__qualname__, name]) + member.__doc__ = 'Implements {}`~{}`.'.format(member_type, base_member_name) + break + return cls diff --git a/tests/hardware/tabor_tests.py b/tests/hardware/tabor_tests.py index 8ba0401f2..fcb4cd6a1 100644 --- a/tests/hardware/tabor_tests.py +++ b/tests/hardware/tabor_tests.py @@ -489,7 +489,7 @@ def test_upload(self): my_class = DummyTaborProgramClass(segments=segments, segment_lengths=segment_lengths) sys.modules['qctoolkit.hardware.awgs.tabor'].TaborProgram = my_class try: - program = Loop() + program = Loop(waveform=DummyWaveform(duration=192)) channel_pair = TaborChannelPair(get_instrument(), identifier='asd', channels=(1, 2)) channel_pair._segment_references = segment_references diff --git a/tests/pulses/multi_channel_pulse_template_tests.py b/tests/pulses/multi_channel_pulse_template_tests.py index f76f42fb7..934bd97eb 100644 --- a/tests/pulses/multi_channel_pulse_template_tests.py +++ b/tests/pulses/multi_channel_pulse_template_tests.py @@ -4,7 +4,7 @@ from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ MissingParameterDeclarationException -from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform, MappingTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate +from qctoolkit.pulses.multi_channel_pulse_template import MultiChannelWaveform, MappingPulseTemplate, ChannelMappingException, AtomicMultiChannelPulseTemplate from qctoolkit.pulses.parameters import ParameterConstraint, ParameterConstraintViolation from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate, DummyWaveform @@ -178,7 +178,7 @@ def test_init_empty(self) -> None: def test_non_atomic_subtemplates(self): non_atomic_pt = PulseTemplateStub(duration='t1', defined_channels={'A'}, parameter_names=set()) atomic_pt = DummyPulseTemplate(defined_channels={'B'}, duration='t1') - non_atomic_mapping = MappingTemplate(non_atomic_pt) + non_atomic_mapping = MappingPulseTemplate(non_atomic_pt) with self.assertRaises(TypeError): AtomicMultiChannelPulseTemplate(non_atomic_pt) @@ -243,7 +243,7 @@ def test_mapping_template_pure_conversion(self): def test_mapping_template_mixed_conversion(self): subtemp_args = [ (self.subtemplates[0], self.param_maps[0], self.chan_maps[0]), - MappingTemplate(self.subtemplates[1], parameter_mapping=self.param_maps[1], channel_mapping=self.chan_maps[1]), + MappingPulseTemplate(self.subtemplates[1], parameter_mapping=self.param_maps[1], channel_mapping=self.chan_maps[1]), (self.subtemplates[2], self.param_maps[2], self.chan_maps[2]) ] template = AtomicMultiChannelPulseTemplate(*subtemp_args) diff --git a/tests/pulses/pulse_template_parameter_mapping_tests.py b/tests/pulses/pulse_template_parameter_mapping_tests.py index ba863efb7..0e6557a23 100644 --- a/tests/pulses/pulse_template_parameter_mapping_tests.py +++ b/tests/pulses/pulse_template_parameter_mapping_tests.py @@ -2,7 +2,7 @@ import itertools from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException,\ - UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate,\ + UnnecessaryMappingException, MissingParameterDeclarationException, MappingPulseTemplate,\ AmbiguousMappingException, MappingCollisionException from qctoolkit.expressions import Expression from qctoolkit.pulses.parameters import ParameterNotProvidedException @@ -20,21 +20,21 @@ def test_init_exceptions(self): parameter_mapping = {'foo': 't*k', 'bar': 't*l'} with self.assertRaises(MissingMappingException): - MappingTemplate(template, parameter_mapping={}) + MappingPulseTemplate(template, parameter_mapping={}) with self.assertRaises(MissingMappingException): - MappingTemplate(template, parameter_mapping={'bar': 'kneipe'}) + MappingPulseTemplate(template, parameter_mapping={'bar': 'kneipe'}) with self.assertRaises(UnnecessaryMappingException): - MappingTemplate(template, parameter_mapping=dict(**parameter_mapping, foobar='asd')) + MappingPulseTemplate(template, parameter_mapping=dict(**parameter_mapping, foobar='asd')) with self.assertRaises(UnnecessaryMappingException): - MappingTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=dict(a='b')) + MappingPulseTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=dict(a='b')) with self.assertRaises(UnnecessaryMappingException): - MappingTemplate(template, parameter_mapping=parameter_mapping, channel_mapping=dict(a='b')) + MappingPulseTemplate(template, parameter_mapping=parameter_mapping, channel_mapping=dict(a='b')) with self.assertRaises(TypeError): - MappingTemplate(template, parameter_mapping) + MappingPulseTemplate(template, parameter_mapping) - MappingTemplate(template, parameter_mapping=parameter_mapping) + MappingPulseTemplate(template, parameter_mapping=parameter_mapping) def test_from_tuple_exceptions(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, @@ -42,25 +42,25 @@ def test_from_tuple_exceptions(self): defined_channels={'bar', 'foobar'}) with self.assertRaises(ValueError): - MappingTemplate.from_tuple((template, {'A': 'B'})) + MappingPulseTemplate.from_tuple((template, {'A': 'B'})) with self.assertRaises(AmbiguousMappingException): - MappingTemplate.from_tuple((template, {'foo': 'foo'})) + MappingPulseTemplate.from_tuple((template, {'foo': 'foo'})) with self.assertRaises(AmbiguousMappingException): - MappingTemplate.from_tuple((template, {'bar': 'bar'})) + MappingPulseTemplate.from_tuple((template, {'bar': 'bar'})) with self.assertRaises(AmbiguousMappingException): - MappingTemplate.from_tuple((template, {'foobar': 'foobar'})) + MappingPulseTemplate.from_tuple((template, {'foobar': 'foobar'})) template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) with self.assertRaises(MappingCollisionException): - MappingTemplate.from_tuple((template, {'foo': '1', 'bar': 2}, {'foo': '1', 'bar': 4})) + MappingPulseTemplate.from_tuple((template, {'foo': '1', 'bar': 2}, {'foo': '1', 'bar': 4})) template = DummyPulseTemplate(defined_channels={'A'}) with self.assertRaises(MappingCollisionException): - MappingTemplate.from_tuple((template, {'A': 'N'}, {'A': 'C'})) + MappingPulseTemplate.from_tuple((template, {'A': 'N'}, {'A': 'C'})) template = DummyPulseTemplate(measurement_names={'M'}) with self.assertRaises(MappingCollisionException): - MappingTemplate.from_tuple((template, {'M': 'N'}, {'M': 'N'})) + MappingPulseTemplate.from_tuple((template, {'M': 'N'}, {'M': 'N'})) def test_from_tuple(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}, @@ -69,15 +69,15 @@ def test_from_tuple(self): def test_mapping_permutations(template: DummyPulseTemplate, pmap, mmap, cmap): - direct = MappingTemplate(template, - parameter_mapping=pmap, - measurement_mapping=mmap, - channel_mapping=cmap) + direct = MappingPulseTemplate(template, + parameter_mapping=pmap, + measurement_mapping=mmap, + channel_mapping=cmap) mappings = [m for m in [pmap, mmap, cmap] if m is not None] for current_mapping_order in itertools.permutations(mappings): - mapper = MappingTemplate.from_tuple((template, *current_mapping_order)) + mapper = MappingPulseTemplate.from_tuple((template, *current_mapping_order)) self.assertEqual(mapper.measurement_mapping, direct.measurement_mapping) self.assertEqual(mapper.channel_mapping, direct.channel_mapping) self.assertEqual(mapper.parameter_mapping, direct.parameter_mapping) @@ -95,13 +95,13 @@ def test_mapping_permutations(template: DummyPulseTemplate, def test_external_params(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) - st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) + st = MappingPulseTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) external_params = {'t', 'l', 'k'} self.assertEqual(st.parameter_names, external_params) def test_constrained(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) - st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}, parameter_constraints=['t < m']) + st = MappingPulseTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}, parameter_constraints=['t < m']) external_params = {'t', 'l', 'k', 'm'} self.assertEqual(st.parameter_names, external_params) @@ -110,7 +110,7 @@ def test_constrained(self): def test_map_parameters(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) - st = MappingTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) + st = MappingPulseTemplate(template, parameter_mapping={'foo': 't*k', 'bar': 't*l'}) parameters = {'t': ConstantParameter(3), 'k': ConstantParameter(2), 'l': ConstantParameter(7)} values = {'foo': 6, 'bar': 21} @@ -127,30 +127,30 @@ def test_map_parameters(self): def test_partial_parameter_mapping(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) - st = MappingTemplate(template, parameter_mapping={'foo': 't*k'}, allow_partial_parameter_mapping=True) + st = MappingPulseTemplate(template, parameter_mapping={'foo': 't*k'}, allow_partial_parameter_mapping=True) self.assertEqual(st.parameter_mapping, {'foo': 't*k', 'bar': 'bar'}) def test_nested_mapping_avoidance(self): template = DummyPulseTemplate(parameter_names={'foo', 'bar'}) - st_1 = MappingTemplate(template, parameter_mapping={'foo': 't*k'}, allow_partial_parameter_mapping=True) - st_2 = MappingTemplate(st_1, parameter_mapping={'bar': 't*l'}, allow_partial_parameter_mapping=True) + st_1 = MappingPulseTemplate(template, parameter_mapping={'foo': 't*k'}, allow_partial_parameter_mapping=True) + st_2 = MappingPulseTemplate(st_1, parameter_mapping={'bar': 't*l'}, allow_partial_parameter_mapping=True) self.assertIs(st_2.template, template) self.assertEqual(st_2.parameter_mapping, {'foo': 't*k', 'bar': 't*l'}) - st_3 = MappingTemplate(template, - parameter_mapping={'foo': 't*k'}, - allow_partial_parameter_mapping=True, - identifier='käse') - st_4 = MappingTemplate(st_3, parameter_mapping={'bar': 't*l'}, allow_partial_parameter_mapping=True) + st_3 = MappingPulseTemplate(template, + parameter_mapping={'foo': 't*k'}, + allow_partial_parameter_mapping=True, + identifier='käse') + st_4 = MappingPulseTemplate(st_3, parameter_mapping={'bar': 't*l'}, allow_partial_parameter_mapping=True) self.assertIs(st_4.template, st_3) self.assertEqual(st_4.parameter_mapping, {'t': 't', 'k': 'k', 'bar': 't*l'}) def test_get_updated_channel_mapping(self): template = DummyPulseTemplate(defined_channels={'foo', 'bar'}) - st = MappingTemplate(template, channel_mapping={'bar': 'kneipe'}) + st = MappingPulseTemplate(template, channel_mapping={'bar': 'kneipe'}) with self.assertRaises(KeyError): st.get_updated_channel_mapping(dict()) self.assertEqual(st.get_updated_channel_mapping({'kneipe': 'meas1', 'foo': 'meas2', 'troet': 'meas3'}), @@ -158,18 +158,18 @@ def test_get_updated_channel_mapping(self): def test_measurement_names(self): template = DummyPulseTemplate(measurement_names={'foo', 'bar'}) - st = MappingTemplate(template, measurement_mapping={'foo': 'froop', 'bar': 'kneipe'}) + st = MappingPulseTemplate(template, measurement_mapping={'foo': 'froop', 'bar': 'kneipe'}) self.assertEqual( st.measurement_names, {'froop','kneipe'} ) def test_defined_channels(self): mapping = {'asd': 'A', 'fgh': 'B'} template = DummyPulseTemplate(defined_channels=set(mapping.keys())) - st = MappingTemplate(template, channel_mapping=mapping) + st = MappingPulseTemplate(template, channel_mapping=mapping) self.assertEqual(st.defined_channels, set(mapping.values())) def test_get_updated_measurement_mapping(self): template = DummyPulseTemplate(measurement_names={'foo', 'bar'}) - st = MappingTemplate(template, measurement_mapping={'bar': 'kneipe'}) + st = MappingPulseTemplate(template, measurement_mapping={'bar': 'kneipe'}) with self.assertRaises(KeyError): st.get_updated_measurement_mapping(dict()) self.assertEqual(st.get_updated_measurement_mapping({'kneipe': 'meas1', 'foo': 'meas2', 'troet': 'meas3'}), @@ -181,7 +181,7 @@ def test_build_sequence(self): template = DummyPulseTemplate(measurement_names=set(measurement_mapping.keys()), parameter_names=set(parameter_mapping.keys())) - st = MappingTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=measurement_mapping) + st = MappingPulseTemplate(template, parameter_mapping=parameter_mapping, measurement_mapping=measurement_mapping) sequencer = DummySequencer() block = DummyInstructionBlock() pre_parameters = {'k': ConstantParameter(5)} diff --git a/tests/pulses/pulse_template_tests.py b/tests/pulses/pulse_template_tests.py index a696ca333..2ccae2190 100644 --- a/tests/pulses/pulse_template_tests.py +++ b/tests/pulses/pulse_template_tests.py @@ -3,7 +3,7 @@ import copy from typing import Optional, Dict, Set, Any, List -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.utils.types import MeasurementWindow, ChannelID from qctoolkit.expressions import Expression from qctoolkit.pulses.pulse_template import AtomicPulseTemplate, MeasurementDeclaration, PulseTemplate from qctoolkit.pulses.instructions import Waveform, EXECInstruction diff --git a/tests/pulses/sequence_pulse_template_tests.py b/tests/pulses/sequence_pulse_template_tests.py index ac6408e7a..bd09499d3 100644 --- a/tests/pulses/sequence_pulse_template_tests.py +++ b/tests/pulses/sequence_pulse_template_tests.py @@ -7,7 +7,7 @@ from qctoolkit.expressions import Expression from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate, SequenceWaveform -from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException, MappingTemplate +from qctoolkit.pulses.pulse_template_parameter_mapping import MissingMappingException, UnnecessaryMappingException, MissingParameterDeclarationException, MappingPulseTemplate from qctoolkit.pulses.parameters import ParameterNotProvidedException, ConstantParameter, ParameterConstraint, ParameterConstraintViolation from tests.pulses.sequencing_dummies import DummySequencer, DummyInstructionBlock, DummyPulseTemplate,\ @@ -112,9 +112,9 @@ def __init__(self, *args, **kwargs) -> None: self.parameters['pulse_length'] = ConstantParameter(100) self.parameters['voltage'] = ConstantParameter(10) - self.sequence = SequencePulseTemplate(MappingTemplate(self.square, - parameter_mapping=self.mapping1, - measurement_mapping=self.window_name_mapping), + self.sequence = SequencePulseTemplate(MappingPulseTemplate(self.square, + parameter_mapping=self.mapping1, + measurement_mapping=self.window_name_mapping), external_parameters=self.outer_parameters) def test_init(self): diff --git a/tests/pulses/sequencing_dummies.py b/tests/pulses/sequencing_dummies.py index a8d4ac02b..743938f73 100644 --- a/tests/pulses/sequencing_dummies.py +++ b/tests/pulses/sequencing_dummies.py @@ -5,7 +5,7 @@ import numpy """LOCAL IMPORTS""" -from qctoolkit import MeasurementWindow, ChannelID +from qctoolkit.utils.types import MeasurementWindow, ChannelID from qctoolkit.serialization import Serializer from qctoolkit.pulses.instructions import Waveform, Instruction, CJMPInstruction, GOTOInstruction, REPJInstruction from qctoolkit.pulses.sequencing import Sequencer, InstructionBlock, SequencingElement diff --git a/tests/pulses/table_sequence_sequencer_intergration_tests.py b/tests/pulses/table_sequence_sequencer_intergration_tests.py index 61f34f7b1..881f68f27 100644 --- a/tests/pulses/table_sequence_sequencer_intergration_tests.py +++ b/tests/pulses/table_sequence_sequencer_intergration_tests.py @@ -1,6 +1,6 @@ import unittest -from qctoolkit.pulses.multi_channel_pulse_template import MappingTemplate +from qctoolkit.pulses.multi_channel_pulse_template import MappingPulseTemplate from qctoolkit.pulses.table_pulse_template import TablePulseTemplate from qctoolkit.pulses.sequence_pulse_template import SequencePulseTemplate from qctoolkit.pulses.parameters import ParameterNotProvidedException @@ -22,8 +22,8 @@ def test_table_sequence_sequencer_integration(self) -> None: (5, 0)]}, measurements=[('foo', 4, 1)]) - seqt = SequencePulseTemplate(MappingTemplate(t1, measurement_mapping={'foo': 'bar'}), - MappingTemplate(t2, parameter_mapping={'bar': '2 * hugo'})) + seqt = SequencePulseTemplate(MappingPulseTemplate(t1, measurement_mapping={'foo': 'bar'}), + MappingPulseTemplate(t2, parameter_mapping={'bar': '2 * hugo'})) with self.assertRaises(ParameterNotProvidedException): t1.requires_stop(dict(), dict()) From b7d0a5f566608dd041e36fed5dc0ca6bafe1c0fa Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 11:39:54 +0200 Subject: [PATCH 107/116] Update SimpleTablePulse example --- doc/source/examples/00SimpleTablePulse.ipynb | 56 ++++++++------------ 1 file changed, 23 insertions(+), 33 deletions(-) diff --git a/doc/source/examples/00SimpleTablePulse.ipynb b/doc/source/examples/00SimpleTablePulse.ipynb index e9a3f5347..c9c62497c 100644 --- a/doc/source/examples/00SimpleTablePulse.ipynb +++ b/doc/source/examples/00SimpleTablePulse.ipynb @@ -6,7 +6,7 @@ "source": [ "# Modelling a Simple TablePulseTemplate\n", "\n", - "This example demonstrates how to set up a simple TablePulseTemplate.\n", + "This example demonstrates how to set up a simple TablePulseTemplate(or short TablePT).\n", "\n", "![The pulse we want to model using the qctoolkit](img/example_pulse.png)\n", "\n", @@ -21,7 +21,7 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ @@ -37,37 +37,35 @@ "source": [ "The interpolation set for an entry always applies to the range from the previous entry to the new entry. Thus, the value for the first entry is always ignored. The default value for interpolation is 'hold'. 'hold' and 'jump' differ in that 'hold' will hold the previous value until the new entry is reached while 'jump' will immediately assume the value of the new entry.\n", "\n", - "Now, we must decide on which channel the `TablePulseTemplate` is defined on. As a `TablePulseTemplate` can contain multiple channels it takes a python `dict` whose keys are the channel identifiers and whose values are lists of entries like the one above. Channel identifiers can be either strings like 'channel_A' or integers. For this example we choose the channel ID '0' for our pulse and create the `TablePulseTemplate` `template`:" + "Now, we must decide on which channel the `TablePulseTemplate` is defined on. As a `TablePulseTemplate` can contain multiple channels it takes a python `dict` whose keys are the channel identifiers and whose values are lists of entries like the one above. Channel identifiers can be either strings like 'channel_A' or integers. For this example we choose the channel ID '0' for our pulse and create the `TablePulseTemplate` object `template`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ - "from qctoolkit.pulses import TablePulseTemplate\n", + "from qctoolkit.pulses import TablePT\n", "\n", "entries = {0: entry_list}\n", "\n", - "template = TablePulseTemplate(entries)" + "template = TablePT(entries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We plot `template` to see if everything is correct (ignore the matplotlib warning here):" + "Notice that we used the alias `TablePT`. We plot `template` to see if everything is correct:" ] }, { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -849,7 +847,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -861,9 +859,9 @@ ], "source": [ "%matplotlib notebook\n", - "from qctoolkit.pulses import plot\n", + "from qctoolkit.pulses.plotting import plot\n", "\n", - "_ = plot(template, sample_rate=100)" + "_ = plot(template, sample_rate=100, show=True)" ] }, { @@ -882,7 +880,7 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ @@ -891,7 +889,7 @@ " ('tb', 'vb', 'linear'),\n", " ('tend', 0, 'jump')]}\n", "\n", - "param_template = TablePulseTemplate(param_entries)" + "param_template = TablePT(param_entries)" ] }, { @@ -904,15 +902,13 @@ { "cell_type": "code", "execution_count": 5, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'va', 'tb', 'tend', 'ta', 'vb'}\n" + "{'ta', 'vb', 'tend', 'va', 'tb'}\n" ] } ], @@ -930,9 +926,7 @@ { "cell_type": "code", "execution_count": 6, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -1714,7 +1708,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1725,7 +1719,6 @@ } ], "source": [ - "%matplotlib notebook\n", "parameters = {'ta': 2,\n", " 'va': 2,\n", " 'tb': 4,\n", @@ -1744,9 +1737,7 @@ { "cell_type": "code", "execution_count": 7, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -2528,7 +2519,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -2539,7 +2530,6 @@ } ], "source": [ - "%matplotlib notebook\n", "parameters = {'ta': 2,\n", " 'va': 2,\n", " 'tb': 6,\n", @@ -2551,9 +2541,9 @@ ], "metadata": { "kernelspec": { - "display_name": "lab_master", + "display_name": "Python [conda env:qctoolkit]", "language": "python", - "name": "lab_master" + "name": "conda-env-qctoolkit-py" }, "language_info": { "codemirror_mode": { @@ -2565,9 +2555,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.5.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } From 338a3a10bfda22f1cf0862c85b9ac2a6be3abdf6 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 11:40:25 +0200 Subject: [PATCH 108/116] Update doc requirements --- doc/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/requirements.txt b/doc/requirements.txt index 892fa8f44..224b0f16b 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,3 +1,4 @@ sphinx>=1.4 nbsphinx -ipykernel \ No newline at end of file +ipykernel +sphinx_autodoc_typehints From 94d43b6ce0be3052f6a20731af246032f9559301 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 12:48:24 +0200 Subject: [PATCH 109/116] FIX bug in PointPulseTemplate + test --- qctoolkit/pulses/point_pulse_template.py | 5 +++-- tests/pulses/point_pulse_template_tests.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/qctoolkit/pulses/point_pulse_template.py b/qctoolkit/pulses/point_pulse_template.py index 9f29ebfd8..71e46fd6e 100644 --- a/qctoolkit/pulses/point_pulse_template.py +++ b/qctoolkit/pulses/point_pulse_template.py @@ -70,9 +70,10 @@ def build_waveform(self, mapped_channels = tuple(channel_mapping[c] for c in self._channels) - waveform_entries = [[]]*len(mapped_channels) + waveform_entries = list([] for _ in range(len(mapped_channels))) for entry in self._entries: - for ch_entries, wf_entry in zip(waveform_entries, entry.instantiate(parameters, len(self._channels))): + instantiated_entries = entry.instantiate(parameters, len(self._channels)) + for ch_entries, wf_entry in zip(waveform_entries, instantiated_entries): ch_entries.append(wf_entry) if waveform_entries[0][0].t > 0: diff --git a/tests/pulses/point_pulse_template_tests.py b/tests/pulses/point_pulse_template_tests.py index 836023b4a..e66187579 100644 --- a/tests/pulses/point_pulse_template_tests.py +++ b/tests/pulses/point_pulse_template_tests.py @@ -132,11 +132,15 @@ def test_build_waveform_multi_channel_same(self): (1., 0., HoldInterpolationStrategy()), (1.1, 21., LinearInterpolationStrategy())], [('K', 0.2, 1), ('L', 0.2, 1)]) - expected_A = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + expected_A = PointWaveform('A', [(0, 1., HoldInterpolationStrategy()), (0.1, 1., HoldInterpolationStrategy()), (1., 0., HoldInterpolationStrategy()), (1.1, 21., LinearInterpolationStrategy())], []) self.assertEqual(wf.defined_channels, {1, 'A'}) + self.assertEqual(wf._sub_waveforms[0].defined_channels, {1}) + self.assertEqual(wf._sub_waveforms[0], expected_1) + self.assertEqual(wf._sub_waveforms[1].defined_channels, {'A'}) + self.assertEqual(wf._sub_waveforms[1], expected_A) def test_build_waveform_multi_channel_vectorized(self): ppt = PointPulseTemplate([('t1', 'A'), @@ -150,11 +154,15 @@ def test_build_waveform_multi_channel_vectorized(self): (1., 0., HoldInterpolationStrategy()), (1.1, 19., LinearInterpolationStrategy())], [('K', 0.2, 1), ('L', 0.2, 1)]) - expected_A = PointWaveform(1, [(0, 1., HoldInterpolationStrategy()), + expected_A = PointWaveform('A', [(0, 1., HoldInterpolationStrategy()), (0.1, 1., HoldInterpolationStrategy()), (1., 0., HoldInterpolationStrategy()), (1.1, 20., LinearInterpolationStrategy())], []) self.assertEqual(wf.defined_channels, {1, 'A'}) + self.assertEqual(wf._sub_waveforms[0].defined_channels, {1}) + self.assertEqual(wf._sub_waveforms[0], expected_1) + self.assertEqual(wf._sub_waveforms[1].defined_channels, {'A'}) + self.assertEqual(wf._sub_waveforms[1], expected_A) class TablePulseTemplateConstraintTest(ParameterConstrainerTest): From 9eb790611eb1aec23f8b3aff8d51069516f05fb2 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 14:40:10 +0200 Subject: [PATCH 110/116] Make sequencing handle numpy.ndarray correctly and extend plotting interface --- qctoolkit/pulses/plotting.py | 22 ++++++++++++++-------- qctoolkit/pulses/sequencing.py | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/qctoolkit/pulses/plotting.py b/qctoolkit/pulses/plotting.py index 5fbcc1d9e..641d2b610 100644 --- a/qctoolkit/pulses/plotting.py +++ b/qctoolkit/pulses/plotting.py @@ -73,7 +73,9 @@ def get_waveform_generator(instruction_block): def plot(pulse: PulseTemplate, parameters: Dict[str, Parameter]=None, - sample_rate: int=10) -> Any: # pragma: no cover + sample_rate: int=10, + axes: Any=None, + show: bool=True) -> Any: # pragma: no cover """Plot a pulse using matplotlib. The given pulse will first be sequenced using the Sequencer class. The resulting @@ -86,6 +88,8 @@ def plot(pulse: PulseTemplate, objects. sample_rate: The rate with which the waveforms are sampled for the plot in samples per time unit. (default = 10) + axes: matplotlib Axes object the pulse will be drawn into if provided + show: If true, the figure will be shown Returns: matplotlib.pyplot.Figure instance in which the pulse is rendered Raises: @@ -109,13 +113,14 @@ def plot(pulse: PulseTemplate, raise PlottingNotPossibleException(pulse) times, voltages = render(sequence, sample_rate) - # plot to figure - figure = plt.figure() - ax = figure.add_subplot(111) + if axes is None: + # plot to figure + figure = plt.figure() + axes = figure.add_subplot(111) for ch_name, voltage in voltages.items(): - ax.step(times, voltage, where='post', label='channel {}'.format(ch_name)) + axes.step(times, voltage, where='post', label='channel {}'.format(ch_name)) - ax.legend() + axes.legend() max_voltage = max(max(channel) for channel in voltages.values()) min_voltage = min(min(channel) for channel in voltages.values()) @@ -130,8 +135,9 @@ def plot(pulse: PulseTemplate, if pulse.identifier: plt.title(pulse.identifier) - figure.show() - return figure + if show: + axes.get_figure().show() + return axes.get_figure() class PlottingNotPossibleException(Exception): diff --git a/qctoolkit/pulses/sequencing.py b/qctoolkit/pulses/sequencing.py index 47814cbd3..ea254f46f 100644 --- a/qctoolkit/pulses/sequencing.py +++ b/qctoolkit/pulses/sequencing.py @@ -160,7 +160,7 @@ def push(self, else: channel_mapping = dict() for (key, value) in parameters.items(): - if isinstance(value, numbers.Real): + if not isinstance(value, Parameter): parameters[key] = ConstantParameter(value) if target_block not in self.__sequencing_stacks: From af390236688cdf4a0525785621ee0d332049e10d Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 31 Aug 2017 14:42:00 +0200 Subject: [PATCH 111/116] Add, rename and reorder some examples --- .../examples/01AdvancedTablePulse.ipynb | 2508 +++++++++++++++++ doc/source/examples/03PointPulse.ipynb | 874 ++++++ ...ialization.ipynb => 04Serialization.ipynb} | 24 +- ...uencePulse.ipynb => 06SequencePulse.ipynb} | 45 +- ...{04Sequencing.ipynb => 99Sequencing.ipynb} | 0 5 files changed, 3411 insertions(+), 40 deletions(-) create mode 100644 doc/source/examples/01AdvancedTablePulse.ipynb create mode 100644 doc/source/examples/03PointPulse.ipynb rename doc/source/examples/{03Serialization.ipynb => 04Serialization.ipynb} (99%) rename doc/source/examples/{01SequencePulse.ipynb => 06SequencePulse.ipynb} (70%) rename doc/source/examples/{04Sequencing.ipynb => 99Sequencing.ipynb} (100%) diff --git a/doc/source/examples/01AdvancedTablePulse.ipynb b/doc/source/examples/01AdvancedTablePulse.ipynb new file mode 100644 index 000000000..c4e453deb --- /dev/null +++ b/doc/source/examples/01AdvancedTablePulse.ipynb @@ -0,0 +1,2508 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Modelling an Advanced TablePulseTemplate\n", + "\n", + "[The SimpleTablePulse example](00SimpleTablePulse.ipynb) shows how a simple parametrized ```TablePT``` on one channel can implemented and how the interpolation works.\n", + "\n", + "This example demonstrates how to set up a more complex ```TablePT```. This means we will include multiple channels and use expressions for times and voltages.\n", + "\n", + "First lets reimplement the pulse from the previous example but this time with a second channel `'B'`, that has the same voltage values but negative. To do this, we extend the entry dict by a second item with the channel ID `'B'` as key and the entry list as value.\n", + "\n", + "Then we plot it to see that it actually works." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "expr_pulse = TablePT({'A': [(0, 'a_0'),\n", + " ('t_1', 'a_0 + exp(theta)', 'hold'),\n", + " ('t_2', 'Abs(x_0 - y_0)', 'linear')],\n", + " 'B': [(0, 'b_0'),\n", + " ('t_1*(b_0/a_0)', 'b_1', 'linear'),\n", + " ('t_2', 'b_2')]})\n", + "_ = plot(expr_pulse, dict(a_0=1.1, theta=2, x_0=0.5, y_0=1, t_1=10, t_2=25, b_0=0.6, b_1=6, b_2=0.4))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + " __Is there a requirement that all channels have the same duration?__\n", + " \n", + " No. The shorter channels stay on their last value until the last channel is finished. The duration of the complete pulse template is given as the corresponding expression:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max(t_A, t_B)\n" + ] + }, + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib notebook\n", + "from qctoolkit.pulses.plotting import plot\n", + "import numpy as np\n", + "\n", + "parameters = dict(t=3,\n", + " v_0=1,\n", + " v_1=np.array([1.2, 2.5]))\n", + "\n", + "_ = plot(point_template, parameters, sample_rate=100)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:qctoolkit]", + "language": "python", + "name": "conda-env-qctoolkit-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/doc/source/examples/03Serialization.ipynb b/doc/source/examples/04Serialization.ipynb similarity index 99% rename from doc/source/examples/03Serialization.ipynb rename to doc/source/examples/04Serialization.ipynb index 3933eb21a..3a73693cb 100644 --- a/doc/source/examples/03Serialization.ipynb +++ b/doc/source/examples/04Serialization.ipynb @@ -15,9 +15,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from qctoolkit.pulses import TablePulseTemplate\n", @@ -45,9 +43,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "identified_table = TablePulseTemplate({'A': [('ta', 'va', 'hold'),\n", @@ -72,9 +68,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -1676,9 +1670,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "from qctoolkit.pulses import SequencePulseTemplate\n", @@ -1734,9 +1726,9 @@ ], "metadata": { "kernelspec": { - "display_name": "lab_master", + "display_name": "Python [conda env:qctoolkit]", "language": "python", - "name": "lab_master" + "name": "conda-env-qctoolkit-py" }, "language_info": { "codemirror_mode": { @@ -1748,9 +1740,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.5.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/doc/source/examples/01SequencePulse.ipynb b/doc/source/examples/06SequencePulse.ipynb similarity index 70% rename from doc/source/examples/01SequencePulse.ipynb rename to doc/source/examples/06SequencePulse.ipynb index a2c70d9f1..22704483b 100644 --- a/doc/source/examples/01SequencePulse.ipynb +++ b/doc/source/examples/06SequencePulse.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Combining PulseTemplates Using SequencePulseTemplate\n", + "# Sequencing PulseTemplates\n", "\n", "In this example we will use the `SequencePulseTemplate` class to build a sequence of two of the simple table pulses defined in [Modelling a Simple TablePulseTemplate](00SimpleTablePulse.ipynb). We will also map the parameters such that we can individually adapt values in either of the two subtemplates.\n", "\n", @@ -15,16 +15,16 @@ "cell_type": "code", "execution_count": 1, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ - "from qctoolkit.pulses import TablePulseTemplate\n", + "from qctoolkit.pulses import TablePT\n", "\n", - "template = TablePulseTemplate(entries={0: [(0, 0),\n", - " ('ta', 'va', 'hold'),\n", - " ('tb', 'vb', 'linear'),\n", - " ('tend', 0, 'jump')]},identifier='foo')" + "template = TablePT(entries={0: [(0, 0),\n", + " ('ta', 'va', 'hold'),\n", + " ('tb', 'vb', 'linear'),\n", + " ('tend', 0, 'jump')]},identifier='foo')" ] }, { @@ -38,11 +38,11 @@ "cell_type": "code", "execution_count": 2, "metadata": { - "collapsed": false + "collapsed": true }, "outputs": [], "source": [ - "from qctoolkit.pulses import SequencePulseTemplate\n", + "from qctoolkit.pulses import SequencePT\n", "\n", "external_parameters = ['ta', 'tb', 'tc', 'td', 'va', 'vb', 'tend']\n", "first_mapping = {\n", @@ -59,9 +59,9 @@ " 'vb': 'va + vb',\n", " 'tend': '2 * tend'\n", "}\n", - "sequence = SequencePulseTemplate((template, first_mapping),\n", - " (template, second_mapping),\n", - " external_parameters=external_parameters)" + "sequence = SequencePT((template, first_mapping),\n", + " (template, second_mapping),\n", + " external_parameters=external_parameters)" ] }, { @@ -74,15 +74,13 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'vb', 'ta', 'va', 'tb', 'tend', 'tc', 'td'}\n" + "{'tend', 'vb', 'ta', 'va', 'tc', 'tb', 'td'}\n" ] } ], @@ -103,7 +101,6 @@ "cell_type": "code", "execution_count": 4, "metadata": { - "collapsed": false, "scrolled": true }, "outputs": [ @@ -666,7 +663,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", + "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -887,7 +884,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -899,7 +896,7 @@ ], "source": [ "%matplotlib notebook\n", - "from qctoolkit.pulses import plot\n", + "from qctoolkit.pulses.plotting import plot\n", "\n", "parameters = {'ta': 2,\n", " 'va': 2,\n", @@ -914,9 +911,9 @@ ], "metadata": { "kernelspec": { - "display_name": "lab_master", + "display_name": "Python [default]", "language": "python", - "name": "lab_master" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -928,9 +925,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.3" + "version": "3.5.2" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/doc/source/examples/04Sequencing.ipynb b/doc/source/examples/99Sequencing.ipynb similarity index 100% rename from doc/source/examples/04Sequencing.ipynb rename to doc/source/examples/99Sequencing.ipynb From 1ae48ccc5a9c19d6f5252daac62cf1e7543d7323 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Tue, 5 Sep 2017 18:17:55 +0200 Subject: [PATCH 112/116] FIX obvious bugs in tabor driver --- qctoolkit/hardware/awgs/tabor.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/qctoolkit/hardware/awgs/tabor.py b/qctoolkit/hardware/awgs/tabor.py index 3c029c243..7f149bd11 100644 --- a/qctoolkit/hardware/awgs/tabor.py +++ b/qctoolkit/hardware/awgs/tabor.py @@ -465,7 +465,9 @@ def __init__(self, tabor_device: TaborAWGRepresentation, channels: Tuple[int, in def free_program(self, name: str) -> TaborProgramMemory: program = self._known_programs.pop(name) - self._segment_references[program.segment_indices] -= 1 + self._segment_references[program.segment_indices-1] -= 1 + if self._current_program == name: + self._current_program = None return program @property @@ -545,7 +547,6 @@ def upload(self, name: str, if np.any(to_amend): segments_to_amend = segments[to_amend] - self._amend_segments(segments_to_amend) waveform_to_segment[to_amend] = self._amend_segments(segments_to_amend) self._known_programs[name] = TaborProgramMemory(segment_indices=waveform_to_segment, @@ -588,9 +589,9 @@ def _find_place_for_segments_in_memory(self, segments: Sequence, segment_lengths to_insert = np.full(len(segments), fill_value=-1, dtype=np.int64) reserved_indices = np.flatnonzero(new_reference_counter > 0) - last_reserved = reserved_indices[-1] if len(reserved_indices) else 0 + first_free = reserved_indices[-1] + 1 if len(reserved_indices) else 0 - free_segments = new_reference_counter[:last_reserved] == 0 + free_segments = new_reference_counter[:first_free] == 0 free_segment_count = np.sum(free_segments) # look for a free segment place with the same length @@ -598,7 +599,7 @@ def _find_place_for_segments_in_memory(self, segments: Sequence, segment_lengths if free_segment_count == 0: break - pos_of_same_length = np.logical_and(free_segments, segment_lengths[segment_idx] == self._segment_capacity[:last_reserved]) + pos_of_same_length = np.logical_and(free_segments, segment_lengths[segment_idx] == self._segment_capacity[:first_free]) idx_same_length = np.argmax(pos_of_same_length) if pos_of_same_length[idx_same_length]: free_segments[idx_same_length] = False @@ -610,7 +611,7 @@ def _find_place_for_segments_in_memory(self, segments: Sequence, segment_lengths # try to find places that are larger than the segments to fit in starting with the large segments and large # free spaces segment_indices = np.flatnonzero(to_amend)[np.argsort(segment_lengths[to_amend])[::-1]] - capacities = self._segment_capacity[:last_reserved] + capacities = self._segment_capacity[:first_free] for segment_idx in segment_indices: free_capacities = capacities[free_segments] free_segments_indices = np.flatnonzero(free_segments)[np.argsort(free_capacities)[::-1]] @@ -625,7 +626,7 @@ def _find_place_for_segments_in_memory(self, segments: Sequence, segment_lengths to_amend[segment_idx] = False to_insert[segment_idx] = fitting_segment - free_points_at_end = self.total_capacity - np.sum(self._segment_capacity[:last_reserved]) + free_points_at_end = self.total_capacity - np.sum(self._segment_capacity[:first_free]) if np.sum(segment_lengths[to_amend] + 16) > free_points_at_end: raise MemoryError('Fragmentation does not allow upload.', np.sum(segment_lengths[to_amend] + 16), @@ -690,18 +691,21 @@ def _amend_segments(self, segments: List[TaborSegment]) -> np.ndarray: self._segment_hashes = segment_hashes self._segment_references = segment_references - return segment_index + np.arange(len(segments), dtype=np.int64) - 1 + return segment_index + np.arange(len(segments), dtype=np.int64) def cleanup(self) -> None: """Discard all segments after the last which is still referenced""" reserved_indices = np.flatnonzero(self._segment_references > 0) - + old_end = len(self._segment_lengths) new_end = reserved_indices[-1]+1 if len(reserved_indices) else 0 self._segment_lengths = self._segment_lengths[:new_end] self._segment_capacity = self._segment_capacity[:new_end] self._segment_hashes = self._segment_hashes[:new_end] self._segment_references = self._segment_references[:new_end] + delete_cmd = ';'.join('TRAC:DEL {}'.format(i+1) for i in range(new_end, old_end)) + self._device.send_cmd(delete_cmd) + def remove(self, name: str) -> None: """Remove a program from the AWG. From 0a6c97c4987aa8c3056d80ab8fd432eb9fd81308 Mon Sep 17 00:00:00 2001 From: Simon Humpohl Date: Thu, 7 Sep 2017 10:59:04 +0200 Subject: [PATCH 113/116] Extend examples, make constraints Expressions and clarify some exception strings --- doc/source/examples/05MappingTemplate.ipynb | 255 + doc/source/examples/06SequencePulse.ipynb | 862 +++- .../examples/07MultiChannelTemplates.ipynb | 2525 ++++++++++ doc/source/examples/07PulseControl.ipynb | 34 - doc/source/examples/08Measurements.ipynb | 93 + doc/source/examples/08RealWorldCase.ipynb | 4234 ----------------- .../examples/09ParameterConstraints.ipynb | 925 ++++ .../examples/10MultiChannelTemplates.ipynb | 2535 ---------- ... => 95DetailedSequencingWalkthrough.ipynb} | 114 +- doc/source/examples/96RealWorldCase.ipynb | 280 ++ ...ion.ipynb => 97ConditionalExecution.ipynb} | 20 +- ...{05Parameters.ipynb => 98Parameters.ipynb} | 18 +- qctoolkit/expressions.py | 5 +- .../pulses/multi_channel_pulse_template.py | 6 +- qctoolkit/pulses/parameters.py | 21 +- qctoolkit/serialization.py | 10 +- 16 files changed, 4998 insertions(+), 6939 deletions(-) create mode 100644 doc/source/examples/05MappingTemplate.ipynb create mode 100644 doc/source/examples/07MultiChannelTemplates.ipynb delete mode 100644 doc/source/examples/07PulseControl.ipynb create mode 100644 doc/source/examples/08Measurements.ipynb delete mode 100644 doc/source/examples/08RealWorldCase.ipynb create mode 100644 doc/source/examples/09ParameterConstraints.ipynb delete mode 100644 doc/source/examples/10MultiChannelTemplates.ipynb rename doc/source/examples/{09DetailedSequencingWalkthrough.ipynb => 95DetailedSequencingWalkthrough.ipynb} (95%) create mode 100644 doc/source/examples/96RealWorldCase.ipynb rename doc/source/examples/{06ConditionalExecution.ipynb => 97ConditionalExecution.ipynb} (97%) rename doc/source/examples/{05Parameters.ipynb => 98Parameters.ipynb} (97%) diff --git a/doc/source/examples/05MappingTemplate.ipynb b/doc/source/examples/05MappingTemplate.ipynb new file mode 100644 index 000000000..ef49aa074 --- /dev/null +++ b/doc/source/examples/05MappingTemplate.ipynb @@ -0,0 +1,255 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mapping with the MappingPulseTemplate\n", + "\n", + "We will now have a look on how to remap parameters, channel ids and measurements. The definition of measurements is illustrated [here](Measurements.ipynb). The `MappingPulseTemplate` \n", + "\n", + "First we will have a look at the __mapping of parameters__:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2*pi/omega\n", + "{'a', 'omega'}\n" + ] + } + ], + "source": [ + "from qctoolkit.pulses import MappingPT, FunctionPT, SequencePT, AtomicMultiChannelPT\n", + "\n", + "sine = FunctionPT('a*sin(omega*t)', 't_duration')\n", + "\n", + "my_parameter_mapping = dict(t_duration='2*pi/omega', omega='omega', a='a')\n", + "\n", + "single_period_sine = MappingPT(sine, parameter_mapping=my_parameter_mapping)\n", + "\n", + "print(single_period_sine.duration)\n", + "print(single_period_sine.parameter_names)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Notice that we had to give mappings for all parameters, not only for the ones which changed. If we omit some of the encapsulated pulse tempaltes parameters an `MissingMappingException` is raised. This is done to enforce active thinking.\n", + "\n", + "You can, however, allow partial parameter mappings by passing `allow_partial_paramter_mappings=True` to the constructor." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MissingMappingException : The template needs a mapping function for parameter(s) {'a', 'omega'}\n", + "\n", + "2*pi/omega\n", + "{'a', 'omega'}\n" + ] + } + ], + "source": [ + "partial_parameter_mapping = dict(t_duration='2*pi/omega')\n", + "try:\n", + " single_period_sine = MappingPT(sine, parameter_mapping=partial_parameter_mapping)\n", + "except Exception as exception:\n", + " print(type(exception).__name__, ':', exception)\n", + "print('')\n", + "\n", + "\n", + "single_period_sine = MappingPT(sine, parameter_mapping=partial_parameter_mapping, allow_partial_parameter_mapping=True)\n", + "print(single_period_sine.duration)\n", + "print(single_period_sine.parameter_names)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Mapping of channel ids and measurement names:\n", + "\n", + "Sometimes it is necessary to rename channels or measurements. Here we see a case where we want to play a sine and a cosine in parallel by using the `AtomicMultiChannelPulseTemplate`. Of course, this doesn't work as both pulses are by default defined on the 'default' channel." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ChannelMappingException : Channel is defined in subtemplate 1 and subtemplate 2\n" + ] + } + ], + "source": [ + "sine_measurements = [('M', 't_duration/2', 't_duration')]\n", + "sine = FunctionPT('a*sin(omega*t)', 't_duration', measurements=sine_measurements)\n", + "\n", + "cos_measurements = [('M', 0, 't_duration/2')]\n", + "cos = FunctionPT('a*cos(omega*t)', 't_duration', measurements=cos_measurements)\n", + "\n", + "try:\n", + " both = AtomicMultiChannelPT(sine, cos)\n", + "except Exception as exception:\n", + " print(type(exception).__name__, ':', exception)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The solution is to use the `MappingPT` and rename the channels as we see in the next cell. Additionally, we want to distinguish between the measurements, so we rename them, too. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "remapped_cos channels: {'cos_channel'}\n", + "remapped_cos measurements: {'M_cos'}\n", + "\n", + "remapped_sine channels: {'sin_channel'}\n", + "remapped_sine measurements: {'M_sin'}\n", + "\n", + "{'cos_channel', 'sin_channel'}\n", + "{'M_cos', 'M_sin'}\n" + ] + } + ], + "source": [ + "cos_channel_mapping = dict(default='cos_channel')\n", + "cos_measurement_mapping = dict(M='M_cos')\n", + "remapped_cos = MappingPT(cos, channel_mapping=cos_channel_mapping, measurement_mapping=cos_measurement_mapping)\n", + "print('remapped_cos channels:', remapped_cos.defined_channels)\n", + "print('remapped_cos measurements:', remapped_cos.measurement_names)\n", + "print()\n", + "\n", + "sine_channel_mapping = dict(default='sin_channel')\n", + "sine_measurement_mapping = dict(M='M_sin')\n", + "remapped_sine = MappingPT(sine, measurement_mapping=sine_measurement_mapping, channel_mapping=sine_channel_mapping)\n", + "print('remapped_sine channels:', remapped_sine.defined_channels)\n", + "print('remapped_sine measurements:', remapped_sine.measurement_names)\n", + "print()\n", + "\n", + "both = AtomicMultiChannelPT(remapped_sine, remapped_cos)\n", + "print(both.defined_channels)\n", + "print(both.measurement_names)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Automatically created mapping templates\n", + "\n", + "Besides the explicit usage of the template it is also used implicitly in some cases. All implicit uses make use of the static member function `MappingPulseTemplate.from_tuple`. This 'constructor' automatically decides which mapping belongs to which entity." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "channels: {'default'}\n", + "measurements {'M_sin'}\n", + "parameters {'t_duration', 'a', 'omega'}\n", + "\n", + "channels: {'default'}\n", + "measurements {'M_sin'}\n", + "parameters {'a', 'omega'}\n", + "\n" + ] + } + ], + "source": [ + "auto_mapped = MappingPT.from_tuple((sine, sine_measurement_mapping))\n", + "print('channels:', auto_mapped.defined_channels)\n", + "print('measurements', auto_mapped.measurement_names)\n", + "print('parameters', auto_mapped.parameter_names)\n", + "print()\n", + "\n", + "auto_mapped = MappingPT.from_tuple((sine, sine_measurement_mapping, partial_parameter_mapping))\n", + "print('channels:', auto_mapped.defined_channels)\n", + "print('measurements', auto_mapped.measurement_names)\n", + "print('parameters', auto_mapped.parameter_names)\n", + "print()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In many cases, you do not need to create the MappingPT yourself. Most PulseParameters accept a mapping tuple like the ones used in the last cell. We could create our combined pulse also by using this implicit conversion:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'cos_channel', 'sin_channel'}\n", + "{'M_cos', 'M_sin'}\n" + ] + } + ], + "source": [ + "both_implicit = AtomicMultiChannelPT((sine, sine_channel_mapping, sine_measurement_mapping), \n", + " (cos, cos_measurement_mapping, cos_channel_mapping))\n", + "print(both_implicit.defined_channels)\n", + "print(both_implicit.measurement_names)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:qctoolkit]", + "language": "python", + "name": "conda-env-qctoolkit-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/doc/source/examples/06SequencePulse.ipynb b/doc/source/examples/06SequencePulse.ipynb index 22704483b..4edd1bc87 100644 --- a/doc/source/examples/06SequencePulse.ipynb +++ b/doc/source/examples/06SequencePulse.ipynb @@ -31,15 +31,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Constructing a sequence of two of these requires us to set up a `SequencePulseTemplate` instance which expects a list of subtemplates, a set of external parameters and corresponding parameters mappings:" + "Constructing a sequence of two of these requires us to set up a `SequencePulseTemplate` instance which expects a list of subtemplates. Optionally we can provide a set of external parametersand qctoolkit checks for us whether the result has exactly these:" ] }, { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [ "from qctoolkit.pulses import SequencePT\n", @@ -59,7 +57,13 @@ " 'vb': 'va + vb',\n", " 'tend': '2 * tend'\n", "}\n", - "sequence = SequencePT((template, first_mapping),\n", + "\n", + "# works\n", + "sequence = SequencePT(template,\n", + " (template, second_mapping))\n", + "\n", + "# works, too\n", + "sequence = SequencePT(template,\n", " (template, second_mapping),\n", " external_parameters=external_parameters)" ] @@ -68,7 +72,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Our sequence now exposes the parameters declared in the `external_parameters` set:" + "Here we see what happens if we provide the wrong external parameter set" ] }, { @@ -80,7 +84,37 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'tend', 'vb', 'ta', 'va', 'tc', 'tb', 'td'}\n" + "MissingParameterDeclarationException : A mapping for template requires a parameter 'tend' which has not been declared as an external parameter of the SequencePulseTemplate.\n" + ] + } + ], + "source": [ + "try:\n", + " wrong_external_parameters = external_parameters[:-1]\n", + " sequence = SequencePT(template,\n", + " (template, second_mapping),\n", + " external_parameters=wrong_external_parameters)\n", + "except Exception as exception:\n", + " print(type(exception).__name__, ':', exception)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our sequence now exposes the parameters declared in the `external_parameters` set:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'va', 'tend', 'tb', 'td', 'vb', 'tc', 'ta'}\n" ] } ], @@ -99,7 +133,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "scrolled": true }, @@ -663,7 +697,7 @@ "};\n", "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n", "\n", - "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n", + "mpl.extensions = [\"eps\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\"];\n", "\n", "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n", " // Create a \"websocket\"-like object which calls the given IPython comm\n", @@ -884,7 +918,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -907,6 +941,814 @@ " 'tend': 6}\n", "_ = plot(sequence, parameters, sample_rate=100)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "There is a shortcut to sequencing pulse templates via the '@' operator. Of course, here we did not change the parameter mapping so the result looks different." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of channels in table_template is 2.\n" + ] + } + ], + "source": [ + "from qctoolkit.pulses import TablePT\n", + "\n", + "table_template = TablePT(identifier='2-channel-table-template',\n", + " entries={0: [(0, 0),\n", + " (1, 4),\n", + " ('foo', 'bar'),\n", + " (10, 0)],\n", + " 1: [(0, 0),\n", + " ('foo', 2.7, 'linear'),\n", + " (9, 'bar', 'linear')]})\n", + "\n", + "# plot it\n", + "%matplotlib notebook\n", + "from qctoolkit.pulses.plotting import plot\n", + "parameters = dict(\n", + " foo=7,\n", + " bar=-1.3\n", + ")\n", + "_ = plot(table_template, parameters, sample_rate=100)\n", + "print(\"The number of channels in table_template is {}.\".format(table_template.num_channels))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Combining Templates: `AtomicMultiChannelPulseTemplate`\n", + "\n", + "`AtomicMultiChannelPulseTemplate`(`AtomicMultiChannelPT`) allows to compose a multi-channel template out of atomic (i.e., no control flow) templates of equal duration. It allows to reassign channel indices of the channels of its subtemplates. The constructor is similar to the one of `SequencePulseTemplate` and expects subtemplates including parameter and channel mappings as well as a set of external parameters.\n", + "\n", + "The following example will combine the two-channel table pulse template `table_template` from above and a function pulse template `function_template` to a three-channel template `template`. We reassign indices such that channel 'chan_B' of `template` is channel 0 and 'chan_B' is channel 1 of `table_template`. Furthermore the parameters get remapped. `function_template` doesnt get changed at all." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of channels in sequence_template is 2.\n" + ] + } + ], + "source": [ + "from qctoolkit.pulses import SequencePT\n", + "from qctoolkit.pulses import MappingPT\n", + "\n", + "sequence_template = SequencePT(\n", + " (table_template, dict(foo='1.2 * hugo', bar='hugo ** 2')),\n", + " (table_template, dict(foo='1.2 * hugo', bar='hugo ** 2'), {0: 1, 1: 0}),\n", + " identifier='2-channel-sequence-template'\n", + ")\n", + "\n", + "plot(sequence_template, dict(hugo=2), sample_rate=100)\n", + "print(\"The number of channels in sequence_template is {}.\".format(sequence_template.num_channels))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:qctoolkit]", + "language": "python", + "name": "conda-env-qctoolkit-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/doc/source/examples/07PulseControl.ipynb b/doc/source/examples/07PulseControl.ipynb deleted file mode 100644 index 6aa8bb111..000000000 --- a/doc/source/examples/07PulseControl.ipynb +++ /dev/null @@ -1,34 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Pulse-Control Integration\n", - "\n", - "An example for the current state of the integration of the `qctoolkit` functionalities in the `special-measure.pulsecontrol` MATLAB framework is provided by the file `MATLAB/qctoolkitExample.m` in the code repository. It relies on the MATLAB function `convert_qctoolkit` that can be found in the same directory." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.1" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/doc/source/examples/08Measurements.ipynb b/doc/source/examples/08Measurements.ipynb new file mode 100644 index 000000000..264108e89 --- /dev/null +++ b/doc/source/examples/08Measurements.ipynb @@ -0,0 +1,93 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Definition of measurements\n", + "\n", + "Many pulse templates allow us to declare measurements upon their creation. Each measurement declaration is a tuple that consists of the measurements name, the starting point in the pulse template and the measurements length. The idea behind measurement names is that you can put different types of measurements in one pulse and easily distinguish between the results. `qctoolkit` automatically configures the acquisition driver to measure at the measurement windows.\n", + "\n", + "The following example creates a pulse template that contains a measurement named 'M' and a measurement named 'N':" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'N', 'M'}\n", + "[('M', 0, 't_meas'), ('N', 0, 't_meas/2')]\n" + ] + } + ], + "source": [ + "from qctoolkit.pulses import PointPT\n", + "\n", + "meas = PointPT([(0, 'm'),\n", + " ('t_meas', 'm')],\n", + " channel_names=('RF_X', 'RF_Y'),\n", + " measurements=[('M', 0, 't_meas'), ('N', 0, 't_meas/2')])\n", + "print(meas.measurement_names)\n", + "print(meas.measurement_declarations)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "The measurement windows may not reach out of the declaring pulse template. If that happens an exception is raised during sequencing.\n", + "\n", + "It is also possible to rename the measurements:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'dbz_fid', 'N', 'charge_scan'}\n" + ] + } + ], + "source": [ + "from qctoolkit.pulses import SequencePT\n", + "\n", + "my_complicated_pulse = SequencePT((meas, {'M': 'charge_scan'}),\n", + " (meas, {'M': 'dbz_fid'}))\n", + "print(my_complicated_pulse.measurement_names)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:qctoolkit]", + "language": "python", + "name": "conda-env-qctoolkit-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/doc/source/examples/08RealWorldCase.ipynb b/doc/source/examples/08RealWorldCase.ipynb deleted file mode 100644 index 9bbdf673a..000000000 --- a/doc/source/examples/08RealWorldCase.ipynb +++ /dev/null @@ -1,4234 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Gate Configuration - A Real Use Case" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "An example for a real use case of the qctoolkit is the search for and evaluation of parameters for pulses that represent quantum gate operations.\n", - "\n", - "## Description of the Experiment\n", - "The experiment will typically involve a set of gate pulses $G_j, 0 \\leq j \\lt N_{Gates}$.\n", - "\n", - "The template for a gate pulse $G_j$ is a sequence of $\\epsilon_i, 0 \\leq i \\lt N_{G_j}$ voltage levels held for time $\\Delta t = 1$ ns as illustrated in the figure below (with $N_{G_j} = 7$).\n", - "\n", - "![Template of a gate pulse](img/gate_pulse_scheme.png)\n", - "\n", - "The experiment defines a number of sequences $S_k, 0 \\leq k \\lt N_{Sequences}$ of the $G_j$ as $$S_k = (G_{m_k(1)}, G_{m_k(2)}, \\dots, G_{m_k(N_{S_k})})$$ where $N_{S_k}$ is the length of sequence $k$ and $m_k(i): \\{0, \\dots, N_{S_k} - 1\\} \\rightarrow \\{0, \\dots, N_{Gates} - 1\\}$ is a function that maps an index $i$ to the $m_k(i)$-th gate of sequence $S_k$ and thus fully describes the sequence. (These sequences express the sequential application of the gates to the qubit. In terms of quantum mathematics they may rather be expressed as multiplication of the matrices describing the unitary transformations applied by the gates: $S_k = \\prod_{i=N_{S_k} - 1}^{0} G_{m_k(i)} = G_{(N_{S_k} - 1)} \\cdot \\dots \\cdot G_{1} \\cdot G_{0}$.)\n", - "\n", - "Measuring and analysing the effects of these sequences on the qubit's state to derive parameters $\\epsilon_i$ for gate pulses that achieve certain state transformations is the goal of the experiment.\n", - "\n", - "To this end, every sequence must be extended by some preceeding initialization pulse and a succeeding measurement pulse. Furthermore, due to hardware constraints in measuring, all sequences must be of equal length (which is typically 4 µs). Thus, some sequences require some wait time before initialization to increase their playback duration. These requirements give raise to extended sequences $S_k'$ of the form:\n", - "$$S_k' = I_{p(k)} | W_k | S_k | M_{q(k)}$$\n", - "where the functions $p(k)$ and $q(k)$ respectively select some initialization pulse $I_{p(k)} \\in \\{I_1, I_2, \\dots\\}$ and measurement pulse $M_{q(k)} \\in \\{M_1, M_2, \\dots\\}$ for sequence $k$ and $W_k$ is the aforementioned wait pulse. The '|' denote concatenation of pulses, i.e., sequential execution.\n", - "\n", - "Since measurement of quantum state is a probabilistic process, many measurements of the effect of a single sequence must be made to reconstruct the resulting state of the qubit. Thus, the experiment at last defines scanlines (typically of duration 1 second), which are sequences of the $S_k'$. (These simply represent batches of sequences to configure playback and measurement systems and have no meaning to the experiment beyond these practical considerations.)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Implementation Using the qctoolkit\n", - "\n", - "We now want to illustrate how to setup the experiment described above using the qctoolkit. Let us assume the experiment considers only two different gates pulses ($N_{Gates} = 2$). We further assume that $N_{G_1} = 20$ and $N_{G_2} = 18$. We define them using instances of `TablePulseTemplate`:" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "from qctoolkit.pulses import TablePulseTemplate\n", - "\n", - "delta_t = 1 # assuming that delta_t is 1 elementary time unit long in pulse defintions, i.e. 1 time unit = 1 ns\n", - "\n", - "gate_0 = TablePulseTemplate({0: [(i*delta_t, 'gate_0_eps_' + str(i))\n", - " for i in range(19)]})\n", - " \n", - "gate_1 = TablePulseTemplate({0: [(i*delta_t, 'gate_2_eps_' + str(i))\n", - " for i in range(17)]})\n", - " \n", - "gates = [gate_0, gate_1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We thus obtain two `TablePulseTemplate` of the desired form with parameters 'gate_1_eps_1' to 'gate_1_eps_20' and 'gate_2_eps_1' to 'gate_2_eps_18'.\n", - "\n", - "Next, we will define sequences as `SequncePulseTemplate` objects. We assume that the mapping functions $m_k$ are given as a 2 dimensional array (which impilicitly also defines $N_{Sequences}$ and $N_{S_k}$), e.g.:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "m = [\n", - " [0, 1, 0, 0, 0, 1, 1, 0, 1], # m_0(i)\n", - " [1, 1, 0, 0, 1, 0], # m_1(i)\n", - " [1, 0, 0, 1, 1, 0, 0, 1] #m_2(i)\n", - " ]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The `SequencePulseTemplate` objects can now easily be constructed:" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "from qctoolkit.pulses import SequencePulseTemplate\n", - "\n", - "# SequencePulseTemplate requires a definition of parameter mappings from parameters passed into the\n", - "# SequencePulseTemplate object to its subtemplates. In this case, we want parameters to map 1 to 1\n", - "# and thus create an identity mapping of parameter names for both gates using python list/set/dict comprehension\n", - "epsilon_mappings = [\n", - " {param_name: param_name for param_name in gates[0].parameter_names},\n", - " {param_name: param_name for param_name in gates[1].parameter_names}\n", - "]\n", - "all_epsilons = gates[0].parameter_names | gates[1].parameter_names\n", - "\n", - "sequences = []\n", - "for m_k in m:\n", - " subtemplates = []\n", - " \n", - " sequences.append(SequencePulseTemplate(*(gates[g_ki] for g_ki in m_k)))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We end up with a list `sequences` which contains the sequences described by our $m$ as qctoolkit pulse templates.\n", - "\n", - "To visualize our progress, let us plot our two gates and the second sequence with some random values between $-5$ and $5$ for the $\\epsilon_i$:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false, - "scrolled": false - }, - "outputs": [ - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", - " button.click(method_name, toolbar_event);\n", - " button.mouseover(tooltip, toolbar_mouse_event);\n", - " nav_element.append(button);\n", - " }\n", - "\n", - " // Add the status bar.\n", - " var status_bar = $('');\n", - " nav_element.append(status_bar);\n", - " this.message = status_bar[0];\n", - "\n", - " // Add the close button to the window.\n", - " var buttongrp = $('
');\n", - " var button = $('');\n", - " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", - " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", - " buttongrp.append(button);\n", - " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", - " titlebar.prepend(buttongrp);\n", - "}\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(el){\n", - " var fig = this\n", - " el.on(\"remove\", function(){\n", - "\tfig.close_ws(fig, {});\n", - " });\n", - "}\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(el){\n", - " // this is important to make the div 'focusable\n", - " el.attr('tabindex', 0)\n", - " // reach out to IPython and tell the keyboard manager to turn it's self\n", - " // off when our div gets focus\n", - "\n", - " // location in version 3\n", - " if (IPython.notebook.keyboard_manager) {\n", - " IPython.notebook.keyboard_manager.register_events(el);\n", - " }\n", - " else {\n", - " // location in version 2\n", - " IPython.keyboard_manager.register_events(el);\n", - " }\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._key_event_extra = function(event, name) {\n", - " var manager = IPython.notebook.keyboard_manager;\n", - " if (!manager)\n", - " manager = IPython.keyboard_manager;\n", - "\n", - " // Check for shift+enter\n", - " if (event.shiftKey && event.which == 13) {\n", - " this.canvas_div.blur();\n", - " // select the cell after this one\n", - " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", - " IPython.notebook.select(index + 1);\n", - " }\n", - "}\n", - "\n", - "mpl.figure.prototype.handle_save = function(fig, msg) {\n", - " fig.ondownload(fig, null);\n", - "}\n", - "\n", - "\n", - "mpl.find_output_cell = function(html_output) {\n", - " // Return the cell and output element which can be found *uniquely* in the notebook.\n", - " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", - " // IPython event is triggered only after the cells have been serialised, which for\n", - " // our purposes (turning an active figure into a static one), is too late.\n", - " var cells = IPython.notebook.get_cells();\n", - " var ncells = cells.length;\n", - " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", - " data = data.data;\n", - " }\n", - " if (data['text/html'] == html_output) {\n", - " return [cell, data, j];\n", - " }\n", - " }\n", - " }\n", - " }\n", - "}\n", - "\n", - "// Register the function which deals with the matplotlib target/channel.\n", - "// The kernel may be null if the page has been refreshed.\n", - "if (IPython.notebook.kernel != null) {\n", - " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", - "}\n" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", - " button.click(method_name, toolbar_event);\n", - " button.mouseover(tooltip, toolbar_mouse_event);\n", - " nav_element.append(button);\n", - " }\n", - "\n", - " // Add the status bar.\n", - " var status_bar = $('');\n", - " nav_element.append(status_bar);\n", - " this.message = status_bar[0];\n", - "\n", - " // Add the close button to the window.\n", - " var buttongrp = $('
');\n", - " var button = $('');\n", - " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", - " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", - " buttongrp.append(button);\n", - " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", - " titlebar.prepend(buttongrp);\n", - "}\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(el){\n", - " var fig = this\n", - " el.on(\"remove\", function(){\n", - "\tfig.close_ws(fig, {});\n", - " });\n", - "}\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(el){\n", - " // this is important to make the div 'focusable\n", - " el.attr('tabindex', 0)\n", - " // reach out to IPython and tell the keyboard manager to turn it's self\n", - " // off when our div gets focus\n", - "\n", - " // location in version 3\n", - " if (IPython.notebook.keyboard_manager) {\n", - " IPython.notebook.keyboard_manager.register_events(el);\n", - " }\n", - " else {\n", - " // location in version 2\n", - " IPython.keyboard_manager.register_events(el);\n", - " }\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._key_event_extra = function(event, name) {\n", - " var manager = IPython.notebook.keyboard_manager;\n", - " if (!manager)\n", - " manager = IPython.keyboard_manager;\n", - "\n", - " // Check for shift+enter\n", - " if (event.shiftKey && event.which == 13) {\n", - " this.canvas_div.blur();\n", - " // select the cell after this one\n", - " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", - " IPython.notebook.select(index + 1);\n", - " }\n", - "}\n", - "\n", - "mpl.figure.prototype.handle_save = function(fig, msg) {\n", - " fig.ondownload(fig, null);\n", - "}\n", - "\n", - "\n", - "mpl.find_output_cell = function(html_output) {\n", - " // Return the cell and output element which can be found *uniquely* in the notebook.\n", - " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", - " // IPython event is triggered only after the cells have been serialised, which for\n", - " // our purposes (turning an active figure into a static one), is too late.\n", - " var cells = IPython.notebook.get_cells();\n", - " var ncells = cells.length;\n", - " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", - " data = data.data;\n", - " }\n", - " if (data['text/html'] == html_output) {\n", - " return [cell, data, j];\n", - " }\n", - " }\n", - " }\n", - " }\n", - "}\n", - "\n", - "// Register the function which deals with the matplotlib target/channel.\n", - "// The kernel may be null if the page has been refreshed.\n", - "if (IPython.notebook.kernel != null) {\n", - " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", - "}\n" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import random\n", - "\n", - "random.seed('Some seed such that numbers generated are predictable')\n", - "parameters = {parameter_name: random.random() * 10 - 5 for parameter_name in all_epsilons}\n", - "\n", - "%matplotlib notebook\n", - "from qctoolkit.pulses import plot\n", - "plot(gates[0], parameters)\n", - "plot(gates[1], parameters)\n", - "plot(sequences[1], parameters)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We now must construct the $S_k'$. For simplicity, we assume that there is only one initialization and one measurement pulse which are defined somehow and define only stubs here. We also define a waiting pulse with a variable length:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# stub for an initialization pulse of length 4\n", - "init = TablePulseTemplate({0: [(0, 5), (4, 0, 'linear')]})\n", - "\n", - "# stub for a measurement pulse of length 12\n", - "measure = TablePulseTemplate({0: [(0, 0), (12, 5, 'linear')]})\n", - "\n", - "# a wating pulse\n", - "wait = TablePulseTemplate({0: [(0, 0), ('wait_duration', 0)]})" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For our example, let us assume that we want all $S_k'$ to take 200 ns (since we've chosen the $S_k$ to be rather short). We know that the duration of our gate pulses in nanoseconds is equal to the number of entries in the `TablePulseTemplate` objects (each voltage level is held for one unit of time in the tables which corresponds to $\\Delta t = 1$ ns). Accordingly, the init pulse lasts for 4 ns and the measure pulse for 12 ns. The required length of the wait pulse can then be computed as follows:" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[175, 178, 176]\n" - ] - } - ], - "source": [ - "wait_times = []\n", - "desired_time = 200\n", - "\n", - "for m_k in m:\n", - " duration_k = 4 + 12 # init + measurement duration\n", - " for g_ki in m_k:\n", - " duration_k += len(gates[g_ki].entries) # add the number of entries of all gates in the sequence\n", - " wait_time_k = desired_time - duration_k\n", - " wait_times.append(wait_time_k)\n", - " \n", - "print(wait_times)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Finally we can construct the $S_k'$:" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "# an identity mapping for all epsilons\n", - "all_epsilons_map = {param_name: param_name for param_name in all_epsilons}\n", - "\n", - "gates_with_init_and_readout = [SequencePulseTemplate((wait, {'wait_duration': wait_duration}),\n", - " init,\n", - " gate,\n", - " measure)\n", - " for gate, wait_duration in zip(sequences, wait_times)]\n", - "final_sequence = SequencePulseTemplate(*gates_with_init_and_readout)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let us plot $S_1'$ to see whether we've accomplished our goal:" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib notebook\n", + "from qctoolkit.pulses import TablePT\n", + "from qctoolkit.pulses.plotting import plot\n", + "\n", + "table_pulse = TablePT({'A': [(0, 'v_a'),\n", + " ('t_ramp', 'v_b', 'linear')]})\n", + "_ = plot(table_pulse, dict(t_ramp=10, v_a=-1, v_b=1), sample_rate=100)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now, we want to restrict the ramp rate of the pulse to some maximum ramp rate `max_rate` and the ramp time to be larger than 1:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'max_rate', 'v_b', 'v_a', 't_ramp'}\n", + "Abs(v_a - v_b)/t_ramp < max_rate\n", + "t_ramp > 1\n" + ] + } + ], + "source": [ + "table_pulse = TablePT({'A': [(0, 'v_a'),\n", + " ('t_ramp', 'v_b', 'linear')]},\n", + " parameter_constraints=['Abs(v_a-v_b)/t_ramp < max_rate', 't_ramp>1'])\n", + "print(table_pulse.parameter_names)\n", + "print(table_pulse.parameter_constraints[0])\n", + "print(table_pulse.parameter_constraints[1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We see that the pulse got the extra parameter `max_rate`. We cannot instantiate this pulse without providing this parameter." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ParameterNotProvidedException: No value was provided for parameter ''max_rate'' \n" + ] + } + ], + "source": [ + "try:\n", + " _ = plot(table_pulse, dict(t_ramp=10, v_a=-1, v_b=1), sample_rate=100)\n", + "except Exception as exception:\n", + " print('{}: {}'.format(type(exception).__name__, exception))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If one of the constraints is violated an exception is raised:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ParameterConstraintViolation: The constraint 'Abs(v_a - v_b)/t_ramp < max_rate' is not fulfilled.\n", + "Parameters: {'max_rate': 0.1, 'v_b': 1, 'v_a': -1, 't_ramp': 10}\n" + ] + } + ], + "source": [ + "try:\n", + " _ = plot(table_pulse, dict(t_ramp=10, v_a=-1, v_b=1, max_rate=0.1), sample_rate=100)\n", + "except Exception as exception:\n", + " print('{}: {}'.format(type(exception).__name__, exception))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python [conda env:qctoolkit]", + "language": "python", + "name": "conda-env-qctoolkit-py" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/doc/source/examples/10MultiChannelTemplates.ipynb b/doc/source/examples/10MultiChannelTemplates.ipynb deleted file mode 100644 index 646b06115..000000000 --- a/doc/source/examples/10MultiChannelTemplates.ipynb +++ /dev/null @@ -1,2535 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Multi-Channel Pulses\n", - "\n", - "Usually there is a need to define pulses for multiple control channels simulateously. While this would be possible by simply defining several separate pulse templates (one for each channel), the `qctoolkit` also allows to define pulse templates directly for multiple channels or combine existing templates in a multi-channel way. This tutorial explores these possibilities.\n", - "\n", - "## A Multi-Channel Table Pulse\n", - "`TablePulseTemplate` allows to model multiple channel in a straighforward way: We simply initialize our `TablePulseTemplate` instance with the `channels` argument. When we then add new entries to it, we specify the channel for the entry with the `channel` argument of the `add_entry` method. Note that this is zero-indexed. We may share parameters between the channels. The following example constructs a 2-channel table pulse template with shared parameters and plots it." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", - " button.click(method_name, toolbar_event);\n", - " button.mouseover(tooltip, toolbar_mouse_event);\n", - " nav_element.append(button);\n", - " }\n", - "\n", - " // Add the status bar.\n", - " var status_bar = $('');\n", - " nav_element.append(status_bar);\n", - " this.message = status_bar[0];\n", - "\n", - " // Add the close button to the window.\n", - " var buttongrp = $('
');\n", - " var button = $('');\n", - " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", - " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", - " buttongrp.append(button);\n", - " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", - " titlebar.prepend(buttongrp);\n", - "}\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(el){\n", - " var fig = this\n", - " el.on(\"remove\", function(){\n", - "\tfig.close_ws(fig, {});\n", - " });\n", - "}\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(el){\n", - " // this is important to make the div 'focusable\n", - " el.attr('tabindex', 0)\n", - " // reach out to IPython and tell the keyboard manager to turn it's self\n", - " // off when our div gets focus\n", - "\n", - " // location in version 3\n", - " if (IPython.notebook.keyboard_manager) {\n", - " IPython.notebook.keyboard_manager.register_events(el);\n", - " }\n", - " else {\n", - " // location in version 2\n", - " IPython.keyboard_manager.register_events(el);\n", - " }\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._key_event_extra = function(event, name) {\n", - " var manager = IPython.notebook.keyboard_manager;\n", - " if (!manager)\n", - " manager = IPython.keyboard_manager;\n", - "\n", - " // Check for shift+enter\n", - " if (event.shiftKey && event.which == 13) {\n", - " this.canvas_div.blur();\n", - " // select the cell after this one\n", - " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", - " IPython.notebook.select(index + 1);\n", - " }\n", - "}\n", - "\n", - "mpl.figure.prototype.handle_save = function(fig, msg) {\n", - " fig.ondownload(fig, null);\n", - "}\n", - "\n", - "\n", - "mpl.find_output_cell = function(html_output) {\n", - " // Return the cell and output element which can be found *uniquely* in the notebook.\n", - " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", - " // IPython event is triggered only after the cells have been serialised, which for\n", - " // our purposes (turning an active figure into a static one), is too late.\n", - " var cells = IPython.notebook.get_cells();\n", - " var ncells = cells.length;\n", - " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", - " data = data.data;\n", - " }\n", - " if (data['text/html'] == html_output) {\n", - " return [cell, data, j];\n", - " }\n", - " }\n", - " }\n", - " }\n", - "}\n", - "\n", - "// Register the function which deals with the matplotlib target/channel.\n", - "// The kernel may be null if the page has been refreshed.\n", - "if (IPython.notebook.kernel != null) {\n", - " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", - "}\n" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The number of channels in function_template is 1.\n", - "The number of channels in template is 3.\n" - ] - } - ], - "source": [ - "from qctoolkit.pulses import FunctionPulseTemplate, MultiChannelPulseTemplate\n", - "\n", - "function_template = FunctionPulseTemplate('-sin(t)**2', '10', identifier='function-template', channel='chan_A')\n", - "\n", - "template = MultiChannelPulseTemplate(\n", - " [(function_template, dict(), {'chan_A': 1}),\n", - " (table_template, dict(foo='5', bar='2 * hugo'), {0: 'rectangle', 1: 'triangle'})],\n", - " {'hugo'},\n", - " identifier='3-channel-combined-template'\n", - ")\n", - "\n", - "_ = plot(template, dict(hugo=-1.3), sample_rate=100)\n", - "print(\"The number of channels in function_template is {}.\".format(function_template.num_channels))\n", - "print(\"The number of channels in template is {}.\".format(template.num_channels))" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The syntax for the subtemplates passed into the constructor of `MultiChannelPulseTemplate` is `(template, dictionary of parameters mappings, channel mapping array)`. A value $j$ at the $i$-th index of the channel mapping array indicates that this channel of the subtemplate shall be mapped to the $j$-th channel of the newly created `MultiChannelPulseTemplate`. Note that an exception will be raised during the sampling of the waveforms (i.e., during the sequencing process) if the subtemplates have different length." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Multiple Channels in Non-Atomic Templates\n", - "\n", - "All higher order template, i.e., `SequencePulseTemplate`, `LoopPulseTemplate` and `BranchPulseTemplate` also support multiple channels insofar as that they can be composed using multi-channel atomic templates as subtemplates. They require that all these subtemplates define an equal amount of channels and raise an exception if that is not the case. The following example constructs a `SequencePulseTempate` `sequence_template` by chaining the above defined two-channel `table_template`. In the second instance of `table_template` in the sequence, we swap the channels by wrapping a `MultiChannelPulseTemplate` around it." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "application/javascript": [ - "/* Put everything inside the global mpl namespace */\n", - "window.mpl = {};\n", - "\n", - "\n", - "mpl.get_websocket_type = function() {\n", - " if (typeof(WebSocket) !== 'undefined') {\n", - " return WebSocket;\n", - " } else if (typeof(MozWebSocket) !== 'undefined') {\n", - " return MozWebSocket;\n", - " } else {\n", - " alert('Your browser does not have WebSocket support.' +\n", - " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", - " 'Firefox 4 and 5 are also supported but you ' +\n", - " 'have to enable WebSockets in about:config.');\n", - " };\n", - "}\n", - "\n", - "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", - " this.id = figure_id;\n", - "\n", - " this.ws = websocket;\n", - "\n", - " this.supports_binary = (this.ws.binaryType != undefined);\n", - "\n", - " if (!this.supports_binary) {\n", - " var warnings = document.getElementById(\"mpl-warnings\");\n", - " if (warnings) {\n", - " warnings.style.display = 'block';\n", - " warnings.textContent = (\n", - " \"This browser does not support binary websocket messages. \" +\n", - " \"Performance may be slow.\");\n", - " }\n", - " }\n", - "\n", - " this.imageObj = new Image();\n", - "\n", - " this.context = undefined;\n", - " this.message = undefined;\n", - " this.canvas = undefined;\n", - " this.rubberband_canvas = undefined;\n", - " this.rubberband_context = undefined;\n", - " this.format_dropdown = undefined;\n", - "\n", - " this.image_mode = 'full';\n", - "\n", - " this.root = $('
');\n", - " this._root_extra_style(this.root)\n", - " this.root.attr('style', 'display: inline-block');\n", - "\n", - " $(parent_element).append(this.root);\n", - "\n", - " this._init_header(this);\n", - " this._init_canvas(this);\n", - " this._init_toolbar(this);\n", - "\n", - " var fig = this;\n", - "\n", - " this.waiting = false;\n", - "\n", - " this.ws.onopen = function () {\n", - " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", - " fig.send_message(\"send_image_mode\", {});\n", - " if (mpl.ratio != 1) {\n", - " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", - " }\n", - " fig.send_message(\"refresh\", {});\n", - " }\n", - "\n", - " this.imageObj.onload = function() {\n", - " if (fig.image_mode == 'full') {\n", - " // Full images could contain transparency (where diff images\n", - " // almost always do), so we need to clear the canvas so that\n", - " // there is no ghosting.\n", - " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", - " }\n", - " fig.context.drawImage(fig.imageObj, 0, 0);\n", - " };\n", - "\n", - " this.imageObj.onunload = function() {\n", - " this.ws.close();\n", - " }\n", - "\n", - " this.ws.onmessage = this._make_on_message_function(this);\n", - "\n", - " this.ondownload = ondownload;\n", - "}\n", - "\n", - "mpl.figure.prototype._init_header = function() {\n", - " var titlebar = $(\n", - " '
');\n", - " var titletext = $(\n", - " '
');\n", - " titlebar.append(titletext)\n", - " this.root.append(titlebar);\n", - " this.header = titletext[0];\n", - "}\n", - "\n", - "\n", - "\n", - "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "\n", - "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", - "\n", - "}\n", - "\n", - "mpl.figure.prototype._init_canvas = function() {\n", - " var fig = this;\n", - "\n", - " var canvas_div = $('
');\n", - "\n", - " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", - "\n", - " function canvas_keyboard_event(event) {\n", - " return fig.key_event(event, event['data']);\n", - " }\n", - "\n", - " canvas_div.keydown('key_press', canvas_keyboard_event);\n", - " canvas_div.keyup('key_release', canvas_keyboard_event);\n", - " this.canvas_div = canvas_div\n", - " this._canvas_extra_style(canvas_div)\n", - " this.root.append(canvas_div);\n", - "\n", - " var canvas = $('');\n", - " canvas.addClass('mpl-canvas');\n", - " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", - "\n", - " this.canvas = canvas[0];\n", - " this.context = canvas[0].getContext(\"2d\");\n", - "\n", - " var backingStore = this.context.backingStorePixelRatio ||\n", - "\tthis.context.webkitBackingStorePixelRatio ||\n", - "\tthis.context.mozBackingStorePixelRatio ||\n", - "\tthis.context.msBackingStorePixelRatio ||\n", - "\tthis.context.oBackingStorePixelRatio ||\n", - "\tthis.context.backingStorePixelRatio || 1;\n", - "\n", - " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", - "\n", - " var rubberband = $('');\n", - " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", - "\n", - " var pass_mouse_events = true;\n", - "\n", - " canvas_div.resizable({\n", - " start: function(event, ui) {\n", - " pass_mouse_events = false;\n", - " },\n", - " resize: function(event, ui) {\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " stop: function(event, ui) {\n", - " pass_mouse_events = true;\n", - " fig.request_resize(ui.size.width, ui.size.height);\n", - " },\n", - " });\n", - "\n", - " function mouse_event_fn(event) {\n", - " if (pass_mouse_events)\n", - " return fig.mouse_event(event, event['data']);\n", - " }\n", - "\n", - " rubberband.mousedown('button_press', mouse_event_fn);\n", - " rubberband.mouseup('button_release', mouse_event_fn);\n", - " // Throttle sequential mouse events to 1 every 20ms.\n", - " rubberband.mousemove('motion_notify', mouse_event_fn);\n", - "\n", - " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", - " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", - "\n", - " canvas_div.on(\"wheel\", function (event) {\n", - " event = event.originalEvent;\n", - " event['data'] = 'scroll'\n", - " if (event.deltaY < 0) {\n", - " event.step = 1;\n", - " } else {\n", - " event.step = -1;\n", - " }\n", - " mouse_event_fn(event);\n", - " });\n", - "\n", - " canvas_div.append(canvas);\n", - " canvas_div.append(rubberband);\n", - "\n", - " this.rubberband = rubberband;\n", - " this.rubberband_canvas = rubberband[0];\n", - " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", - " this.rubberband_context.strokeStyle = \"#000000\";\n", - "\n", - " this._resize_canvas = function(width, height) {\n", - " // Keep the size of the canvas, canvas container, and rubber band\n", - " // canvas in synch.\n", - " canvas_div.css('width', width)\n", - " canvas_div.css('height', height)\n", - "\n", - " canvas.attr('width', width * mpl.ratio);\n", - " canvas.attr('height', height * mpl.ratio);\n", - " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", - "\n", - " rubberband.attr('width', width);\n", - " rubberband.attr('height', height);\n", - " }\n", - "\n", - " // Set the figure to an initial 600x600px, this will subsequently be updated\n", - " // upon first draw.\n", - " this._resize_canvas(600, 600);\n", - "\n", - " // Disable right mouse context menu.\n", - " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", - " return false;\n", - " });\n", - "\n", - " function set_focus () {\n", - " canvas.focus();\n", - " canvas_div.focus();\n", - " }\n", - "\n", - " window.setTimeout(set_focus, 100);\n", - "}\n", - "\n", - "mpl.figure.prototype._init_toolbar = function() {\n", - " var fig = this;\n", - "\n", - " var nav_element = $('
')\n", - " nav_element.attr('style', 'width: 100%');\n", - " this.root.append(nav_element);\n", - "\n", - " // Define a callback function for later on.\n", - " function toolbar_event(event) {\n", - " return fig.toolbar_button_onclick(event['data']);\n", - " }\n", - " function toolbar_mouse_event(event) {\n", - " return fig.toolbar_button_onmouseover(event['data']);\n", - " }\n", - "\n", - " for(var toolbar_ind in mpl.toolbar_items) {\n", - " var name = mpl.toolbar_items[toolbar_ind][0];\n", - " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", - " var image = mpl.toolbar_items[toolbar_ind][2];\n", - " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", - "\n", - " if (!name) {\n", - " // put a spacer in here.\n", - " continue;\n", - " }\n", - " var button = $('');\n", + " button.click(method_name, toolbar_event);\n", + " button.mouseover(tooltip, toolbar_mouse_event);\n", + " nav_element.append(button);\n", + " }\n", + "\n", + " // Add the status bar.\n", + " var status_bar = $('');\n", + " nav_element.append(status_bar);\n", + " this.message = status_bar[0];\n", + "\n", + " // Add the close button to the window.\n", + " var buttongrp = $('
');\n", + " var button = $('');\n", + " button.click(function (evt) { fig.handle_close(fig, {}); } );\n", + " button.mouseover('Stop Interaction', toolbar_mouse_event);\n", + " buttongrp.append(button);\n", + " var titlebar = this.root.find($('.ui-dialog-titlebar'));\n", + " titlebar.prepend(buttongrp);\n", + "}\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(el){\n", + " var fig = this\n", + " el.on(\"remove\", function(){\n", + "\tfig.close_ws(fig, {});\n", + " });\n", + "}\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(el){\n", + " // this is important to make the div 'focusable\n", + " el.attr('tabindex', 0)\n", + " // reach out to IPython and tell the keyboard manager to turn it's self\n", + " // off when our div gets focus\n", + "\n", + " // location in version 3\n", + " if (IPython.notebook.keyboard_manager) {\n", + " IPython.notebook.keyboard_manager.register_events(el);\n", + " }\n", + " else {\n", + " // location in version 2\n", + " IPython.keyboard_manager.register_events(el);\n", + " }\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._key_event_extra = function(event, name) {\n", + " var manager = IPython.notebook.keyboard_manager;\n", + " if (!manager)\n", + " manager = IPython.keyboard_manager;\n", + "\n", + " // Check for shift+enter\n", + " if (event.shiftKey && event.which == 13) {\n", + " this.canvas_div.blur();\n", + " // select the cell after this one\n", + " var index = IPython.notebook.find_cell_index(this.cell_info[0]);\n", + " IPython.notebook.select(index + 1);\n", + " }\n", + "}\n", + "\n", + "mpl.figure.prototype.handle_save = function(fig, msg) {\n", + " fig.ondownload(fig, null);\n", + "}\n", + "\n", + "\n", + "mpl.find_output_cell = function(html_output) {\n", + " // Return the cell and output element which can be found *uniquely* in the notebook.\n", + " // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n", + " // IPython event is triggered only after the cells have been serialised, which for\n", + " // our purposes (turning an active figure into a static one), is too late.\n", + " var cells = IPython.notebook.get_cells();\n", + " var ncells = cells.length;\n", + " for (var i=0; i= 3 moved mimebundle to data attribute of output\n", + " data = data.data;\n", + " }\n", + " if (data['text/html'] == html_output) {\n", + " return [cell, data, j];\n", + " }\n", + " }\n", + " }\n", + " }\n", + "}\n", + "\n", + "// Register the function which deals with the matplotlib target/channel.\n", + "// The kernel may be null if the page has been refreshed.\n", + "if (IPython.notebook.kernel != null) {\n", + " IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n", + "}\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%matplotlib notebook\n", + "\n", + "example_values = dict(meas=[0, 0],\n", + " op=[5, -5],\n", + " eps_J=[1, -1],\n", + " ST_plus=[2.5, -2.5],\n", + " S_init=[-1, -1],\n", + " ST_jump=[1, -1],\n", + " max_ramp_speed=0.3,\n", + " \n", + " t_init=5,\n", + " \n", + " t_meas_wait = 1,\n", + " \n", + " t_ST_prep = 10,\n", + " t_op = 20,\n", + " \n", + " t_ST_read = 10,\n", + " t_meas_start = 20,\n", + " t_meas_duration=5,\n", + " \n", + " t_start=0,\n", + " t_step=5,\n", + " N_fid_steps=5, N_repetitions=2)\n", + "\n", + "# convert lists to numpy arrays\n", + "example_values = {k: np.array(v) if isinstance(v, list) else v\n", + " for k, v in example_values.items()}\n", + "from qctoolkit.pulses.plotting import plot\n", + "\n", + "_ = plot(experiment, example_values)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "application/javascript": [ + "/* Put everything inside the global mpl namespace */\n", + "window.mpl = {};\n", + "\n", + "\n", + "mpl.get_websocket_type = function() {\n", + " if (typeof(WebSocket) !== 'undefined') {\n", + " return WebSocket;\n", + " } else if (typeof(MozWebSocket) !== 'undefined') {\n", + " return MozWebSocket;\n", + " } else {\n", + " alert('Your browser does not have WebSocket support.' +\n", + " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", + " 'Firefox 4 and 5 are also supported but you ' +\n", + " 'have to enable WebSockets in about:config.');\n", + " };\n", + "}\n", + "\n", + "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", + " this.id = figure_id;\n", + "\n", + " this.ws = websocket;\n", + "\n", + " this.supports_binary = (this.ws.binaryType != undefined);\n", + "\n", + " if (!this.supports_binary) {\n", + " var warnings = document.getElementById(\"mpl-warnings\");\n", + " if (warnings) {\n", + " warnings.style.display = 'block';\n", + " warnings.textContent = (\n", + " \"This browser does not support binary websocket messages. \" +\n", + " \"Performance may be slow.\");\n", + " }\n", + " }\n", + "\n", + " this.imageObj = new Image();\n", + "\n", + " this.context = undefined;\n", + " this.message = undefined;\n", + " this.canvas = undefined;\n", + " this.rubberband_canvas = undefined;\n", + " this.rubberband_context = undefined;\n", + " this.format_dropdown = undefined;\n", + "\n", + " this.image_mode = 'full';\n", + "\n", + " this.root = $('
');\n", + " this._root_extra_style(this.root)\n", + " this.root.attr('style', 'display: inline-block');\n", + "\n", + " $(parent_element).append(this.root);\n", + "\n", + " this._init_header(this);\n", + " this._init_canvas(this);\n", + " this._init_toolbar(this);\n", + "\n", + " var fig = this;\n", + "\n", + " this.waiting = false;\n", + "\n", + " this.ws.onopen = function () {\n", + " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", + " fig.send_message(\"send_image_mode\", {});\n", + " if (mpl.ratio != 1) {\n", + " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", + " }\n", + " fig.send_message(\"refresh\", {});\n", + " }\n", + "\n", + " this.imageObj.onload = function() {\n", + " if (fig.image_mode == 'full') {\n", + " // Full images could contain transparency (where diff images\n", + " // almost always do), so we need to clear the canvas so that\n", + " // there is no ghosting.\n", + " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", + " }\n", + " fig.context.drawImage(fig.imageObj, 0, 0);\n", + " };\n", + "\n", + " this.imageObj.onunload = function() {\n", + " this.ws.close();\n", + " }\n", + "\n", + " this.ws.onmessage = this._make_on_message_function(this);\n", + "\n", + " this.ondownload = ondownload;\n", + "}\n", + "\n", + "mpl.figure.prototype._init_header = function() {\n", + " var titlebar = $(\n", + " '
');\n", + " var titletext = $(\n", + " '
');\n", + " titlebar.append(titletext)\n", + " this.root.append(titlebar);\n", + " this.header = titletext[0];\n", + "}\n", + "\n", + "\n", + "\n", + "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "\n", + "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", + "\n", + "}\n", + "\n", + "mpl.figure.prototype._init_canvas = function() {\n", + " var fig = this;\n", + "\n", + " var canvas_div = $('
');\n", + "\n", + " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", + "\n", + " function canvas_keyboard_event(event) {\n", + " return fig.key_event(event, event['data']);\n", + " }\n", + "\n", + " canvas_div.keydown('key_press', canvas_keyboard_event);\n", + " canvas_div.keyup('key_release', canvas_keyboard_event);\n", + " this.canvas_div = canvas_div\n", + " this._canvas_extra_style(canvas_div)\n", + " this.root.append(canvas_div);\n", + "\n", + " var canvas = $('');\n", + " canvas.addClass('mpl-canvas');\n", + " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", + "\n", + " this.canvas = canvas[0];\n", + " this.context = canvas[0].getContext(\"2d\");\n", + "\n", + " var backingStore = this.context.backingStorePixelRatio ||\n", + "\tthis.context.webkitBackingStorePixelRatio ||\n", + "\tthis.context.mozBackingStorePixelRatio ||\n", + "\tthis.context.msBackingStorePixelRatio ||\n", + "\tthis.context.oBackingStorePixelRatio ||\n", + "\tthis.context.backingStorePixelRatio || 1;\n", + "\n", + " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", + "\n", + " var rubberband = $('');\n", + " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", + "\n", + " var pass_mouse_events = true;\n", + "\n", + " canvas_div.resizable({\n", + " start: function(event, ui) {\n", + " pass_mouse_events = false;\n", + " },\n", + " resize: function(event, ui) {\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " stop: function(event, ui) {\n", + " pass_mouse_events = true;\n", + " fig.request_resize(ui.size.width, ui.size.height);\n", + " },\n", + " });\n", + "\n", + " function mouse_event_fn(event) {\n", + " if (pass_mouse_events)\n", + " return fig.mouse_event(event, event['data']);\n", + " }\n", + "\n", + " rubberband.mousedown('button_press', mouse_event_fn);\n", + " rubberband.mouseup('button_release', mouse_event_fn);\n", + " // Throttle sequential mouse events to 1 every 20ms.\n", + " rubberband.mousemove('motion_notify', mouse_event_fn);\n", + "\n", + " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", + " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", + "\n", + " canvas_div.on(\"wheel\", function (event) {\n", + " event = event.originalEvent;\n", + " event['data'] = 'scroll'\n", + " if (event.deltaY < 0) {\n", + " event.step = 1;\n", + " } else {\n", + " event.step = -1;\n", + " }\n", + " mouse_event_fn(event);\n", + " });\n", + "\n", + " canvas_div.append(canvas);\n", + " canvas_div.append(rubberband);\n", + "\n", + " this.rubberband = rubberband;\n", + " this.rubberband_canvas = rubberband[0];\n", + " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", + " this.rubberband_context.strokeStyle = \"#000000\";\n", + "\n", + " this._resize_canvas = function(width, height) {\n", + " // Keep the size of the canvas, canvas container, and rubber band\n", + " // canvas in synch.\n", + " canvas_div.css('width', width)\n", + " canvas_div.css('height', height)\n", + "\n", + " canvas.attr('width', width * mpl.ratio);\n", + " canvas.attr('height', height * mpl.ratio);\n", + " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", + "\n", + " rubberband.attr('width', width);\n", + " rubberband.attr('height', height);\n", + " }\n", + "\n", + " // Set the figure to an initial 600x600px, this will subsequently be updated\n", + " // upon first draw.\n", + " this._resize_canvas(600, 600);\n", + "\n", + " // Disable right mouse context menu.\n", + " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", + " return false;\n", + " });\n", + "\n", + " function set_focus () {\n", + " canvas.focus();\n", + " canvas_div.focus();\n", + " }\n", + "\n", + " window.setTimeout(set_focus, 100);\n", + "}\n", + "\n", + "mpl.figure.prototype._init_toolbar = function() {\n", + " var fig = this;\n", + "\n", + " var nav_element = $('
')\n", + " nav_element.attr('style', 'width: 100%');\n", + " this.root.append(nav_element);\n", + "\n", + " // Define a callback function for later on.\n", + " function toolbar_event(event) {\n", + " return fig.toolbar_button_onclick(event['data']);\n", + " }\n", + " function toolbar_mouse_event(event) {\n", + " return fig.toolbar_button_onmouseover(event['data']);\n", + " }\n", + "\n", + " for(var toolbar_ind in mpl.toolbar_items) {\n", + " var name = mpl.toolbar_items[toolbar_ind][0];\n", + " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", + " var image = mpl.toolbar_items[toolbar_ind][2];\n", + " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", + "\n", + " if (!name) {\n", + " // put a spacer in here.\n", + " continue;\n", + " }\n", + " var button = $('