QCHackers / tqec

Design automation software tools for Topological Quantum Error Correction
https://tqec.app
Apache License 2.0
58 stars 15 forks source link

Add a REST server with FastAPI #239

Closed nelimee closed 1 month ago

nelimee commented 1 month ago

This is a draft PR trying to implement a REST server on the backend side.

This is the initial implementation that will allow a link between the frontend and the backend.

Currently, the idea is the following: all the QEC-related state is stored in the backend and the frontend is making requests through the provided REST API to 1. query the current QEC state or 2. change it. Here, "QEC-related state" means that:

Because we want to avoid a communication at each user inputs, the frontend may store partial state.

Note that everything I wrote above and the code from this PR is a proposal, I am very much open to any other idea if it turns out to be easier to use.

To use the code from that PR and experiment with the REST server, follow the instructions:

  1. checkout the branch from that PR (https://github.com/nelimee/tqec/tree/feat/fast-api-rest),
  2. re-install the tqec package to get the new dependencies
    python -m pip install -e ./

    or install the added dependencies directly

    python -m pip install pydantic fastapi
  3. Start the REST server with
    fastapi dev ./web/server.py
  4. If everything succeeded, you can access the server on http://127.0.0.1:8000/ (try out http://127.0.0.1:8000/templates/list or http://127.0.0.1:8000/plaquettes/list). The REST api documentation is available at http://127.0.0.1:8000/docs.

Related issues:

Gistbatch commented 1 month ago

Just as a reminder: We already had a Flask server in place, which was initially set up by @smburdick, and the front end has some unused functionality. The file server root is in /web/main.py, also deployed to gcloud. I don't have any strong preference between flask and fastapi, and most of the concepts are very similar.

As an alternative, there is also GraphQL which can be used to query data in a different way, I don't think it's useful for our purposes though.

nelimee commented 1 month ago

I have no personal preference on that. As I already said multiple times, I am not competent for web-related development task (and I try to stay as far away as possible from anything related to web-technologies).

My choice of pydantic + fastapi was based on the easy of use of pydantic and the ultra-fast integration of fastapi once you have pydantic models in place. There is really no smart thinking that happened on my side, an I will adopt whatever is judged to be the best solution by others.

Let's consider this PR as a playground to start exploring the integration between the frontend and backend, and see what is required from both sides. At any time (if possible, early), if the choice of pydantic + fastapi seems worse than another library, please tell me, or change the framework directly.

Gistbatch commented 1 month ago

I'm very much in favor of FastAPI because I have worked with it before. I think pydantic also works with flask and django.

Gistbatch commented 1 month ago

Could you move this to a repo branch instead of the fork? This would make it easier for others to check out / work on it.

nelimee commented 1 month ago

I will resolve all the conflicts and push to a repository branch in the afternoon.

nelimee commented 1 month ago

Replaced by #240