CQCL / pytket-qir

Public repo for the pytket-qir package
Apache License 2.0
6 stars 1 forks source link

Feature/nix support #120

Open jake-arkinstall opened 7 months ago

jake-arkinstall commented 7 months ago

Description

Added nix support, The nix flake fetches the pytket flake to fetch pytket, and uses a custom derivation to generate pyqir. mypy and pytest are run as part of nix flake check, which is performed in CI (currently set to every sunday at 2am).

At the moment, tket and pytket will be built. The speed of performing the flake check will be improved significantly if we use cachix.

To try out this flake:

nix develop github:CQCL/pytket-qir/feature/nix-support

Then invoke python. pytket.qir is importable and working, e.g.:

$ nix develop github:CQCL/pytket-qir/feature/nix-support

$ python3
Python 3.11.7 (main, Dec  4 2023, 18:10:11) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pytket.circuit import Circuit
>>> from pytket.qir.conversion.api import QIRFormat, pytket_to_qir
>>> c = Circuit(2)
>>> c.H(0)
[H q[0]; ]
>>> c.H(1)
[H q[0]; H q[1]; ]
>>> c.CX(0, 1)
[H q[0]; H q[1]; CX q[0], q[1]; ]
>>> c.H(0)
[H q[0]; H q[1]; CX q[0], q[1]; H q[0]; ]
>>> c.H(1)
[H q[0]; H q[1]; CX q[0], q[1]; H q[0]; H q[1]; ]
>>> print(pytket_to_qir(c, name="example", qir_format=QIRFormat.STRING))
; ModuleID = 'example'
source_filename = "example"

%Qubit = type opaque
%Result = type opaque
; ... and so on

After the merge of this PR, the development shell will be accessible with nix develop github:CQCL/pytket-qir

Checklist

cqc-melf commented 7 months ago

And it would be nice if you could add something to the changelog?

jake-arkinstall commented 7 months ago

For some reason my github app isnt letting my reply in thread, so:

I case I am doing the update, is there anything else I need to do besides updating the version in this file as well?

If the new version requires to change to the setup process, then this is my usual update process:

  1. Reset the hash to an empty string: ""
  2. Update the version number to an available tag of the repository
  3. Run "nix build"

The nix build log will then emit a warning along the lines of: "Empty hash provided, assuming sha256 AAAAAAAA...."

Once it has built the package it will calculate the actual hash. It will emit an error along the lines of:

Incorrect hash.
    Specified: AAAAAAAA.....
    Got: "ABCDEF1234....."

What I do then is add the provided hash back into the nix file and run nix build again.

It should immediately notice that it has already seen a derivation with this source, build instructions, and output hash, and resolve to the package it previously built.

The hash thing is annoying but extremely useful. If Nix can verify the hash then it knows if it has seen it before and lets it skip steps.

Likewise, subsequent runs on that machine will serve that result immediately. If we get a cachix subscription, we can upload the resulting derivation there, and any user that needs it (as specified by the matching source, build instructions and output hash) will be offered a ready-to-go downloadable package instead of going through the build and test process.

Will you do the same with other packages?

Indeed! I was asked to look at pytket-quantinuum and pytket-qir is a dependency of it (in the testing pipeline) so I started here.

This, with the above about the caching possibilities, is quite exciting.

Cachix is when the real benefit of the nix integration will kick in: users will be able to run nix develop github:CQCL/pytket-foo on any Linux or Mac Silicon machine and everything from symengine to tket, pyket to pyqir, and everything they need, will be served over a download with no additional building or configuration required. One command and after a few seconds they'll be ready to go.

It doesn't stop at environments, but can also launch tools. So we could possibly have a one-liner to launch a Jupiter notebook pre-filled with demo code, with the environment all loaded up, ready to showcase. But this is all future ifs and maybes, and I don't want to push it in a direction you don't want to take it in.

cqc-melf commented 7 months ago

Thank you for all the information! I think it would be helpful if you could just copy this into a readme file and add this to the repo as well. Just to make sure this is documented somewhere.

Happy to review your PR on pytket-quantinuum as well.

cqc-melf commented 7 months ago

Maybe it would be good if we could add this to pytket-qiskit as well? As they are both the most used extensions?

jake-arkinstall commented 7 months ago

That is my next challenge after I've got pytket-quantinuum in.

There are qiskit libraries already available on the nix store, however they are marked as broken due to conflicts with symengine. So the qiskit integration PR might be a bit more substantial of a PR than this one, depending on how it goes.

On Tue, 30 Jan 2024, 14:59 cqc-melf, @.***> wrote:

Maybe it would be good if we could add this to pytket-qiskit as well? As they are both the most used extensions?

— Reply to this email directly, view it on GitHub https://github.com/CQCL/pytket-qir/pull/120#issuecomment-1917078615, or unsubscribe https://github.com/notifications/unsubscribe-auth/APSUR26F3C7Q6DHU2OZTNMDYREDDTAVCNFSM6AAAAABCPZQNWKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJXGA3TQNRRGU . You are receiving this because you authored the thread.Message ID: @.***>

cqc-melf commented 6 months ago

@jake-arkinstall is this ready for re-review?

jake-arkinstall commented 6 months ago

@jake-arkinstall is this ready for re-review?

Yes, it is! Although as some movement looks to have been made on the cachix side it might be worth waiting for that to land in tket itself, as it'll speed things up considerably (this repo can then rely on the tket's cached outputs instead of forcing a build).

cqc-alec commented 3 months ago

Please rebase and retarget to main which is now the default branch.

jake-arkinstall commented 3 months ago

I have rebased to main, and I have also updated the flake and workflow to utilise the TKET cachix (binary cache).

For the workflow to succeed, CACHIX_AUTH_TOKEN will need to be available in the workflow environment, as in the tket repository.

I have also run nix flake check locally (x86_64-linux) and everything passes.

jake-arkinstall commented 3 months ago

(Additionally, I set nixpkgs to follow TKET's - this will help with possible cache misses if the two disagree on nixpkgs. As more weight is on TKET's build, doing it this way around seems to make the most sense)

cqc-alec commented 3 months ago

I added CACHIX_AUTH_TOKEN to the repo.

cqc-alec commented 3 months ago

Closing and reopening to trigger CI...