From d8b6d7e3a5f47b579932616e19e1347a058dcfbc Mon Sep 17 00:00:00 2001 From: Kyoo-Log Date: Thu, 12 Dec 2024 21:52:43 +0700 Subject: [PATCH] Add files via upload --- # Simple Quantum-Inspired Optimization A.md | 25 ++++++++++++ Example Response.json | 4 ++ fastapi.md | 5 +++ main.py | 44 +++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 # Simple Quantum-Inspired Optimization A.md create mode 100644 Example Response.json create mode 100644 fastapi.md create mode 100644 main.py diff --git a/# Simple Quantum-Inspired Optimization A.md b/# Simple Quantum-Inspired Optimization A.md new file mode 100644 index 0000000..f52cd14 --- /dev/null +++ b/# Simple Quantum-Inspired Optimization A.md @@ -0,0 +1,25 @@ +# Simple Quantum-Inspired Optimization API + +This is a simple API that demonstrates quantum-inspired optimization using the Quantum Approximate Optimization Algorithm (QAOA) to solve the MaxCut problem. It uses Qiskit for quantum circuit simulation and FastAPI for creating the API. + +## Getting Started + +1. Clone the repository: `git clone https://github.com/kyoo-log/QIOS.git` (replace with your repo URL) +2. Create a virtual environment (recommended): `python -m venv .venv` +3. Activate the virtual environment: + * Windows: `.venv\Scripts\activate` + * macOS/Linux: `source .venv/bin/activate` +4. Install dependencies: `pip install -r requirements.txt` +5. Run the API: `uvicorn main:app --reload` + +## Usage + +Send a POST request to `/max_cut` with a JSON payload containing the adjacency matrix of the graph and the number of QAOA layers (`p`). + +Example Request: + +```json +{ + "graph": [[0, 1, 1], [1, 0, 1], [1, 1, 0]], + "p": 2 +} \ No newline at end of file diff --git a/Example Response.json b/Example Response.json new file mode 100644 index 0000000..0f7bf00 --- /dev/null +++ b/Example Response.json @@ -0,0 +1,4 @@ +{ + "optimal_bitstring": "100", + "optimal_value": 2.0 + } \ No newline at end of file diff --git a/fastapi.md b/fastapi.md new file mode 100644 index 0000000..08bd5c4 --- /dev/null +++ b/fastapi.md @@ -0,0 +1,5 @@ +fastapi +uvicorn[standard] +pydantic +qiskit +numpy diff --git a/main.py b/main.py new file mode 100644 index 0000000..7c294d7 --- /dev/null +++ b/main.py @@ -0,0 +1,44 @@ +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel +import numpy as np +from qiskit import Aer +from qiskit.algorithms import QAOA +from qiskit.optimization import QuadraticProgram +from qiskit.optimization.converters import QuadraticProgramToQubo + +app = FastAPI(title="Simple Quantum-Inspired Optimization API") + +class MaxCutRequest(BaseModel): + graph: list[list[int]] + p: int = 1 + +@app.post("/max_cut") +async def max_cut(request: MaxCutRequest): + try: + graph = np.array(request.graph) + num_nodes = len(graph) + + qp = QuadraticProgram() + for i in range(num_nodes): + qp.binary_var(f'x_{i}') + + for i in range(num_nodes): + for j in range(i + 1, num_nodes): + if graph[i, j] == 1: + qp.objective.linear[f'x_{i}'] += 1 + qp.objective.linear[f'x_{j}'] += 1 + qp.objective.quadratic[f'x_{i}', f'x_{j}'] -= 2 + + qubo = QuadraticProgramToQubo().convert(qp) + + backend = Aer.get_backend('qasm_simulator') + qaoa = QAOA(quantum_instance=backend, p=request.p) + result = qaoa.compute_minimum_eigenvalue(qubo) + + optimal_bitstring = "".join(map(str, map(int, result.optimal_bitstring))) + optimal_value = result.optimal_value + + return {"optimal_bitstring": optimal_bitstring, "optimal_value": optimal_value} + + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) \ No newline at end of file