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}
|
|
||||||
414
.gitignore
vendored
414
.gitignore
vendored
@@ -1,207 +1,207 @@
|
|||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
__pycache__/
|
__pycache__/
|
||||||
*.py[codz]
|
*.py[codz]
|
||||||
*$py.class
|
*$py.class
|
||||||
|
|
||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
build/
|
build/
|
||||||
develop-eggs/
|
develop-eggs/
|
||||||
dist/
|
dist/
|
||||||
downloads/
|
downloads/
|
||||||
eggs/
|
eggs/
|
||||||
.eggs/
|
.eggs/
|
||||||
lib/
|
lib/
|
||||||
lib64/
|
lib64/
|
||||||
parts/
|
parts/
|
||||||
sdist/
|
sdist/
|
||||||
var/
|
var/
|
||||||
wheels/
|
wheels/
|
||||||
share/python-wheels/
|
share/python-wheels/
|
||||||
*.egg-info/
|
*.egg-info/
|
||||||
.installed.cfg
|
.installed.cfg
|
||||||
*.egg
|
*.egg
|
||||||
MANIFEST
|
MANIFEST
|
||||||
|
|
||||||
# PyInstaller
|
# PyInstaller
|
||||||
# Usually these files are written by a python script from a template
|
# Usually these files are written by a python script from a template
|
||||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
*.manifest
|
*.manifest
|
||||||
*.spec
|
*.spec
|
||||||
|
|
||||||
# Installer logs
|
# Installer logs
|
||||||
pip-log.txt
|
pip-log.txt
|
||||||
pip-delete-this-directory.txt
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
# Unit test / coverage reports
|
# Unit test / coverage reports
|
||||||
htmlcov/
|
htmlcov/
|
||||||
.tox/
|
.tox/
|
||||||
.nox/
|
.nox/
|
||||||
.coverage
|
.coverage
|
||||||
.coverage.*
|
.coverage.*
|
||||||
.cache
|
.cache
|
||||||
nosetests.xml
|
nosetests.xml
|
||||||
coverage.xml
|
coverage.xml
|
||||||
*.cover
|
*.cover
|
||||||
*.py.cover
|
*.py.cover
|
||||||
.hypothesis/
|
.hypothesis/
|
||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
cover/
|
cover/
|
||||||
|
|
||||||
# Translations
|
# Translations
|
||||||
*.mo
|
*.mo
|
||||||
*.pot
|
*.pot
|
||||||
|
|
||||||
# Django stuff:
|
# Django stuff:
|
||||||
*.log
|
*.log
|
||||||
local_settings.py
|
local_settings.py
|
||||||
db.sqlite3
|
db.sqlite3
|
||||||
db.sqlite3-journal
|
db.sqlite3-journal
|
||||||
|
|
||||||
# Flask stuff:
|
# Flask stuff:
|
||||||
instance/
|
instance/
|
||||||
.webassets-cache
|
.webassets-cache
|
||||||
|
|
||||||
# Scrapy stuff:
|
# Scrapy stuff:
|
||||||
.scrapy
|
.scrapy
|
||||||
|
|
||||||
# Sphinx documentation
|
# Sphinx documentation
|
||||||
docs/_build/
|
docs/_build/
|
||||||
|
|
||||||
# PyBuilder
|
# PyBuilder
|
||||||
.pybuilder/
|
.pybuilder/
|
||||||
target/
|
target/
|
||||||
|
|
||||||
# Jupyter Notebook
|
# Jupyter Notebook
|
||||||
.ipynb_checkpoints
|
.ipynb_checkpoints
|
||||||
|
|
||||||
# IPython
|
# IPython
|
||||||
profile_default/
|
profile_default/
|
||||||
ipython_config.py
|
ipython_config.py
|
||||||
|
|
||||||
# pyenv
|
# pyenv
|
||||||
# For a library or package, you might want to ignore these files since the code is
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
# intended to run in multiple environments; otherwise, check them in:
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
# .python-version
|
# .python-version
|
||||||
|
|
||||||
# pipenv
|
# pipenv
|
||||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
# install all needed dependencies.
|
# install all needed dependencies.
|
||||||
#Pipfile.lock
|
#Pipfile.lock
|
||||||
|
|
||||||
# UV
|
# UV
|
||||||
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
||||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
# commonly ignored for libraries.
|
# commonly ignored for libraries.
|
||||||
#uv.lock
|
#uv.lock
|
||||||
|
|
||||||
# poetry
|
# poetry
|
||||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
# commonly ignored for libraries.
|
# commonly ignored for libraries.
|
||||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
#poetry.lock
|
#poetry.lock
|
||||||
#poetry.toml
|
#poetry.toml
|
||||||
|
|
||||||
# pdm
|
# pdm
|
||||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
||||||
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
||||||
#pdm.lock
|
#pdm.lock
|
||||||
#pdm.toml
|
#pdm.toml
|
||||||
.pdm-python
|
.pdm-python
|
||||||
.pdm-build/
|
.pdm-build/
|
||||||
|
|
||||||
# pixi
|
# pixi
|
||||||
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
||||||
#pixi.lock
|
#pixi.lock
|
||||||
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
||||||
# in the .venv directory. It is recommended not to include this directory in version control.
|
# in the .venv directory. It is recommended not to include this directory in version control.
|
||||||
.pixi
|
.pixi
|
||||||
|
|
||||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
__pypackages__/
|
__pypackages__/
|
||||||
|
|
||||||
# Celery stuff
|
# Celery stuff
|
||||||
celerybeat-schedule
|
celerybeat-schedule
|
||||||
celerybeat.pid
|
celerybeat.pid
|
||||||
|
|
||||||
# SageMath parsed files
|
# SageMath parsed files
|
||||||
*.sage.py
|
*.sage.py
|
||||||
|
|
||||||
# Environments
|
# Environments
|
||||||
.env
|
.env
|
||||||
.envrc
|
.envrc
|
||||||
.venv
|
.venv
|
||||||
env/
|
env/
|
||||||
venv/
|
venv/
|
||||||
ENV/
|
ENV/
|
||||||
env.bak/
|
env.bak/
|
||||||
venv.bak/
|
venv.bak/
|
||||||
|
|
||||||
# Spyder project settings
|
# Spyder project settings
|
||||||
.spyderproject
|
.spyderproject
|
||||||
.spyproject
|
.spyproject
|
||||||
|
|
||||||
# Rope project settings
|
# Rope project settings
|
||||||
.ropeproject
|
.ropeproject
|
||||||
|
|
||||||
# mkdocs documentation
|
# mkdocs documentation
|
||||||
/site
|
/site
|
||||||
|
|
||||||
# mypy
|
# mypy
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
.dmypy.json
|
.dmypy.json
|
||||||
dmypy.json
|
dmypy.json
|
||||||
|
|
||||||
# Pyre type checker
|
# Pyre type checker
|
||||||
.pyre/
|
.pyre/
|
||||||
|
|
||||||
# pytype static type analyzer
|
# pytype static type analyzer
|
||||||
.pytype/
|
.pytype/
|
||||||
|
|
||||||
# Cython debug symbols
|
# Cython debug symbols
|
||||||
cython_debug/
|
cython_debug/
|
||||||
|
|
||||||
# PyCharm
|
# PyCharm
|
||||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
# Abstra
|
# Abstra
|
||||||
# Abstra is an AI-powered process automation framework.
|
# Abstra is an AI-powered process automation framework.
|
||||||
# Ignore directories containing user credentials, local state, and settings.
|
# Ignore directories containing user credentials, local state, and settings.
|
||||||
# Learn more at https://abstra.io/docs
|
# Learn more at https://abstra.io/docs
|
||||||
.abstra/
|
.abstra/
|
||||||
|
|
||||||
# Visual Studio Code
|
# Visual Studio Code
|
||||||
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
||||||
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
||||||
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
||||||
# you could uncomment the following to ignore the entire vscode folder
|
# you could uncomment the following to ignore the entire vscode folder
|
||||||
# .vscode/
|
# .vscode/
|
||||||
|
|
||||||
# Ruff stuff:
|
# Ruff stuff:
|
||||||
.ruff_cache/
|
.ruff_cache/
|
||||||
|
|
||||||
# PyPI configuration file
|
# PyPI configuration file
|
||||||
.pypirc
|
.pypirc
|
||||||
|
|
||||||
# Cursor
|
# Cursor
|
||||||
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
||||||
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
||||||
# refer to https://docs.cursor.com/context/ignore-files
|
# refer to https://docs.cursor.com/context/ignore-files
|
||||||
.cursorignore
|
.cursorignore
|
||||||
.cursorindexingignore
|
.cursorindexingignore
|
||||||
|
|
||||||
# Marimo
|
# Marimo
|
||||||
marimo/_static/
|
marimo/_static/
|
||||||
marimo/_lsp/
|
marimo/_lsp/
|
||||||
__marimo__/
|
__marimo__/
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
# local_quantum_simulator
|
# local_quantum_simulator
|
||||||
This is a repository for a local app that gets deployed for the sake of simulating thq VQE algorythm
|
This is a repository for a local app that gets deployed for the sake of simulating thq VQE algorythm
|
||||||
|
|||||||
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"]
|
|
||||||
142
main.py
142
main.py
@@ -1,39 +1,103 @@
|
|||||||
import datetime
|
import asyncio
|
||||||
from time import sleep, time
|
import datetime
|
||||||
|
import logging
|
||||||
from multiprocessing import Process, Queue
|
import os
|
||||||
import source.vqe as vqe
|
from ast import Lambda
|
||||||
#import project.source.archive.mqtt as mqtt
|
from multiprocessing import Process, Queue
|
||||||
|
from socket import timeout
|
||||||
if __name__ == "__main__":
|
from time import sleep, time
|
||||||
|
|
||||||
|
import aio_pika
|
||||||
broker_address = "mqtt.deowl.ru" # Example public broker
|
import source.vqe as vqe
|
||||||
broker_port = 1883
|
from fasthtml.common import Div, P, fast_app, serve
|
||||||
#mqttBroker = mqtt.vqeMqttBroker(broker_address, broker_port, max_connection_attempts=5)
|
|
||||||
#print(mqttBroker.get_status())
|
logger = logging.basicConfig(level=logging.INFO)
|
||||||
#mqttBroker.connect_to_server()
|
app, rt = fast_app()
|
||||||
|
|
||||||
#while True:
|
|
||||||
# print(mqttBroker.get_status())
|
async def rabbit_worker():
|
||||||
# sleep(1)
|
broker_address = os.environ.get("MQTT_HOST") # Example public broker
|
||||||
|
broker_port = os.environ.get("MQTT_PORT")
|
||||||
# GLOBAL SYSTEM PARAMETERS
|
|
||||||
#active_electrons=2
|
if not broker_address or not broker_port:
|
||||||
'''active_orbitals=2
|
if not broker_address:
|
||||||
max_iterations = 500
|
logging.fatal("Not Found Environment Variable: MQTT_HOST")
|
||||||
conv_tol = 1e-04
|
if not broker_port:
|
||||||
step_size = 0.05
|
logging.fatal("Not Found Environment Variable: MQTT_PORT")
|
||||||
|
logging.info("Shutting down")
|
||||||
q = Queue()
|
return
|
||||||
p = Process(target=vqe.run_vqe, args=(q, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size))
|
logging.info(f"starting connection: {broker_address}, {broker_port}")
|
||||||
p.start()
|
connection = await aio_pika.connect(
|
||||||
while p.is_alive():
|
host=broker_address, port=int(broker_port), timeout=5
|
||||||
try:
|
)
|
||||||
print(q.get_nowait(), datetime.datetime.now())
|
logging.info("debug")
|
||||||
except Exception as e:
|
channel = await connection.channel()
|
||||||
print("no_data_to_get", e)
|
queue = await channel.declare_queue("my_queue", durable=True)
|
||||||
sleep(0.1)
|
|
||||||
|
async with queue.iterator() as queue_iter:
|
||||||
client.loop_stop() # Stop the background loop thread if used
|
async for message in queue_iter:
|
||||||
client.disconnect()'''
|
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()
|
||||||
|
|
||||||
|
"""
|
||||||
|
# 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()"""
|
||||||
|
|||||||
@@ -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
|
aio-pika==9.5.8
|
||||||
pika==1.3.2
|
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,36 +1,46 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import ssl
|
||||||
import pika
|
import sys
|
||||||
import ssl
|
|
||||||
|
import pika
|
||||||
context = ssl.create_default_context()
|
|
||||||
context.verify_mode = ssl.CERT_REQUIRED
|
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")
|
|
||||||
|
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))
|
def main():
|
||||||
channel = connection.channel()
|
connection = pika.BlockingConnection(
|
||||||
|
pika.ConnectionParameters(
|
||||||
channel.queue_declare(queue='hello')
|
"rabbitmq.deowl.ru",
|
||||||
|
virtual_host="/",
|
||||||
def callback(ch, method, properties, body):
|
port=5671,
|
||||||
print(f" [x] Received {body}")
|
ssl_options=ssl_options,
|
||||||
|
credentials=credential,
|
||||||
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
|
)
|
||||||
|
)
|
||||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
channel = connection.channel()
|
||||||
channel.start_consuming()
|
|
||||||
|
channel.queue_declare(queue="hello")
|
||||||
if __name__ == '__main__':
|
|
||||||
try:
|
def callback(ch, method, properties, body):
|
||||||
main()
|
print(f" [x] Received {body}")
|
||||||
except KeyboardInterrupt:
|
|
||||||
print('Interrupted')
|
channel.basic_consume(queue="hello", on_message_callback=callback, auto_ack=True)
|
||||||
try:
|
|
||||||
sys.exit(0)
|
print(" [*] Waiting for messages. To exit press CTRL+C")
|
||||||
except SystemExit:
|
channel.start_consuming()
|
||||||
os._exit(0)
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Interrupted")
|
||||||
|
try:
|
||||||
|
sys.exit(0)
|
||||||
|
except SystemExit:
|
||||||
|
os._exit(0)
|
||||||
|
|||||||
108
source/vqe.py
108
source/vqe.py
@@ -1,45 +1,63 @@
|
|||||||
from pennylane import numpy as np
|
import os
|
||||||
import pennylane as qml
|
from multiprocessing import Queue
|
||||||
import os
|
|
||||||
from multiprocessing import Queue
|
import pennylane as qml
|
||||||
|
from pennylane import numpy as np
|
||||||
os.environ["OMP_NUM_THREADS"] = '16'
|
|
||||||
|
os.environ["OMP_NUM_THREADS"] = "16"
|
||||||
def get_sctructure_from_xyz_path(path: str):
|
|
||||||
return qml.qchem.read_structure("methane.xyz")
|
|
||||||
|
def get_sctructure_from_xyz_path(path: str):
|
||||||
def run_vqe(q: Queue, symbols, coordinates, active_electrons, active_orbitals, max_iterations, conv_tol, step_size):
|
return qml.qchem.read_structure("methane.xyz")
|
||||||
molecule = qml.qchem.Molecule(symbols, coordinates, load_data=True)
|
|
||||||
H, qubits = qml.qchem.molecular_hamiltonian(molecule, active_electrons=active_electrons,
|
|
||||||
active_orbitals=active_orbitals)
|
def run_vqe(
|
||||||
dev = qml.device("lightning.qubit", wires=qubits)
|
queue_callback: Queue,
|
||||||
|
symbols,
|
||||||
|
coordinates,
|
||||||
|
active_electrons,
|
||||||
singles, doubles = qml.qchem.excitations(active_electrons, qubits)
|
active_orbitals,
|
||||||
params = np.array(np.zeros(len(singles) + len(doubles)), requires_grad=True)
|
max_iterations,
|
||||||
@qml.qnode(dev)
|
conv_tol,
|
||||||
def circuit(param, wires):
|
step_size,
|
||||||
# Map excitations to the wires the UCCSD circuit will act on
|
):
|
||||||
s_wires, d_wires = qml.qchem.excitations_to_wires(singles, doubles)
|
molecule = qml.qchem.Molecule(symbols, coordinates, load_data=True)
|
||||||
qml.UCCSD(param, wires, s_wires=s_wires, d_wires=d_wires, init_state=qml.qchem.hf_state(active_electrons, qubits))
|
H, qubits = qml.qchem.molecular_hamiltonian(
|
||||||
return qml.expval(H)
|
molecule, active_electrons=active_electrons, active_orbitals=active_orbitals
|
||||||
|
)
|
||||||
def cost_fn(param):
|
dev = qml.device("lightning.qubit", wires=qubits)
|
||||||
return circuit(param, wires=range(qubits))
|
|
||||||
|
singles, doubles = qml.qchem.excitations(active_electrons, qubits)
|
||||||
opt = qml.GradientDescentOptimizer(stepsize=step_size)
|
params = np.array(np.zeros(len(singles) + len(doubles)), requires_grad=True)
|
||||||
|
|
||||||
for n in range(max_iterations):
|
@qml.qnode(dev)
|
||||||
# Take step
|
def circuit(param, wires):
|
||||||
params, prev_energy = opt.step_and_cost(cost_fn, params)
|
# Map excitations to the wires the UCCSD circuit will act on
|
||||||
|
s_wires, d_wires = qml.qchem.excitations_to_wires(singles, doubles)
|
||||||
energy = cost_fn(params)
|
qml.UCCSD(
|
||||||
|
param,
|
||||||
# Calculate difference between new and old energies
|
wires,
|
||||||
conv = np.abs(energy - prev_energy)
|
s_wires=s_wires,
|
||||||
|
d_wires=d_wires,
|
||||||
q.put([n, energy,params])
|
init_state=qml.qchem.hf_state(active_electrons, qubits),
|
||||||
|
)
|
||||||
if conv <= conv_tol:
|
return qml.expval(H)
|
||||||
break
|
|
||||||
|
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
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
2
|
2
|
||||||
Hydrogen molecule
|
Hydrogen molecule
|
||||||
H 0.000 0.000 0.000
|
H 0.000 0.000 0.000
|
||||||
H 0.740 0.000 0.000
|
H 0.740 0.000 0.000
|
||||||
Reference in New Issue
Block a user