-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kyoo-Log
authored
Dec 12, 2024
1 parent
7d1afbe
commit d8b6d7e
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"optimal_bitstring": "100", | ||
"optimal_value": 2.0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fastapi | ||
uvicorn[standard] | ||
pydantic | ||
qiskit | ||
numpy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |