Skip to content

Commit

Permalink
[infra] Add nix support (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-arkinstall-quantinuum authored Oct 20, 2023
1 parent 8dfae57 commit 7220b48
Show file tree
Hide file tree
Showing 29 changed files with 591 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build-with-nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: build with nix
on:
schedule:
# 01:00 every Sunday morning
- cron: '0 1 * * 0'
workflow_dispatch: {}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build_and_test:
strategy:
matrix:
os: ['ubuntu-22.04', 'macos-12']
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v20
- name: Build and test tket
run: nix flake check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.egg-info
*.o
*.so
*.swp
.cache
.eggs
.hypothesis
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ building and testing tket as a standalone C++ library.
See the [README](pytket/README.md) in the `pytket` directory for instructions on
building and testing pytket.

### Nix Support

Tket and pytket are available as a Nix flake. See the [README](nix-support/README.md)
in the `nix-support` directory for instructions on building and testing tket and pytket
through Nix, and on how to use it within a Nix project.

## API documentation

The `tket` (C++) API documentation (generated with `doxygen`, and still rather
Expand Down
60 changes: 60 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
description = "Tket Quantum SDK";
inputs.nixpkgs.url = "github:nixos/nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils";
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
(import ./nix-support/libs.nix)
(import ./nix-support/symengine.nix)
(import ./nix-support/tket.nix)
(import ./nix-support/third-party-python-packages.nix)
(import ./nix-support/pytket.nix)
];
};
in {
packages = {
tket = pkgs.tket;
pytket = pkgs.pytket;
};
devShells = {
default = pkgs.mkShell { buildInputs = [ pkgs.tket pkgs.pytket ]; };
};
checks = {
tket-tests = pkgs.run-tket-tests;
pytket-tests = pkgs.pytket;
};
});
}
7 changes: 6 additions & 1 deletion libs/tkassert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ find_package(tklog CONFIG REQUIRED)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

option(INSTALL_NAME_DIR "Set the install name dir for the library to @loader_path for Apple targets" ON)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
Expand All @@ -35,7 +37,7 @@ if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS yes)
endif()

if(APPLE)
if(APPLE AND INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
Expand Down Expand Up @@ -64,6 +66,9 @@ target_include_directories(tkassert PUBLIC
$<INSTALL_INTERFACE:include/tkassert>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/tkassert>)
target_link_libraries(tkassert PRIVATE tklog::tklog)
IF(APPLE)
target_link_libraries(tkassert PRIVATE "-flat_namespace")
ENDIF()

target_sources(tkassert
PRIVATE src/AssertMessage.cpp
Expand Down
8 changes: 7 additions & 1 deletion libs/tklog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

option(INSTALL_NAME_DIR "Set the install name dir for the library to @loader_path for Apple targets" ON)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
Expand All @@ -33,7 +35,7 @@ if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS yes)
endif()

if(APPLE)
if(APPLE AND INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
Expand Down Expand Up @@ -62,6 +64,10 @@ target_include_directories(tklog PUBLIC
$<INSTALL_INTERFACE:include/tklog>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/tklog>)

IF(APPLE)
target_link_libraries(tklog PRIVATE "-flat_namespace")
ENDIF()

target_sources(tklog
PRIVATE src/TketLog.cpp
PUBLIC FILE_SET HEADERS
Expand Down
7 changes: 6 additions & 1 deletion libs/tkrng/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

option(INSTALL_NAME_DIR "Set the install name dir for the library to @loader_path for Apple targets" ON)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
Expand All @@ -33,7 +35,7 @@ if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS yes)
endif()

if(APPLE)
if(APPLE AND INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
Expand Down Expand Up @@ -62,6 +64,9 @@ target_include_directories(tkrng PUBLIC
$<INSTALL_INTERFACE:include/tkrng>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/tkrng>)

IF(APPLE)
target_link_libraries(tkrng PRIVATE "-flat_namespace")
ENDIF()
target_sources(tkrng
PRIVATE src/RNG.cpp
PUBLIC FILE_SET HEADERS
Expand Down
8 changes: 7 additions & 1 deletion libs/tktokenswap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ find_package(Boost CONFIG REQUIRED)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

option(INSTALL_NAME_DIR "Set the install name dir for the library to @loader_path for Apple targets" ON)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
Expand All @@ -38,7 +40,7 @@ if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS yes)
endif()

if(APPLE)
if(APPLE AND INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
Expand Down Expand Up @@ -71,6 +73,10 @@ target_link_libraries(tktokenswap PRIVATE tkassert::tkassert)
target_link_libraries(tktokenswap PRIVATE tkrng::tkrng)
target_link_libraries(tktokenswap PRIVATE Boost::headers)

IF(APPLE)
target_link_libraries(tktokenswap PRIVATE "-flat_namespace")
ENDIF()

target_sources(tktokenswap
PRIVATE
src/BestFullTsa.cpp
Expand Down
8 changes: 7 additions & 1 deletion libs/tkwsm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ find_package(Boost CONFIG REQUIRED)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)

option(INSTALL_NAME_DIR "Set the install name dir for the library to @loader_path for Apple targets" ON)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
Expand All @@ -38,7 +40,7 @@ if(WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS yes)
endif()

if(APPLE)
if(APPLE AND INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR "@loader_path")
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
Expand Down Expand Up @@ -69,6 +71,9 @@ target_include_directories(tkwsm PUBLIC
target_link_libraries(tkwsm PRIVATE tkassert::tkassert)
target_link_libraries(tkwsm PRIVATE tkrng::tkrng)
target_link_libraries(tkwsm PRIVATE Boost::headers)
IF(APPLE)
target_link_libraries(tkwsm PRIVATE "-flat_namespace")
ENDIF()

target_sources(tkwsm
PRIVATE
Expand Down Expand Up @@ -157,6 +162,7 @@ target_sources(tkwsm
include/tkwsm/WeightPruning/WeightNogoodDetectorManager.hpp
)


include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/tkwsm)

Expand Down
33 changes: 33 additions & 0 deletions nix-support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Nix support for tket and pytket

Tket exposes a Nix flake that you can build, test and use within a Nix environment.
To build and test tket and pytket on your local system, you can run

```
$ nix flake check github:CQCL/tket
```

This will take some time, as it requires the building of a patched symengine,
the compilation of tket and tket tests, and the compilation of pytket's dependencies,
before testing.

You can enter a development environment with tket and pytket available
with use of `nix develop`. For example,

```
$ nix develop github:CQCL/tket
$ python3
Python 3.10.12 (main, Jun 6 2023, 22:43:10) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pytket import Circuit
>>> circuit = Circuit(5)
>>> circuit.X(1)
[X q[1]; ]
>>> circuit.X(2)
[X q[1]; X q[2]; ]
```

You can also use tket and pytket within your own flake project.
An example of such a project is given in the [example-flake-project](example-flake-project)
directory.
18 changes: 18 additions & 0 deletions nix-support/example-flake-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Example flake project

In this example, we create a [flake.nix](flake.nix) file that tells Nix
where to fetch nixpkgs, flake-utils and tket. It also builds an example
application, using [pyproject.toml](pyproject.toml) and the
[src](src) directory. This application [exposes](src/examples/basic_circuits.py)
two scripts: `entanglement` and `teleport`, that set up trivial circuits
and display them in your browser.

To run these scripts, simply run the following commands from within this directory:

```
nix run .#entanglement
```
and
```
nix run .#teleport
```
45 changes: 45 additions & 0 deletions nix-support/example-flake-project/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
inputs.flake-utils.url = "github:numtide/flake-utils";
# fetch tket from github
inputs.tket.url = "github:CQCL/tket";

outputs = { self, nixpkgs, flake-utils, tket }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
(self: super: {
# add tket and pytket to pkgs for later use
tket = tket.packages.${system}.tket;
pytket = tket.packages.${system}.pytket;
})
];
};
examples = pkgs.python3Packages.buildPythonApplication {
pname = "examples";
version = "0.0.1";
format = "pyproject";
# copy pyproject.toml and src to the build directory
unpackPhase = ''
cp ${./pyproject.toml} pyproject.toml
cp -r ${./src} src
chmod 700 src;
'';
# provide pytket as a dependency
propagatedBuildInputs = [ pkgs.pytket ];
};
in {
apps = {
entanglement = {
type = "app";
program = "${examples}/bin/entanglement";
};
teleport = {
type = "app";
program = "${examples}/bin/teleport";
};
};
});
}
9 changes: 9 additions & 0 deletions nix-support/example-flake-project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "examples"
version = "0.0.1"
dependencies = ["pytket"]

[project.scripts]
entanglement = "examples.basic_circuits:entanglement_circuit"
teleport = "examples.basic_circuits:teleport_circuit"

Empty file.
Loading

0 comments on commit 7220b48

Please sign in to comment.