Surface Code

\([[9, 1, 3]]\) code to encode 1 logical qubit into 9 physical qubits with code-distance 3.

There are two folders for (rust and c) compilation to Wasm.

File

Download

Surface Code Project

Download

C Project

C Source

src/lib.c
// Copyright 2020-2024 Quantinuum
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


int PFU = 0;


void init() {
    // to initialise the Wasm sandbox environment   
}


void set_pfu_value(int syn) {
    switch(syn){
        case 1:
            PFU = 4; // 000000100
        case 2:
            PFU = 128; // 010000000
        case 4:
            PFU = 1; // 000000001
        case 8:
            PFU = 64; // 001000000
        case 3:
            PFU = 32; // 000100000
        case 6:
            PFU = 16; // 000010000
        case 12:
            PFU = 8; // 000001000
        case 5:
            PFU = 48; // 000110000
        case 10:
            PFU = 24; // 000011000
        case 9:
            PFU = 68; // 001000100
        case 7:
            PFU = 33; // 000100001
        case 11:
            PFU = 96; // 001100000
        case 13:
            PFU = 12; // 000001100
        case 14:
            PFU = 136; // 010001000
        case 15:
            PFU = 40; // 000101000
    }
}


void update_pfu(int syn) {
    int pfu_temp = PFU;
    set_pfu_value(syn);
    PFU = pfu_temp ^ PFU;
}


int get_pfu() {
    return PFU;
}


void reset_pfu() {
    PFU = 0; // 000000000
}

CMake Files

CMakeLists.txt
cmake_minimum_required(VERSION 3.20)

project(SurfaceCode LANGUAGES C)

add_executable(surface_code src/lib.c)

set_target_properties(surface_code PROPERTIES SUFFIX ".wasm")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/wasm)
wasm-toolchain.cmake
set(CMAKE_SYSTEM_NAME Generic)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

set(CMAKE_C_FLAGS "--target=wasm32 --no-standard-libraries")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--no-entry -Wl,--export-all")

set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

C GTests

tests/test_lib.cc
// Copyright 2020-2024 Quantinuum
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include <gtest/gtest.h>
#include "../src/lib.c"

TEST(TESTS, Test1) {
    reset_pfu();
    int syn = 15;
    set_pfu_value(syn);
    EXPECT_EQ(get_pfu(), 40);
}

TEST(TESTS, Test2) {
    reset_pfu();
    int syn = 4;
    set_pfu_value(syn);
    EXPECT_EQ(get_pfu(), 1);
    update_pfu(15);
    EXPECT_EQ(get_pfu(), 41);
}

TEST(TESTS, Test3) {
    set_pfu_value(5);
    reset_pfu();
    EXPECT_EQ(get_pfu(), 0);
}
tests/CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(surface_code_test)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
set(CMAKE_HAVE_LIBC_PTHREAD, 0)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
  test_lib
  test_lib.cc
)
target_link_libraries(
  test_lib
  GTest::gtest_main
)

include(GoogleTest)
gtest_discover_tests(test_lib)

Rust Project

Rust Source and Tests

src/lib.rs
// Copyright 2020-2024 Quantinuum
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


static mut PFU:i32 = 0; // global variable to track the value of the Pauli Frame Update (000000000)


#[no_mangle]
fn init(){
    // This function can have nothing it in, or load some initial function. 
    // It is needed when passed via the Quantinuum API to warm up the wasm execution environment. 
    // It can also be used to set up a global state.
}


#[no_mangle]
fn set_pfu_value(syn: i32) {
    unsafe{
        match syn {
            1 => 4 // 000000100
            2 => 128; // 010000000
            4 => 1; // 000000001
            8 => 64; // 001000000
            3 => 32; // 000100000
            6 => 16; // 000010000
            12 => 8; // 000001000
            5 => 48; // 000110000
            10 => 24; // 000011000
            9 => 68; // 001000100
            7 => 33; // 000100001
            11 => 96; // 001100000
            13 => 12; // 000001100
            14 => 136; // 010001000
            15 =>  40; // 000101000
        }
    }
}

D
#[no_mangle]
fn update_pfu(syn: i32) {
    unsafe{
        let pfu_temp: i32 = PFU.clone();
        set_pfu_value(syn);
        PFU = pfu_temp ^ PFU;
    }
}


#[no_mangle]
fn get_pfu() -> i32 {

    unsafe{
        return PFU;
    }

}


#[no_mangle]
fn reset_pfu() {

    unsafe{
        PFU = 0; // 000000000
    }

}


#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test1() {
        reset_pfu();
        let val: i32 = 15;
        set_pfu_value(val);
        assert_eq!(get_pfu(), 40);
    }

    #[test]
    fn test2() {
        reset_pfu();
        let val: i32 = 4;
        set_pfu_value(val);
        assert_eq!(get_pfu(), 1);
    }

    #[test]
    fn test3() {
        set_pfu_value(5);
        reset_pfu();
        assert_eq!(get_pfu(), 0);
    }
}

Cargo TOML File

Cargo.toml
[package]
name = "surface_code"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]