Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyoo-Log authored Dec 12, 2024
1 parent 7d1afbe commit d8b6d7e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
25 changes: 25 additions & 0 deletions # Simple Quantum-Inspired Optimization A.md
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
}
4 changes: 4 additions & 0 deletions Example Response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"optimal_bitstring": "100",
"optimal_value": 2.0
}
5 changes: 5 additions & 0 deletions fastapi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fastapi
uvicorn[standard]
pydantic
qiskit
numpy
44 changes: 44 additions & 0 deletions main.py
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))

0 comments on commit d8b6d7e

Please sign in to comment.