working mqtt connection

This commit is contained in:
2025-10-16 21:09:16 +03:00
parent 1b7acff7f9
commit 71621896eb
4 changed files with 92 additions and 25 deletions

46
source/mqtt.py Normal file
View File

@@ -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

45
source/vqe.py Normal file
View File

@@ -0,0 +1,45 @@
from pennylane import numpy as np
import pennylane as qml
import os
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)
H, qubits = qml.qchem.molecular_hamiltonian(molecule, active_electrons=active_electrons,
active_orbitals=active_orbitals)
dev = qml.device("lightning.qubit", wires=qubits)
singles, doubles = qml.qchem.excitations(active_electrons, qubits)
params = np.array(np.zeros(len(singles) + len(doubles)), requires_grad=True)
@qml.qnode(dev)
def circuit(param, wires):
# Map excitations to the wires the UCCSD circuit will act on
s_wires, d_wires = qml.qchem.excitations_to_wires(singles, doubles)
qml.UCCSD(param, wires, s_wires=s_wires, d_wires=d_wires, init_state=qml.qchem.hf_state(active_electrons, qubits))
return qml.expval(H)
def cost_fn(param):
return circuit(param, wires=range(qubits))
opt = qml.GradientDescentOptimizer(stepsize=step_size)
for n in range(max_iterations):
# Take step
params, prev_energy = opt.step_and_cost(cost_fn, params)
energy = cost_fn(params)
# Calculate difference between new and old energies
conv = np.abs(energy - prev_energy)
q.put([n, energy,params])
if conv <= conv_tol:
break