-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.py
47 lines (37 loc) · 1.61 KB
/
host.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
from fastapi import FastAPI, Request, Response, status
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
import qsharp
import qsharp.azure
from QuantumSecretSanta import RunSecretSanta
# UNCOMMENT IF YOU WANT TO USE CLOUD SIMULATOR OR HARDWARE!
# qsharp.azure.connect(resourceId="/subscriptions/.../Microsoft.Quantum/Workspaces/WORKSPACE_NAME", location="West Europe")
app = FastAPI()
app.mount("/static/", StaticFiles(directory="web/static/"), name="static")
async def runAsync(num_players: int):
qsharp.reload()
result = RunSecretSanta.simulate(NumPlayers=num_players)
return result
@app.get("/", response_class=FileResponse)
def read_index():
path = 'web/index.html'
return FileResponse(path)
@app.get("/play/{target}/{num_players}")
async def run_sim(target: str, num_players: int, response: Response):
if target == 'local':
result = await runAsync(num_players)
return {"players": result}
elif target == 'simulator' or target == 'hardware':
try:
if target == 'simulator':
qsharp.azure.target("honeywell.hqs-lt-s1-sim")
else:
qsharp.azure.target("honeywell.hqs-lt-s1")
result = qsharp.azure.execute(RunSecretSanta, NumPlayers=num_players, shots=500, jobName="RunSecretSanta - {}".format(target))
return {"players": result}
except Exception as err:
print(err)
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return {"error":err.error_description}
else:
return {"error": "provide valid target"}