rabbitmq tests

This commit is contained in:
2026-03-16 22:13:26 +03:00
parent 1d0ee54b1f
commit 0855a064c9
13 changed files with 437 additions and 489 deletions

View File

@@ -1,46 +0,0 @@
import paho.mqtt.client as paho
import paho.mqtt.enums as paho_enums
class vqeMqttBroker:
def __init__(self, broker_address, broker_port, username="correct", password="any", 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

View File

@@ -1,36 +1,46 @@
import os
import sys
import pika
import ssl
context = ssl.create_default_context()
context.verify_mode = ssl.CERT_REQUIRED
ssl_options= pika.SSLOptions(context=context, server_hostname="rabbitmq.deowl.ru")
credential = pika.PlainCredentials("test", "test")
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq.deowl.ru', virtual_host='/', port=5671, ssl_options=ssl_options, credentials=credential))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
import os
import ssl
import sys
import pika
context = ssl.create_default_context()
context.verify_mode = ssl.CERT_REQUIRED
ssl_options = pika.SSLOptions(context=context, server_hostname="rabbitmq.deowl.ru")
credential = pika.PlainCredentials("test", "test")
def main():
connection = pika.BlockingConnection(
pika.ConnectionParameters(
"rabbitmq.deowl.ru",
virtual_host="/",
port=5671,
ssl_options=ssl_options,
credentials=credential,
)
)
channel = connection.channel()
channel.queue_declare(queue="hello")
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
channel.basic_consume(queue="hello", on_message_callback=callback, auto_ack=True)
print(" [*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("Interrupted")
try:
sys.exit(0)
except SystemExit:
os._exit(0)

View File

@@ -1,45 +1,63 @@
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
import os
from multiprocessing import Queue
import pennylane as qml
from pennylane import numpy as np
os.environ["OMP_NUM_THREADS"] = "16"
def get_sctructure_from_xyz_path(path: str):
return qml.qchem.read_structure("methane.xyz")
def run_vqe(
queue_callback: 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)
queue_callback.put([n, energy, params])
if conv <= conv_tol:
break