Skip to content

Commit

Permalink
Merge branch 'main' into zh_bias_sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacfomg committed Feb 6, 2024
2 parents ceaec95 + 13bdcb4 commit 8b30327
Show file tree
Hide file tree
Showing 110 changed files with 6,199 additions and 7,669 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/capi.yml
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
36 changes: 36 additions & 0 deletions .github/workflows/rustapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Test Qibolab Rust API
name: Rust API

on:
push:
workflow_dispatch:

jobs:
tests:
strategy:
matrix:
os: [ubuntu-latest, macos-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: |
pip install .
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Check and lint
working-directory: crate
run: |
cargo check
cargo clippy --all-targets
- name: Build & run example
working-directory: crate
run: |
cargo run --example example
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: check-toml
- id: debug-statements
- repo: https://github.com/psf/black
rev: 23.12.0
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ platform = create_platform("my_platform")

# Connects to lab instruments using the details specified in the calibration settings.
platform.connect()
# Configures instruments using the loaded calibration settings.
platform.setup()
# Turns on the local oscillators
platform.start()

# Execute a pulse sequence
options = ExecutionParameters(nshots=1000)
Expand All @@ -70,8 +66,6 @@ results = platform.execute_pulse_sequence(sequence, options)
# Print the acquired shots
print(results.samples)

# Turn off lab instruments
platform.stop()
# Disconnect from the instruments
platform.disconnect()
```
Expand Down
16 changes: 16 additions & 0 deletions capi/.gitignore
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
38 changes: 38 additions & 0 deletions capi/CMakeLists.txt
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)
30 changes: 30 additions & 0 deletions capi/README.md
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.
16 changes: 16 additions & 0 deletions capi/build.md
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
```
2 changes: 2 additions & 0 deletions capi/examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# executable
example
12 changes: 12 additions & 0 deletions capi/examples/Makefile
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
25 changes: 25 additions & 0 deletions capi/examples/example.c
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;
}
3 changes: 3 additions & 0 deletions capi/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# generated
cqibolab.c
qibolab.pc
20 changes: 20 additions & 0 deletions capi/src/build.py
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")
12 changes: 12 additions & 0 deletions capi/src/qibolab.pc.in
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@
5 changes: 5 additions & 0 deletions capi/src/qibolab/qibolab.h
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);
14 changes: 14 additions & 0 deletions capi/src/wrapper.py
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()))
Loading

0 comments on commit 8b30327

Please sign in to comment.