Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pqnstack/app/api/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import httpx
from fastapi import Depends

from pqnstack.app.core.config import NodeState
from pqnstack.app.core.config import get_state
from pqnstack.app.core.config import settings
from pqnstack.network.client import Client

Expand All @@ -22,3 +24,5 @@ async def get_instrument_client() -> AsyncGenerator[Client, None]:


InstrumentClientDep = Annotated[httpx.AsyncClient, Depends(get_instrument_client)]

StateDep = Annotated[NodeState, Depends(get_state)]
23 changes: 10 additions & 13 deletions src/pqnstack/app/api/routes/chsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from fastapi import status

from pqnstack.app.api.deps import ClientDep
from pqnstack.app.api.deps import StateDep
from pqnstack.app.core.config import settings
from pqnstack.app.core.config import state
from pqnstack.app.core.models import calculate_chsh_expectation_error
from pqnstack.network.client import Client

Expand Down Expand Up @@ -44,6 +44,7 @@ async def _chsh( # Complexity is high due to the nature of the CHSH experiment.

expectation_values = []
expectation_errors = []
basis = (0, abs(basis[0] - basis[1]) % 90)
for angle in basis: # Going through my basis angles
for i in range(2): # Going through follower basis angles
counts = []
Expand Down Expand Up @@ -94,18 +95,14 @@ async def _chsh( # Complexity is high due to the nature of the CHSH experiment.
logger.info("Expectation values: %s", expectation_values)
logger.info("Expectation errors: %s", expectation_errors)

negative_count = sum(1 for v in expectation_values if v < 0)
negative_indices = [i for i, v in enumerate(expectation_values) if v < 0]
impossible_counts = [0, 2, 4]
# FIXME: This is a temporary fix for handling impossible expectation values. We should not have to rely on the settings for this.
expectation_values = [
x * y for x, y in zip(expectation_values, settings.chsh_settings.expectation_signs, strict=False)
]
logger.info("What are you settings? %s", settings.chsh_settings.expectation_signs)

if negative_count in impossible_counts:
msg = f"Impossible negative expectation values found: {negative_indices}, expectation_values = {expectation_values}, expectation_errors = {expectation_errors}"
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=msg)

if len(negative_indices) > 1 or negative_indices[0] != 0:
logger.warning("Expectation values have unexpected negative indices: %s", negative_indices)

chsh_value = sum(abs(x) for x in expectation_values)
logger.info("After passing signed calculation: %s", expectation_values)
chsh_value = sum(x for x in expectation_values)
chsh_error = sum(x**2 for x in expectation_errors) ** 0.5

return chsh_value, chsh_error
Expand All @@ -129,7 +126,7 @@ async def chsh(


@router.post("/request-angle-by-basis")
async def request_angle_by_basis(index: int, *, perp: bool = False) -> bool:
async def request_angle_by_basis(index: int, state: StateDep, *, perp: bool = False) -> bool:
client = Client(host=settings.router_address, port=settings.router_port, timeout=600_000)
hwp = cast(
"RotatorInstrument",
Expand Down
5 changes: 5 additions & 0 deletions src/pqnstack/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CHSHSettings(BaseModel):
hwp: tuple[str, str] = ("", "")
request_hwp: tuple[str, str] = ("", "")
measurement_config: MeasurementConfig = Field(default_factory=lambda: MeasurementConfig(integration_time_s=5))
expectation_signs: tuple[int, int, int, int] = (1, 1, 1, -1)


class QKDSettings(BaseModel):
Expand Down Expand Up @@ -95,3 +96,7 @@ class NodeState(BaseModel):


state = NodeState()


def get_state() -> NodeState:
return state