-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
73 changed files
with
945 additions
and
1,403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Test Qibolab C API | ||
name: C API | ||
|
||
on: | ||
push: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
tests: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.10" | ||
- name: Prepare virtual environment | ||
run: | | ||
python -m venv env | ||
- name: Install dependencies | ||
run: | | ||
# build dependencies | ||
pip install cffi | ||
# runtime dependencies | ||
. env/bin/activate | ||
pip install cffi # also a runtime dep | ||
pip install . | ||
- name: Build & install library | ||
working-directory: capi | ||
run: | | ||
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=$(realpath ../env) | ||
cmake --build build | ||
cmake --install build | ||
- name: Build & run example | ||
working-directory: capi/examples | ||
run: | | ||
. ../../env/bin/activate | ||
export PKG_CONFIG_PATH=${VIRTUAL_ENV}/lib/pkgconfig/:${PKG_CONFIG_PATH}: | ||
export LD_LIBRARY_PATH=${VIRTUAL_ENV}/lib/:${LD_LIBRARY_PATH}: | ||
make | ||
./example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# compiled | ||
*.dylib | ||
*.so | ||
*.dll | ||
|
||
CMakeLists.txt.user | ||
CMakeCache.txt | ||
CMakeFiles | ||
CMakeScripts | ||
Testing | ||
Makefile | ||
cmake_install.cmake | ||
install_manifest.txt | ||
compile_commands.json | ||
CTestTestfile.cmake | ||
_deps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
cmake_minimum_required (VERSION 3.0.2...3.28.1) | ||
|
||
SET(CMAKE_SKIP_BUILD_RPATH FALSE) | ||
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) | ||
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") | ||
|
||
project(libqibolab) | ||
|
||
set(VERSION "\"0.0.1\"") | ||
|
||
find_package(Python3 COMPONENTS Interpreter Development) | ||
|
||
# running the cffi builder | ||
if (NOT EXISTS ${PROJECT_SOURCE_DIR/src/cqibolab.cc}) | ||
execute_process(COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/build.py WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src) | ||
endif() | ||
|
||
include_directories(${Python3_INCLUDE_DIRS}) | ||
include_directories(src) | ||
add_library(qibolab SHARED ${PROJECT_SOURCE_DIR}/src/cqibolab.c) | ||
target_link_libraries(qibolab ${Python3_LIBRARIES}) | ||
|
||
# pkg-config | ||
set(prefix ${CMAKE_INSTALL_PREFIX}) | ||
set(exec_prefix "${prefix}") | ||
set(includedir "${prefix}/include") | ||
set(extraincludirs "-I${Python3_INCLUDE_DIRS}") | ||
set(libdir "${prefix}/lib") | ||
set(pythonlibs "${Python3_LIBRARIES}") | ||
|
||
configure_file( | ||
"${PROJECT_SOURCE_DIR}/src/qibolab.pc.in" | ||
"${PROJECT_SOURCE_DIR}/src/qibolab.pc" | ||
) | ||
|
||
install(FILES ${PROJECT_SOURCE_DIR}/src/qibolab.pc DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/pkgconfig) | ||
install(DIRECTORY ${PROJECT_SOURCE_DIR}/src/qibolab DESTINATION ${CMAKE_INSTALL_PREFIX}/include) | ||
install(TARGETS qibolab LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Qibolab C-API | ||
============= | ||
|
||
This repository contains a C library to access Qibolab from programming languages different from Python. | ||
|
||
## Installation | ||
|
||
Make sure you have installed the `qibolab` module in Python, then in order to install proceed with the usual cmake steps: | ||
```bash | ||
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=<your install prefix> | ||
cmake --build build | ||
cmake --install build | ||
``` | ||
|
||
## Usage | ||
|
||
The compiler flags to include this library in your package can be | ||
retrieved with: | ||
```bash | ||
pkg-config qibolab --cflags | ||
pkg-config qibolab --libs | ||
``` | ||
|
||
If you installed to a non-standard location, you need to set up the `PKG_CONFIG_PATH` and `LD_LIBRARY_PATH`, e.g.: | ||
```bash | ||
export PKG_CONFIG_PATH=${VIRTUAL_ENV}/lib/pkgconfig/:${PKG_CONFIG_PATH}: | ||
export LD_LIBRARY_PATH=${VIRTUAL_ENV}/lib/:${LD_LIBRARY_PATH}: | ||
``` | ||
|
||
Sample programs using this library are provided in the `capi/examples/` directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Further compilation instructions | ||
|
||
## Shared libraries on MacOS | ||
|
||
On MacOS the environment variable `LD_LIBRARY_PATH` is replaced by `DYLD_LIBRARY_PATH`. | ||
|
||
## Nix vs Clang | ||
|
||
Same of what is specified in the [README](./README.md), but to use the GCC compiler, | ||
which is not necessarily the default (e.g. on MacOS), add a suitable CMake flag | ||
|
||
```bash | ||
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=<your install prefix> -D CMAKE_CXX_COMPILER=g++ | ||
cmake --build build | ||
cmake --install build | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# executable | ||
example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Makefile example | ||
|
||
CFLAGS=`pkg-config qibolab --cflags` | ||
LIBS=`pkg-config qibolab --libs` | ||
|
||
all: example | ||
|
||
%: %.c | ||
$(CC) $(CFLAGS) -o $@ $< $(LIBS) | ||
|
||
clean: | ||
rm -rf example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// This file is part of Qibolab | ||
#include <stdio.h> | ||
#include "qibolab/qibolab.h" | ||
|
||
int main() { | ||
|
||
int* qasm_res = execute_qasm( | ||
"OPENQASM 2.0;" \ | ||
"include \"qelib1.inc\";" \ | ||
"qreg q[3];" \ | ||
"creg a[2];" \ | ||
"cx q[0],q[2];" \ | ||
"x q[1];" \ | ||
"swap q[0],q[1];" \ | ||
"cx q[1],q[0];" \ | ||
"measure q[0] -> a[0];" \ | ||
"measure q[2] -> a[1];", | ||
"dummy", 10); | ||
|
||
printf("Samples:\n"); | ||
for (int i = 0; i < 2*10; i++) | ||
printf("%d\n", qasm_res[i]); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# generated | ||
cqibolab.c | ||
qibolab.pc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This file is part of Qibolab | ||
import cffi | ||
|
||
ffibuilder = cffi.FFI() | ||
|
||
with open("qibolab/qibolab.h") as f: | ||
ffibuilder.embedding_api(f.read()) | ||
|
||
ffibuilder.set_source( | ||
"cqibolab", | ||
r""" | ||
#include "qibolab/qibolab.h" | ||
""", | ||
source_extension=".c", | ||
) | ||
|
||
with open("wrapper.py") as f: | ||
ffibuilder.embedding_init_code(f.read()) | ||
|
||
ffibuilder.emit_c_code("cqibolab.c") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
prefix=@prefix@ | ||
exec_prefix=@exec_prefix@ | ||
includedir=@includedir@ | ||
extraincludirs=@extraincludirs@ | ||
libdir=@libdir@ | ||
pythonlibs=@pythonlibs@ | ||
|
||
Name: qibolab | ||
Description: The Qibolab C-API library | ||
Version: @VERSION@ | ||
Cflags: -I@includedir@ @extraincludirs@ | ||
Libs: -L@libdir@ -lqibolab @pythonlibs@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Qibolab C API | ||
*/ | ||
|
||
extern int *execute_qasm(const char *circuit, const char *platform, int nshots); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This file is part of | ||
from cqibolab import ffi | ||
|
||
from qibolab import execute_qasm as py_execute_qasm | ||
|
||
|
||
@ffi.def_extern() | ||
def execute_qasm(circuit, platform, nshots): | ||
"""Generate samples for a given circuit qasm, platform and number of | ||
shots.""" | ||
py_circuit = ffi.string(circuit).decode("utf-8") | ||
py_platform = ffi.string(platform).decode("utf-8") | ||
qasm_res = py_execute_qasm(circuit=py_circuit, platform=py_platform, nshots=nshots) | ||
return ffi.cast("int*", ffi.from_buffer(qasm_res.samples().ravel())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.