Files
local_quantum_simulator/Dockerfile_build/source/connections/quantum_backend.py
DeOwl fdeec1bf2e
All checks were successful
Build and Deploy Docker Image / build-and-push (push) Successful in 3m56s
v0.1.1
-- added automativ queue reconenction (querying for queues to join from
server)
2026-05-26 11:44:13 +03:00

160 lines
4.2 KiB
Python

import os
from typing import Any, Dict
import requests
# Global var from env
QUANTUM_BACKEND_URL = os.getenv(
"QUANTUM_BACKEND_URL", os.environ["QUANTUM_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}",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Referer": url,
"Host": "quantum-backend.deowl.ru",
}
try:
response = requests.post(url, json=payload, headers=headers)
print(response.content)
print(response.headers)
print(headers)
response.raise_for_status()
return response.json()
except Exception as e:
print(f"Failed to get or create device: {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}",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Referer": url,
"Host": "quantum-backend.deowl.ru",
}
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}",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Referer": url,
"Host": "quantum-backend.deowl.ru",
}
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