Skip to content

Commit

Permalink
Removing deprecated code and renaming new code #6 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Itaborala committed Jun 22, 2024
1 parent 9baf253 commit 45b9957
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 116 deletions.
83 changes: 16 additions & 67 deletions core/vqh_core_new.py → core/vqh_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from core.vqh_source import VQHSource, VQHSourceStrategy, VQHFileStrategy, VQHProblem, VQHProtocol, VQHProcess
from core.vqh_mapper import VQHMapper, VQHMappingStrategy
from core.vqh_process_test import ProcessTest, ProblemTest, ProtocolTest, MappingTest
from problem.qubo import QUBOProblem, QUBOProblemRT
from problem.qubo import QUBOProblem
from protocols.basis import BasisProtocol
from vqe.vqe_process import VQEProcess
from vqe.vqe_algorithm import VQEAlgorithm
Expand Down Expand Up @@ -30,9 +30,7 @@

PROCESS_LIBRARY = {
"test": (ProcessTest, ProblemTest, ProtocolTest), #Deprecated
"qubo": (VQEProcess, QUBOProblem, BasisProtocol), #Deprecated
"qubort": (VQEProcess, QUBOProblemRT, BasisProtocol), #Deprecated
"qubo_algo": (VQHProcess, VQEAlgorithm, QUBOProblemRT, BasisProtocol)
"qubo_algo": (VQHProcess, VQEAlgorithm, QUBOProblem, BasisProtocol)
}


Expand Down Expand Up @@ -75,8 +73,6 @@ def wait_for_source_and_mapper(source: VQHSource, mapper: VQHMapper):
if mapper.is_done and not mapper_finished:
mapper.stop()
mapper_finished = True
#print(source.is_done, mapper.is_done)
#print(source_finished, mapper_finished)

if source_finished and mapper_finished:
print("Ending Waiter Thread")
Expand All @@ -91,27 +87,17 @@ def __init__(self, strategy_type, strategy_name, hwi_name, son_type, rt_mode_nam
self.strategy_name = strategy_name
self.rt_mode = REALTIME_MODES[rt_mode_name]
self.session_name = session_name
#self.strategy = self.init_strategy()
#print(f"Strategy: {self.strategy}")
self.strategy = None
self.queue = Queue()

self.hardware_library = HardwareLibrary()
self.sonification_library = SonificationLibrary()

self.hardware_interface = self.hardware_library.get_hardware_interface(hwi_name)
#self.hardware_interface.connect()
#self.hardware_interface.get_backend()
#config.PLATFORM = self.hardware_interface
#self.init_hardware_interface()


self.son_type = son_type

#self.source = VQHSource(self.strategy)
self.source = None

#self.mapper = self.init_mapper()
self.mapper = None

def init_hardware_interface(self):
Expand Down Expand Up @@ -141,8 +127,8 @@ def init_strategy(self, strategy_name=None):


elif self.strategy_type == "process":
if self.strategy_name in ['test', 'qubo', 'qubort']:
print(f"This strategy '{self.strategy_name}' is deprecated. Use qubo_algo instead")
if self.strategy_name not in PROCESS_LIBRARY.keys() or self.strategy_name in ['test', None]:
print(f"This strategy '{self.strategy_name}' does not exist (or is deprecated). Use qubo_algo instead")
raise ValueError
return init_vqh_process(self.strategy_name, 'h_setup_rt.csv', self.rt_mode, self.problem_event, self.session_name)

Expand All @@ -154,7 +140,6 @@ def init_mapper(self):
print("Initializing mapper")
synth, mapping = self.sonification_library.get_mapping(self.son_type)

#return VQHMapper(mapping, synth, self.source.queue, clock_speed=0.1)
return VQHMapper(mapping, synth, self.queue, clock_speed=0.1)


Expand All @@ -177,22 +162,11 @@ def __init__(self, core: VQHCore):

self.outlet = VQHOutlet()

#self.qubos_inlet = VQHInlet(self.core.source.strategy.problem, 'qubos')

#self.clock_speed_inlet = VQHInlet(self.core.mapper, 'clock_speed')

#self.scale_inlet = VQHInlet(self.core.mapper.synthesizer.scale, 'current_scale')

#self.outlet.connect(self.qubos_inlet)
#self.outlet.connect(self.clock_speed_inlet)
#self.outlet.connect(self.scale_inlet)


self.current_state = {}
self.current_state["clock_speed"] = 0.05#self.core.mapper.clock_speed


def update_scale(self, scale):
def update_current_scale(self, scale):

if scale != self.core.mapper.synthesizer.scale.current_scale:
self.outlet.bang({"current_scale": scale})
Expand Down Expand Up @@ -283,43 +257,12 @@ def update_realtime(self, thread=None):
json.dump(rt_config, f)
sleep(1)

def update_realtime_old(self):
print(f"Realtime mode: {self.rt_mode}")
if self.rt_mode == 0:
print("No RT mode")
return
elif self.rt_mode == 1:
while self.is_active:
with open("rt_conf.json", "r") as f:
rt_config = json.load(f)
#print(f"Updating {rt_config}")

try:
if rt_config['scale'] != self.core.mapper.synthesizer.scale.current_scale:
self.outlet.bang({"current_scale": rt_config['scale']})
self.current_state["scale"] = rt_config['scale']
except Exception as e:
print('Synth not ready yet. skipping scale update')
continue
elif self.rt_mode == 2:
raise NotImplementedError(f'Mode {self.rt_mode} not implemented yet')
elif self.rt_mode == 3:
print("FILE MODE")
raise NotImplementedError(f'Mode {self.rt_mode} not implemented yet')

if rt_config["clock_speed"] != self.core.mapper.clock_speed:
self.outlet.bang({"clock_speed": rt_config["clock_speed"]})
self.current_state["clock_speed"] = rt_config["clock_speed"]
if rt_config["end"]:
print("Ending UPDATER")
#self.core.mapper.stop()
self.core.mapper.synthesizer.freeall()
sys.exit(0)
break
if rt_config["next_problem"]:
json_to_csv('midi/qubo_control.json', 'h_setup_rt.csv')
#sleep(0.05)
self.outlet.bang({"qubos": "h_setup_rt.csv"})
self.core.problem_event.set()
rt_config["next_problem"] = False
with open("rt_conf.json", "w") as f:
json.dump(rt_config, f)
sleep(1)

def init_core(self, mode=1):
self.rt_mode = mode
Expand Down Expand Up @@ -426,5 +369,11 @@ def clean(self):
#self.updater.join()


def stop_synth(self):
try:
self.core.mapper.synthesizer.freeall()
except Exception as e:
print(e)
print("Synth does not have freeall function, or is not initialized yet")


50 changes: 1 addition & 49 deletions problem/qubo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,8 @@
from util.inlets import VQHInlet
from threading import Lock

class QUBOProblem:
def __init__(self, filename):
self.qubos = []

self.load_data(filename)


def load_data(self, filename="h_setup.csv"):

'''Builds a list of qubos from a csv file.
The csv file must be in the same folder as this script and must be named
"h_setup.csv". The csv file must have the following format:
h1,label1,label2,label3,...,labeln
label1,c11,c12,c13,...,c1n
label2,c21,c22,c23,...,c2n
label3,c31,c32,c33,...,c3n
...
labeln,cn1,cn2,cn3,...,cnn
h2,label1,label2,label3,...,labeln
label1,c11,c12,c13,...,c1n
...
where h1, h2, ... are the QUBO matrices names and label1, label2, ... are the
note labels used by the sonification.

The function returns a list of qubos of the form:
[{(note_1, note_2): coupling, ...}, ...]
where note_1 and note_2 are the note labels with its corresponding coupling
coefficients.
'''

with open(filename, 'r') as hcsv:
hsetup = list(csv.reader(hcsv, delimiter=','))

#header = hsetup.pop(0)
#n_of_ham = int(header[1])
n_of_ham = 1
n_of_notes = 4
self.qubos = []
#n_of_notes = int(header[2])
for h in range(n_of_ham):
notes = hsetup.pop(h*n_of_notes)[1:]
self.qubos.append({(row[0], notes[i]): float(n) for row in hsetup[h*n_of_notes:h*n_of_notes+n_of_notes] for i, n in enumerate(row[1:])})

return self.qubos[-1]



class QUBOProblemRT:
class QUBOProblem:
def __init__(self, filename):

self._qubos = []
Expand Down

0 comments on commit 45b9957

Please sign in to comment.