diff --git a/main.py b/main.py new file mode 100644 index 0000000..8c322c5 --- /dev/null +++ b/main.py @@ -0,0 +1,39 @@ +import datetime +from time import sleep, time + +from multiprocessing import Process, Queue +import source.vqe as vqe +import source.mqtt as mqtt + +if __name__ == "__main__": + + + broker_address = "mqtt.deowl.ru" # Example public broker + broker_port = 1883 + mqttBroker = mqtt.vqeMqttBroker(broker_address, broker_port, max_connection_attempts=5) + print(mqttBroker.get_status()) + mqttBroker.connect_to_server() + + while True: + print(mqttBroker.get_status()) + sleep(1) + + # GLOBAL SYSTEM PARAMETERS + active_electrons=2 + active_orbitals=2 + max_iterations = 500 + conv_tol = 1e-04 + step_size = 0.05 + + q = Queue() + p = Process(target=vqe.run_vqe, args=(q, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size)) + p.start() + while p.is_alive(): + try: + print(q.get_nowait(), datetime.datetime.now()) + except Exception as e: + print("no_data_to_get", e) + sleep(0.1) + + client.loop_stop() # Stop the background loop thread if used + client.disconnect() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 0021eb2..a7f735d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -basis-set-exchange==0.11 \ No newline at end of file +basis-set-exchange==0.11 +paho-mqtt==2.1.0 \ No newline at end of file diff --git a/source/mqtt.py b/source/mqtt.py new file mode 100644 index 0000000..47375ae --- /dev/null +++ b/source/mqtt.py @@ -0,0 +1,46 @@ +import paho.mqtt.client as paho +import paho.mqtt.enums as paho_enums + +class vqeMqttBroker: + def __init__(self, broker_address, broker_port, username="", password="", use_tls=True, max_connection_attempts=30): + self.server = broker_address + self.port = broker_port + self.client = paho.Client(callback_api_version=paho.CallbackAPIVersion.VERSION2, client_id="my_python_client", reconnect_on_failure=True) + self.client.username_pw_set(username, password) + if use_tls: + self.client.tls_set() + self.client.on_connect = self.on_connect + self.client.on_message = self.on_message + self.client.on_connect_fail = self.on_connect_fail + self.client.on_disconnect = self.on_disconnect + self.max_connection_attempts=max_connection_attempts + self.connection_attempt_number=0 + + def connect_to_server(self): + self.connection_attempt_number=0 + self.client.connect_async(self.server, self.port, 5) + self.client.loop_start() + + def get_status(self): + + return self.client._state #self.client.is_connected() + + def on_connect(self, client, userdata, flags, reason_code, properties): + print(f"Connected with result code {reason_code}") + # Subscribe to topics here if needed + client.subscribe("my/topic") + + def on_message(self, client, userdata, msg): + print(f"Received message: {msg.payload.decode()} on topic {msg.topic}") + + def on_connect_fail(self, client, userdata): + self.connection_attempt_number+=1 + print("fail") + + def on_disconnect(self, client, userdata, disconnect_flags, reason_code, properties): + self.connection_attempt_number+=1 + print("disconnected") + if (self.connection_attempt_number > self.max_connection_attempts): + self.client.disconnect() + self.client.loop_stop() + self.connection_attempt_number=0 \ No newline at end of file diff --git a/app.py b/source/vqe.py similarity index 68% rename from app.py rename to source/vqe.py index 43ada61..b95927e 100644 --- a/app.py +++ b/source/vqe.py @@ -1,11 +1,12 @@ -import datetime -from time import sleep, time from pennylane import numpy as np import pennylane as qml import os -from multiprocessing import Process, Queue +from multiprocessing import Queue + os.environ["OMP_NUM_THREADS"] = '16' +def get_sctructure_from_xyz_path(path: str): + return qml.qchem.read_structure("methane.xyz") def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size): molecule = qml.qchem.Molecule(symbols, coordinates, load_data=True) @@ -41,24 +42,4 @@ def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, m q.put([n, energy,params]) if conv <= conv_tol: - break - - - - -if __name__ == "__main__": - symbols, coordinates = qml.qchem.read_structure("methane.xyz") - active_electrons=2 - active_orbitals=2 - max_iterations = 500 - conv_tol = 1e-04 - step_size = 0.05 - q = Queue() - p = Process(target=run_vqe, args=(q, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size)) - p.start() - while p.is_alive(): - try: - print(q.get_nowait(), datetime.datetime.now()) - except Exception as e: - print("no_data_to_get", e) - sleep(0.1) \ No newline at end of file + break \ No newline at end of file