-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
64 lines (53 loc) · 1.68 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
from qiskit import *
from typing import Union
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from PIL import Image
from io import BytesIO
import os
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
api_key = os.getenv("IBM_API_KEY")
IBMQ.save_account(api_key)
provider = IBMQ.load_account()
class Circuit(BaseModel):
qasm: str
@app.post("/circuit/")
async def create_circuit(circuit: Circuit, backend_name: str | None = "ibmq_qasm_simulator"):
circuit = QuantumCircuit.from_qasm_str(circuit.qasm)
backend = provider.get_backend(backend_name)
transpiled = transpile(circuit, backend=backend)
job = backend.run(transpiled)
return job.job_id()
@app.post("/draw")
async def create_circuit(circuit: Circuit):
circuit = QuantumCircuit.from_qasm_str(circuit.qasm)
drawn_image = circuit.draw(output="mpl")
buffer = BytesIO()
drawn_image.savefig(buffer, format="png")
buffer.seek(0)
return StreamingResponse(buffer, media_type="image/png")
@app.post("/result/{job_id}")
def get_result(job_id: str, backend_name: str | None = "ibmq_qasm_simulator"):
job = provider.get_backend(backend_name).retrieve_job(job_id)
result = job.result()
data = {}
counts = result.results[0].data.counts
max_s = 0
for k in counts.keys():
max_s = max(max_s, len(bin(int(k, 16))[2:]))
for k in counts.keys():
data[(bin(int(k, 16))[2:]).ljust(max_s, "0")] = counts[k]
return data