v0.1.0
Changes: - added rabbitmq hearbeat, consumer and publisher - fixed vqe to run with rabbitmq - added frontend via fasthtml - added proper .env configuration
This commit is contained in:
139
Dockerfile_build/source/connections/quantum_backend.py
Normal file
139
Dockerfile_build/source/connections/quantum_backend.py
Normal file
@@ -0,0 +1,139 @@
|
||||
import os
|
||||
from typing import Any, Dict
|
||||
|
||||
import requests
|
||||
|
||||
# Global var from env
|
||||
QUANTUM_BACKEND_URL = os.getenv(
|
||||
"QUANTUM_BACKEND_URL", os.environ["QUNATUM_BACKEND_URL"]
|
||||
)
|
||||
|
||||
|
||||
def get_or_create_device_by_name(
|
||||
system_name: str, max_qubits: int, access_token
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Get or create a device by name.
|
||||
|
||||
Args:
|
||||
system_name: Name of the system
|
||||
max_qubits: Maximum number of qubits
|
||||
|
||||
Returns:
|
||||
Device data as dictionary
|
||||
|
||||
Raises:
|
||||
requests.RequestException: If the request fails
|
||||
"""
|
||||
url = f"{QUANTUM_BACKEND_URL}/machine/"
|
||||
payload = {"system_name": system_name, "max_qubits": max_qubits}
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.RequestException as e:
|
||||
raise Exception(f"Failed to get or create device: {e}") from e
|
||||
|
||||
|
||||
def get_device_by_id(system_id: int, access_token) -> Dict[str, Any]:
|
||||
"""
|
||||
Get device by ID.
|
||||
|
||||
Args:
|
||||
system_id: ID of the system
|
||||
|
||||
Returns:
|
||||
Device data as dictionary
|
||||
|
||||
Raises:
|
||||
requests.RequestException: If the request fails
|
||||
"""
|
||||
url = f"{QUANTUM_BACKEND_URL}/machine/system"
|
||||
params = {"system_id": system_id}
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.get(url, params=params, headers=headers)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.RequestException as e:
|
||||
raise Exception(f"Failed to get device by ID {system_id}: {e}") from e
|
||||
|
||||
|
||||
def update_device_data(
|
||||
system_id: int,
|
||||
system_name: str,
|
||||
system_description: str,
|
||||
max_qubits: int,
|
||||
access_token,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Update device data.
|
||||
|
||||
Args:
|
||||
system_id: ID of the system
|
||||
system_name: New system name
|
||||
system_description: New system description
|
||||
max_qubits: New maximum qubits
|
||||
|
||||
Returns:
|
||||
Updated device data as dictionary
|
||||
|
||||
Raises:
|
||||
requests.RequestException: If the request fails
|
||||
"""
|
||||
url = f"{QUANTUM_BACKEND_URL}/machine"
|
||||
payload = {
|
||||
"system_id": system_id,
|
||||
"system_name": system_name,
|
||||
"system_description": system_description,
|
||||
"max_qubits": max_qubits,
|
||||
}
|
||||
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {access_token}",
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.put(url, json=payload, headers=headers)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except requests.RequestException as e:
|
||||
raise Exception(f"Failed to update device data for ID {system_id}: {e}") from e
|
||||
|
||||
|
||||
def get_comp_system_file(experiment_type_id: int) -> bytes:
|
||||
"""
|
||||
Get comp_system file as a string/blob.
|
||||
|
||||
Args:
|
||||
experiment_type_id: ID of the experiment type
|
||||
|
||||
Returns:
|
||||
Python module content as string
|
||||
|
||||
Raises:
|
||||
requests.RequestException: If the request fails
|
||||
"""
|
||||
url = f"{QUANTUM_BACKEND_URL}/experiment/types/comp-system"
|
||||
params = {"experiment_type_id": experiment_type_id}
|
||||
|
||||
try:
|
||||
response = requests.get(url, params=params)
|
||||
response.raise_for_status()
|
||||
return response.content
|
||||
except requests.RequestException as e:
|
||||
raise Exception(
|
||||
f"Failed to get comp_system file for experiment_type_id {experiment_type_id}: {e}"
|
||||
) from e
|
||||
Reference in New Issue
Block a user