diff --git a/.github/workflows/python-ci.yml b/.github/workflows/python-ci.yml index 3264e47..16d7078 100644 --- a/.github/workflows/python-ci.yml +++ b/.github/workflows/python-ci.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.10, 3.11] + python-version: ["3.10", "3.11"] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} diff --git a/protocols/api_health_checker.py b/protocols/api_health_checker.py index 647c1b1..e84b927 100644 --- a/protocols/api_health_checker.py +++ b/protocols/api_health_checker.py @@ -1,47 +1,38 @@ -# Real Protocol: API Health Checker import requests -import time +from requests.exceptions import RequestException, Timeout, ConnectionError -def task(): - """Check health of various API endpoints""" - endpoints = [ - {'name': 'Local API', 'url': 'http://localhost:8080/health'}, - {'name': 'JSONPlaceholder', 'url': 'https://jsonplaceholder.typicode.com/posts/1'}, - {'name': 'GitHub API', 'url': 'https://api.github.com/rate_limit'} - ] - - results = [] - failures = 0 - - for endpoint in endpoints: + +def execute(protocol_context): + """ + Checks the health of a list of API endpoints. + """ + api_endpoints = protocol_context.get("api_endpoints") + if not api_endpoints: + return { + "status": "error", + "message": "No API endpoints provided in the protocol context.", + } + + results = {} + for endpoint in api_endpoints: try: - start_time = time.time() - response = requests.get(endpoint['url'], timeout=5) - response_time = (time.time() - start_time) * 1000 # ms - - results.append({ - 'name': endpoint['name'], - 'status': response.status_code, - 'response_time_ms': round(response_time, 2), - 'healthy': response.status_code == 200 - }) - - if response.status_code != 200: - failures += 1 - - except Exception as e: - failures += 1 - results.append({ - 'name': endpoint['name'], - 'error': str(e), - 'healthy': False - }) - - return { - 'success': failures < len(endpoints) / 2, # Success if less than half failed - 'action': 'api_health_check', - 'total_endpoints': len(endpoints), - 'healthy_count': len(endpoints) - failures, - 'failure_count': failures, - 'results': results - } \ No newline at end of file + response = requests.get(endpoint, timeout=10) + response.raise_for_status() # Raise an exception for bad codes + results[endpoint] = { + "status": "ok", + "statusCode": response.status_code + } + except Timeout: + results[endpoint] = { + "status": "error", + "message": "Request timed out" + } + except ConnectionError: + results[endpoint] = { + "status": "error", + "message": "Could not connect to the server", + } + except RequestException as e: + results[endpoint] = {"status": "error", "message": str(e)} + + return {"status": "completed", "results": results} diff --git a/protocols/quantum_simulator.py b/protocols/quantum_simulator.py new file mode 100644 index 0000000..2155b16 --- /dev/null +++ b/protocols/quantum_simulator.py @@ -0,0 +1,55 @@ +""" +Quantum Simulator Protocol +Uses dimod for local quantum algorithm testing without D-Wave API access. +""" + +import dimod + + +def execute(protocol_context): + """ + Demonstrates local quantum optimization using dimod's ExactSolver. + This works without any API keys or external connections. + """ + try: + # Create a simple Binary Quadratic Model (BQM) + # This represents a basic optimization problem + linear = {0: -1, 1: 1, 2: -1} # Linear coefficients + quadratic = {(0, 1): 2, (1, 2): -1} # Quadratic coefficients + + # Create the BQM + bqm = dimod.BinaryQuadraticModel( + linear, quadratic, 0.0, dimod.BINARY + ) + + # Solve using local exact solver (no API required) + sampler = dimod.ExactSolver() + sampleset = sampler.sample(bqm) + + # Get the best solution + best_sample = sampleset.first + + return { + "status": "completed", + "quantum_solution": { + "variables": dict(best_sample.sample), + "energy": best_sample.energy, + "num_occurrences": best_sample.num_occurrences, + "solver_used": "dimod.ExactSolver (local)", + "problem_size": len(linear), + "message": "Quantum optimization completed locally" + } + } + + except Exception as e: + return { + "status": "error", + "message": f"Quantum simulation failed: {str(e)}" + } + + +if __name__ == "__main__": + # Test the quantum simulator + result = execute({}) + print("Quantum Simulation Result:") + print(result) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2d06bfb..9f593e1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,6 +15,7 @@ click==8.2.1 cryptography==45.0.4 dataclasses-json==0.6.7 dimod==0.12.20 +# D-Wave tools that work without API access diskcache==5.6.3 distro==1.9.0 dwave-cloud-client==0.13.6