rabbitmq tests
This commit is contained in:
98
.github/workflows/docker-publish.yml
vendored
98
.github/workflows/docker-publish.yml
vendored
@@ -1,98 +0,0 @@
|
||||
name: Docker
|
||||
|
||||
# This workflow uses actions that are not certified by GitHub.
|
||||
# They are provided by a third-party and are governed by
|
||||
# separate terms of service, privacy policy, and support
|
||||
# documentation.
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '16 23 * * *'
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
# Publish semver tags as releases.
|
||||
tags: [ 'v*.*.*' ]
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
|
||||
env:
|
||||
# Use docker.io for Docker Hub if empty
|
||||
REGISTRY: ghcr.io
|
||||
# github.repository as <account>/<repo>
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
# This is used to complete the identity challenge
|
||||
# with sigstore/fulcio when running outside of PRs.
|
||||
id-token: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
# Install the cosign tool except on PR
|
||||
# https://github.com/sigstore/cosign-installer
|
||||
- name: Install cosign
|
||||
if: github.event_name != 'pull_request'
|
||||
uses: sigstore/cosign-installer@59acb6260d9c0ba8f4a2f9d9b48431a222b68e20 #v3.5.0
|
||||
with:
|
||||
cosign-release: 'v2.2.4'
|
||||
|
||||
# Set up BuildKit Docker container builder to be able to build
|
||||
# multi-platform images and export cache
|
||||
# https://github.com/docker/setup-buildx-action
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0
|
||||
|
||||
# Login against a Docker registry except on PR
|
||||
# https://github.com/docker/login-action
|
||||
- name: Log into registry ${{ env.REGISTRY }}
|
||||
if: github.event_name != 'pull_request'
|
||||
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.DOCKER_SECRET }}
|
||||
|
||||
# Extract metadata (tags, labels) for Docker
|
||||
# https://github.com/docker/metadata-action
|
||||
- name: Extract Docker metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
|
||||
# Build and push Docker image with Buildx (don't push on PR)
|
||||
# https://github.com/docker/build-push-action
|
||||
- name: Build and push Docker image
|
||||
id: build-and-push
|
||||
uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0
|
||||
with:
|
||||
context: .
|
||||
push: ${{ github.event_name != 'pull_request' }}
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
|
||||
# Sign the resulting Docker image digest except on PRs.
|
||||
# This will only write to the public Rekor transparency log when the Docker
|
||||
# repository is public to avoid leaking data. If you would like to publish
|
||||
# transparency data even for private images, pass --force to cosign below.
|
||||
# https://github.com/sigstore/cosign
|
||||
- name: Sign the published Docker image
|
||||
if: ${{ github.event_name != 'pull_request' }}
|
||||
env:
|
||||
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
|
||||
TAGS: ${{ steps.meta.outputs.tags }}
|
||||
DIGEST: ${{ steps.build-and-push.outputs.digest }}
|
||||
# This step uses the identity token to provision an ephemeral certificate
|
||||
# against the sigstore community Fulcio instance.
|
||||
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
|
||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
services:
|
||||
local_quantum_app:
|
||||
build:
|
||||
context: .
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- 5001:5001
|
||||
environment:
|
||||
MQTT_HOST: mqtt.deowl.ru
|
||||
MQTT_PORT: 1883
|
||||
@@ -1,7 +0,0 @@
|
||||
FROM pennylaneai/pennylane:latest-lightning-qubit
|
||||
WORKDIR /app
|
||||
COPY ./source ./source
|
||||
COPY ./main.py ./
|
||||
COPY ./requirements.txt ./
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
CMD ["python", "main.py"]
|
||||
100
main.py
100
main.py
@@ -1,32 +1,96 @@
|
||||
import asyncio
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
from ast import Lambda
|
||||
from multiprocessing import Process, Queue
|
||||
from socket import timeout
|
||||
from time import sleep, time
|
||||
|
||||
from multiprocessing import Process, Queue
|
||||
import aio_pika
|
||||
import source.vqe as vqe
|
||||
#import project.source.archive.mqtt as mqtt
|
||||
from fasthtml.common import Div, P, fast_app, serve
|
||||
|
||||
logger = logging.basicConfig(level=logging.INFO)
|
||||
app, rt = fast_app()
|
||||
|
||||
|
||||
async def rabbit_worker():
|
||||
broker_address = os.environ.get("MQTT_HOST") # Example public broker
|
||||
broker_port = os.environ.get("MQTT_PORT")
|
||||
|
||||
if not broker_address or not broker_port:
|
||||
if not broker_address:
|
||||
logging.fatal("Not Found Environment Variable: MQTT_HOST")
|
||||
if not broker_port:
|
||||
logging.fatal("Not Found Environment Variable: MQTT_PORT")
|
||||
logging.info("Shutting down")
|
||||
return
|
||||
logging.info(f"starting connection: {broker_address}, {broker_port}")
|
||||
connection = await aio_pika.connect(
|
||||
host=broker_address, port=int(broker_port), timeout=5
|
||||
)
|
||||
logging.info("debug")
|
||||
channel = await connection.channel()
|
||||
queue = await channel.declare_queue("my_queue", durable=True)
|
||||
|
||||
async with queue.iterator() as queue_iter:
|
||||
async for message in queue_iter:
|
||||
async with message.process():
|
||||
asyncio.create_task(handle_message(message.body))
|
||||
|
||||
|
||||
async def handle_message(body):
|
||||
print("Processing:", body)
|
||||
await asyncio.sleep(2) # simulate work
|
||||
|
||||
|
||||
def handle_done(body):
|
||||
logging.info(f"done, {body}")
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
global rabbit_task
|
||||
rabbit_task = asyncio.create_task(rabbit_worker())
|
||||
rabbit_task.add_done_callback(handle_done)
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
rabbit_task.cancel()
|
||||
|
||||
|
||||
@rt("/")
|
||||
def get():
|
||||
return Div(P("Hello World!"), hx_get="/change")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
serve()
|
||||
|
||||
|
||||
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
|
||||
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 = 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:
|
||||
@@ -35,5 +99,5 @@ if __name__ == "__main__":
|
||||
print("no_data_to_get", e)
|
||||
sleep(0.1)
|
||||
|
||||
client.loop_stop() # Stop the background loop thread if used
|
||||
client.disconnect()'''
|
||||
# client.loop_stop() # Stop the background loop thread if used
|
||||
# client.disconnect()"""
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
2
|
||||
Hydrogen molecule
|
||||
H 0.000 0.000 0.000
|
||||
H 0.740 0.000 0.000
|
||||
@@ -1,2 +1,2 @@
|
||||
basis-set-exchange==0.11
|
||||
pika==1.3.2
|
||||
aio-pika==9.5.8
|
||||
python-fasthtml==0.12.41
|
||||
|
||||
@@ -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
|
||||
@@ -1,35 +1,45 @@
|
||||
import os
|
||||
import sys
|
||||
import pika
|
||||
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")
|
||||
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))
|
||||
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')
|
||||
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)
|
||||
channel.basic_consume(queue="hello", on_message_callback=callback, auto_ack=True)
|
||||
|
||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
||||
print(" [*] Waiting for messages. To exit press CTRL+C")
|
||||
channel.start_consuming()
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
print('Interrupted')
|
||||
print("Interrupted")
|
||||
try:
|
||||
sys.exit(0)
|
||||
except SystemExit:
|
||||
|
||||
@@ -1,28 +1,46 @@
|
||||
from pennylane import numpy as np
|
||||
import pennylane as qml
|
||||
import os
|
||||
from multiprocessing import Queue
|
||||
|
||||
os.environ["OMP_NUM_THREADS"] = '16'
|
||||
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(q: Queue, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size):
|
||||
|
||||
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)
|
||||
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)
|
||||
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
|
||||
# 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))
|
||||
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):
|
||||
@@ -39,7 +57,7 @@ def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, m
|
||||
# Calculate difference between new and old energies
|
||||
conv = np.abs(energy - prev_energy)
|
||||
|
||||
q.put([n, energy,params])
|
||||
queue_callback.put([n, energy, params])
|
||||
|
||||
if conv <= conv_tol:
|
||||
break
|
||||
Reference in New Issue
Block a user