Skip to content

Adds config files for the API#104

Merged
marcosfrenkel merged 8 commits into
PublicQuantumNetwork:masterfrom
marcosfrenkel:config_file
Aug 18, 2025
Merged

Adds config files for the API#104
marcosfrenkel merged 8 commits into
PublicQuantumNetwork:masterfrom
marcosfrenkel:config_file

Conversation

@marcosfrenkel
Copy link
Copy Markdown
Collaborator

The server looks for a file called config.toml wherever is running, and if it cannot find it there it looks for a file specified in the environment variable API_CONFIG_PATH

Config files should look something like this:

# Router configuration
router_name = "router1"
router_address = "xx.xx.xx.xx"
router_port = 5555

# Timetagger configuration (provider_name, instrument_name)
timetagger = ["provider", "ins"]

# Bell state configuration (default: Phi_plus)
bell_state = 0

# CHSH experiment settings
[chsh_settings]
hwp = ["provider", "ins"]
request_hwp = ["provider", "ins"]

# CHSH measurement configuration
[chsh_settings.measurement_config]
integration_time_s= 5
binwidth = 500
channel1 = 1
channel2 = 2
dark_count = 0

# QKD experiment settings
[qkd_settings]
hwp = ["provider", "ins"]
request_hwp = ["provider", "ins"]
bitstring_length = 4
discriminating_threshold = 10

# QKD measurement configuration
[qkd_settings.measurement_config]
integration_time_s = 5
binwidth = 500
channel1 = 1
channel2 = 2
dark_count = 0

Copy link
Copy Markdown
Contributor

@Benjamin-Nussbaum Benjamin-Nussbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only comment I have is to consider using pydantic_settings, which should handle a lot of the boilerplate here. https://docs.pydantic.dev/latest/concepts/pydantic_settings/

@marcosfrenkel
Copy link
Copy Markdown
Collaborator Author

Not entirely sure why it says I dismissed your review but I have updated it to use BaseSettings now. If it looks good we can merge @Benjamin-Nussbaum

Comment thread src/pqnstack/app/core/config.py Outdated
from pqnstack.constants import QKDEncodingBasis
from pqnstack.pqn.protocols.measurement import MeasurementConfig

load_dotenv()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary since pydantic-settings has dotenv support - see https://docs.pydantic.dev/latest/concepts/pydantic_settings/#dotenv-env-support

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

Comment on lines 65 to +66
def get_settings() -> Settings:
raise NotImplementedError(static_typecheck_msg)
return Settings()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are simply importing the settings object and using it there, the get_settings() only gets called once already. I don't think most people know about that function so I don't want to add more complexity for basically no new functionality

Copy link
Copy Markdown
Contributor

@Benjamin-Nussbaum Benjamin-Nussbaum Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have dependencies with other endpoints in addition to the config, it might add to the complexity of why one is a (Annotated) Depends dpendency and another is directly imported. If we want to directly import, the get_settings() function itself is unnecessary; we can just instantiate the Settings object at the module level directly. But it makes more sense to me to stick to the FastAPI standard using Depends, in which case I also recommend using @lru_cache.

Comment thread pyproject.toml Outdated
Comment on lines +33 to +34
"pydantic>=2.11.7",
"pydantic-settings>=2.6.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pydantic is a dependency of pydantic-settings (it should be an option, but that's not how they set it up). Anyway, the separate pydantic dependency here is not necessary with pydantic-settings.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the case with the first version of pydantic but they split it up for the 2.0 version the info box in this section

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread config_example.toml
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this example should be in ./configs/? Or perhaps a new ./examples/ directory?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, I have also added the messaging config example as well

Comment thread pyproject.toml Outdated
Comment on lines +33 to +34
"pydantic>=2.11.7",
"pydantic-settings>=2.6.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread pyproject.toml Outdated
@@ -31,6 +31,7 @@ webapp = [
"fastapi[standard]>=0.115.14",
"httpx>=0.28.1",
"pydantic>=2.11.7",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"pydantic>=2.11.7",

Comment on lines 65 to +66
def get_settings() -> Settings:
raise NotImplementedError(static_typecheck_msg)
return Settings()
Copy link
Copy Markdown
Contributor

@Benjamin-Nussbaum Benjamin-Nussbaum Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have dependencies with other endpoints in addition to the config, it might add to the complexity of why one is a (Annotated) Depends dpendency and another is directly imported. If we want to directly import, the get_settings() function itself is unnecessary; we can just instantiate the Settings object at the module level directly. But it makes more sense to me to stick to the FastAPI standard using Depends, in which case I also recommend using @lru_cache.

@marcosfrenkel
Copy link
Copy Markdown
Collaborator Author

ok I think that was all the suggestions. If it looks good can we merge @Benjamin-Nussbaum ?

Copy link
Copy Markdown
Contributor

@Benjamin-Nussbaum Benjamin-Nussbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lru_cache should be on get_settings(), not the Settings object itself. https://fastapi.tiangolo.com/advanced/settings/?h=settings#creating-the-settings-only-once-with-lru_cache

Otherwise looks good

Comment on lines 62 to 63

def get_settings() -> Settings:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def get_settings() -> Settings:
@lru_cache
def get_settings() -> Settings:

Comment thread src/pqnstack/app/core/config.py Outdated
router_port: int
chsh_settings: CHSHSettings
qkd_settings: QKDSettings
@lru_cache
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@lru_cache

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was dumb of me 😅

@marcosfrenkel marcosfrenkel merged commit 0967acd into PublicQuantumNetwork:master Aug 18, 2025
6 checks passed
@marcosfrenkel marcosfrenkel deleted the config_file branch August 18, 2025 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants