Repetition Code

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

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

File

Download

Repetition 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.

void init() {
    return;
}

int SYN_OLD = 0;

int decode3(int syn) {
    int syn_new = syn ^ SYN_OLD;
    int val;
    int pfu = 0;
    if (syn_new == 1) {
        val = 1;
    } else if (syn_new == 3) {
        val = 2;
    } else if (syn_new == 2) {
        val = 4;
    } else {
        return 0;
    }
    SYN_OLD = syn;
    return val ^ pfu;
}

void reset_syn_old() {
    SYN_OLD = 0;
}

CMake Files

CMakeLists.txt
cmake_minimum_required(VERSION 3.20)

project(RepetitionCode LANGUAGES C)

add_executable(repetition_code src/lib.c)

set_target_properties(repetition_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, test_decode3) {
    int pfu = decode3(2);
    EXPECT_EQ(pfu, 4);
}

TEST(TESTS, test_reset) {
    reset_syn_old();
    EXPECT_EQ(SYN_OLD, 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.

use std::collections::HashMap;

static mut SYN_OLD: i32 = 0;

#[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 decode3(syn: i32) -> i32  { //takes in a string and returns and a string
    let mut decoder:HashMap<i32,i32> = HashMap::new();
    decoder.insert(1, 1); //001 = 1, if syn = 1 then error on qubit 0
    decoder.insert(3, 2); //010 = 2, if sny = 3 then error on qubit 1
    decoder.insert(2, 4); //100 = 4, if syn = 2 then error on qubit 2

    unsafe{
        let syn_new: i32 = SYN_OLD ^ syn;
        SYN_OLD = syn;
        println!("{}", syn_new);

        if syn == 0 {
            return 0;
        }
        else {
            return 0 ^ decoder[&syn_new];
        }
    }
}

#[no_mangle]
fn reset_syn_old(){
  unsafe{
    SYN_OLD = 0;
  }
  
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_decode3() {
        assert_eq!(decode3(2), 4)
    }

    #[test]
    fn test_reset() {
        reset_syn_old();
        unsafe{
            assert_eq!(SYN_OLD, 0);
        }
    }
}

Cargo TOML File

Cargo.toml
[package]
name = "rc"
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]