-
Notifications
You must be signed in to change notification settings - Fork 1
Initial fastapi structure. #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92454ed
Add initial FastAPI structure with HTTP client dependency
marcosfrenkel 506cbd5
First version of "network" chsh
marcosfrenkel 231e060
Fix mypy & ruff
marcosfrenkel 91476fc
Addressed feedback from PR
marcosfrenkel 8595e7e
Fixed mypy issues
marcosfrenkel 3813f70
Added webapp dependencies in typecheck workflow
marcosfrenkel 4ab23c9
fixed typo in http request
marcosfrenkel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Empty file.
This file contains hidden or 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,75 @@ | ||
| import logging | ||
| from collections.abc import AsyncGenerator | ||
| from typing import Annotated | ||
|
|
||
| import httpx | ||
| from fastapi import Depends | ||
| from fastapi import FastAPI | ||
| from fastapi import status | ||
| from pydantic import BaseModel | ||
|
|
||
| logging.basicConfig(level=logging.DEBUG) | ||
| logger = logging.getLogger(__name__) | ||
| app = FastAPI() | ||
|
|
||
|
|
||
| async def get_http_client() -> AsyncGenerator[httpx.AsyncClient, None]: | ||
| async with httpx.AsyncClient() as client: | ||
| yield client | ||
|
|
||
|
|
||
| ClientDep = Annotated[httpx.AsyncClient, Depends(get_http_client)] | ||
|
|
||
|
|
||
| class NodeState(BaseModel): | ||
| waveplates_available: bool = False | ||
| chsh_basis: list[float] = [0.0, 45.0] | ||
|
|
||
|
|
||
| state = NodeState() | ||
|
|
||
|
|
||
| @app.get("/") | ||
| async def root() -> dict[str, str]: | ||
| return {"message": "Hello World"} | ||
|
|
||
|
|
||
| @app.post("/chsh") | ||
| async def chsh(basis: tuple[float, float], other_node_address: str, http_client: ClientDep) -> dict[str, str] | int: | ||
| logger.debug("Starting CHSH") | ||
| expectations = [] | ||
| for angle in basis: | ||
| # measure ab, a'b, ab', a'b' for one expectation value | ||
| counts = [] | ||
| for a in [angle, angle + 90]: | ||
| # local_instruments.hwp.move_to(a) # ruff: noqa: ERA001 | ||
| logger.info("moving waveplate", extra={"angle": a}) | ||
|
|
||
| for i in range(2): | ||
| for perp in [False, True]: | ||
| r = await http_client.get( | ||
| f"http://{other_node_address}/chsh/request-angle-by-basis?i={i}&perp={perp}" | ||
| ) | ||
|
|
||
| # TODO: Handle other status codes | ||
| if r.status_code != status.HTTP_200_OK: | ||
| logger.error("Failed to request follower: %s", r.text) | ||
| return {"error": "Failed to request follower"} | ||
| # count = local_instruments.measure(measurement_config) # ruff: noqa: ERA001 | ||
| count = 1 | ||
| logger.info("measuring counts, %s", count) | ||
| counts.append(count) | ||
|
|
||
| # expectation = compute_expectation(counts) # ruff: noqa: ERA001 | ||
| expectation = sum(counts) | ||
| expectations.append(expectation) | ||
|
|
||
| # chsh_result = compute_chsh(expectations) # ruff: noqa: ERA001 | ||
| return sum(expectations) | ||
|
|
||
|
|
||
| @app.post("/chsh/request-angle-by-basis") | ||
| async def request_angle_by_basis(index: int, *, perp: bool = False) -> bool: | ||
| angle = state.chsh_basis[index] + 90 * perp | ||
| logger.info("moving waveplate", extra={"angle": angle}) | ||
| return True | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.