Skip to content
Snippets Groups Projects
Commit 72b4b425 authored by TUD278427's avatar TUD278427
Browse files

make hardware snapshottable

parent 1c4c8116
No related branches found
No related tags found
No related merge requests found
...@@ -2,142 +2,160 @@ from dataclasses import dataclass ...@@ -2,142 +2,160 @@ from dataclasses import dataclass
import qcodes as qc import qcodes as qc
import numpy as np import numpy as np
import shelve import shelve
from typing import Sequence
@dataclass @dataclass
class virtual_gate: class virtual_gate:
name:str name:str
real_gate_names: list real_gate_names: list
virtual_gate_names: list virtual_gate_names: list
virtual_gate_matrix: np.ndarray virtual_gate_matrix: np.ndarray
def __init__(self, name, real_gate_names, virtual_gate_names=None): def __init__(self, name, real_gate_names, virtual_gate_names=None):
''' '''
generate a virtual gate object. generate a virtual gate object.
Args: Args:
real_gate_names (list<str>) : list with the names of real gates real_gate_names (list<str>) : list with the names of real gates
virtual_gate_names (list<str>) : (optional) names of the virtual gates set. If not provided a "v" is inserted before the gate name. virtual_gate_names (list<str>) : (optional) names of the virtual gates set. If not provided a "v" is inserted before the gate name.
''' '''
self.name = name self.name = name
self.real_gate_names = real_gate_names self.real_gate_names = real_gate_names
self.virtual_gate_matrix = np.eye(len(real_gate_names)).data self.virtual_gate_matrix = np.eye(len(real_gate_names)).data
if virtual_gate_names != None: if virtual_gate_names != None:
self.virtual_gate_names = virtual_gate_names self.virtual_gate_names = virtual_gate_names
else: else:
self.virtual_gate_names = [] self.virtual_gate_names = []
for name in real_gate_names: for name in real_gate_names:
self.virtual_gate_names.append("v" + name) self.virtual_gate_names.append("v" + name)
if len(self.real_gate_names) != len(self.virtual_gate_names): if len(self.real_gate_names) != len(self.virtual_gate_names):
raise ValueError("number of real gates and virtual gates is not equal, please fix the input.") raise ValueError("number of real gates and virtual gates is not equal, please fix the input.")
def __len__(self): def __len__(self):
''' '''
get number of gate in the object. get number of gate in the object.
''' '''
return len(self.real_gate_names) return len(self.real_gate_names)
def __getstate__(self): def __getstate__(self):
''' '''
overwrite state methods so object becomes pickable. overwrite state methods so object becomes pickable.
''' '''
state = self.__dict__.copy() state = self.__dict__.copy()
state["virtual_gate_matrix"] = np.asarray(self.virtual_gate_matrix) state["virtual_gate_matrix"] = np.asarray(self.virtual_gate_matrix)
return state return state
def __setstate__(self, new_state): def __setstate__(self, new_state):
''' '''
overwrite state methods so object becomes pickable. overwrite state methods so object becomes pickable.
''' '''
new_state["virtual_gate_matrix"] = np.asarray(new_state["virtual_gate_matrix"]).data new_state["virtual_gate_matrix"] = np.asarray(new_state["virtual_gate_matrix"]).data
self.__dict__.update(new_state) self.__dict__.update(new_state)
class virtual_gates_mgr(list): class virtual_gates_mgr(list):
def __init__(self, sync_engine, *args): def __init__(self, sync_engine, *args):
super(virtual_gates_mgr, self).__init__(*args) super(virtual_gates_mgr, self).__init__(*args)
self.sync_engine = sync_engine self.sync_engine = sync_engine
def append(self, item): def append(self, item):
if not isinstance(item, virtual_gate): if not isinstance(item, virtual_gate):
raise ValueError("please provide the virtual gates with the virtual_gate data type. {} detected".format(type(item))) raise ValueError("please provide the virtual gates with the virtual_gate data type. {} detected".format(type(item)))
# check for uniqueness of the virtual gate names. # check for uniqueness of the virtual gate names.
virtual_gates = [] virtual_gates = []
virtual_gates += item.virtual_gate_names virtual_gates += item.virtual_gate_names
for i in self: for i in self:
virtual_gates += i.virtual_gate_names virtual_gates += i.virtual_gate_names
if len(np.unique(np.array(virtual_gates))) != len(virtual_gates): if len(np.unique(np.array(virtual_gates))) != len(virtual_gates):
raise ValueError("two duplicate names of virtual gates detected. Please fix this.") raise ValueError("two duplicate names of virtual gates detected. Please fix this.")
if item.name in list(self.sync_engine.keys()): if item.name in list(self.sync_engine.keys()):
item_in_ram = self.sync_engine[item.name] item_in_ram = self.sync_engine[item.name]
if item_in_ram.real_gate_names == item.real_gate_names: if item_in_ram.real_gate_names == item.real_gate_names:
np.asarray(item.virtual_gate_matrix)[:] = np.asarray(item_in_ram.virtual_gate_matrix)[:] np.asarray(item.virtual_gate_matrix)[:] = np.asarray(item_in_ram.virtual_gate_matrix)[:]
self.sync_engine[item.name] = item self.sync_engine[item.name] = item
return super(virtual_gates_mgr, self).append(item) return super(virtual_gates_mgr, self).append(item)
def __getitem__(self, row): def __getitem__(self, row):
if isinstance(row, int): if isinstance(row, int):
return super(virtual_gates_mgr, self).__getitem__(row) return super(virtual_gates_mgr, self).__getitem__(row)
if isinstance(row, str): if isinstance(row, str):
row = self.index(row) row = self.index(row)
return super(virtual_gates_mgr, self).__getitem__(row) return super(virtual_gates_mgr, self).__getitem__(row)
raise ValueError("Invalid key (name) {} provided for the virtual_gate object.") raise ValueError("Invalid key (name) {} provided for the virtual_gate object.")
def index(self, name): def index(self, name):
i = 0 i = 0
options = [] options = []
for v_gate_item in self: for v_gate_item in self:
options.append(v_gate_item.name) options.append(v_gate_item.name)
if v_gate_item.name == name: if v_gate_item.name == name:
return i return i
i += 1 i += 1
if len(options) == 0: if len(options) == 0:
raise ValueError("Trying to get find a virtual gate matrix, but no matrix is defined.") raise ValueError("Trying to get find a virtual gate matrix, but no matrix is defined.")
raise ValueError("{} is not defined as a virtual gate. The options are, {}".format(name,options)) raise ValueError("{} is not defined as a virtual gate. The options are, {}".format(name,options))
class harware_parent(qc.Instrument): class harware_parent(qc.Instrument):
"""docstring for harware_parent -- init a empy hardware object""" """docstring for harware_parent -- init a empy hardware object"""
def __init__(self, sample_name, storage_location): def __init__(self, sample_name, storage_location):
super(harware_parent, self).__init__(sample_name) super(harware_parent, self).__init__(sample_name)
self.storage_location = storage_location self.storage_location = storage_location
self.sync = shelve.open(sample_name, flag='c', writeback=True) self.sync = shelve.open(sample_name, flag='c', writeback=True)
self.dac_gate_map = dict() self.dac_gate_map = dict()
self.boundaries = dict() self.boundaries = dict()
# set this one in the GUI. # set this one in the GUI.
self._AWG_to_dac_conversion = dict() self._AWG_to_dac_conversion = dict()
if 'AWG2DAC' in list(self.sync.keys()): if 'AWG2DAC' in list(self.sync.keys()):
self._AWG_to_dac_conversion = self.sync['AWG2DAC'] self._AWG_to_dac_conversion = self.sync['AWG2DAC']
self._virtual_gates = virtual_gates_mgr(self.sync) self._virtual_gates = virtual_gates_mgr(self.sync)
@property @property
def virtual_gates(self): def virtual_gates(self):
return self._virtual_gates return self._virtual_gates
@property @property
def AWG_to_dac_conversion(self): def AWG_to_dac_conversion(self):
return self._AWG_to_dac_conversion return self._AWG_to_dac_conversion
@AWG_to_dac_conversion.setter @AWG_to_dac_conversion.setter
def AWG_to_dac_conversion(self, AWG_to_dac_ratio): def AWG_to_dac_conversion(self, AWG_to_dac_ratio):
if self._AWG_to_dac_conversion.keys() == AWG_to_dac_ratio.keys(): if self._AWG_to_dac_conversion.keys() == AWG_to_dac_ratio.keys():
AWG_to_dac_ratio = self._AWG_to_dac_conversion AWG_to_dac_ratio = self._AWG_to_dac_conversion
else: else:
self._AWG_to_dac_conversion = AWG_to_dac_ratio self._AWG_to_dac_conversion = AWG_to_dac_ratio
def sync_data(self): def sync_data(self):
for item in self.virtual_gates: for item in self.virtual_gates:
self.sync[item.name] = item self.sync[item.name] = item
self.sync['AWG2DAC'] = self._AWG_to_dac_conversion self.sync['AWG2DAC'] = self._AWG_to_dac_conversion
self.sync.sync() self.sync.sync()
def snapshot_base(self, update: bool=False,
params_to_skip_update: Sequence[str]=None):
vg_snap = {}
for vg in self.virtual_gates:
vg_mat = np.reshape(np.frombuffer(vg.virtual_gate_matrix, dtype=float),np.shape(vg.virtual_gate_matrix))
vg_meta = {}
vg_meta['real_gate_names'] = vg.real_gate_names
vg_meta['virtual_gate_names'] = vg.virtual_gate_names
vg_meta['virtual_gate_matrix'] = vg_mat
vg_snap[vg.name] = vg_meta
self.snap = {'AWG_to_DAC': self.AWG_to_dac_conversion,
'dac_gate_map': self.dac_gate_map,
'virtual_gates': vg_snap
}
return self.snap
if __name__ == '__main__': if __name__ == '__main__':
from V2_software.drivers.virtual_gates.examples.hardware_example import hardware_example from V2_software.drivers.virtual_gates.examples.hardware_example import hardware_example
# example. # example.
hw = hardware_example("my_harware_example") hw = hardware_example("my_harware_example")
print(hw.virtual_gates) print(hw.virtual_gates)
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment