Skip to content

Commit

Permalink
streamline tket code and update wasm decode3 function
Browse files Browse the repository at this point in the history
  • Loading branch information
irfankhan10 committed Jul 24, 2024
1 parent f03e2d1 commit 0a80d45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,7 @@
"\n",
"* `c`\n",
"* `pfu`\n",
"* `pfu_old`\n",
"* `syn`\n",
"* `syn_old`\n",
"* `syn_new`"
"* `syn`"
]
},
{
Expand All @@ -559,17 +556,11 @@
"creg = BitRegister(\"c\", 3)\n",
"circuit.add_c_register(creg)\n",
"\n",
"pfu_old = BitRegister(\"pfu_old\", 3)\n",
"circuit.add_c_register(pfu_old)\n",
"pfu = BitRegister(\"pfu\", 3)\n",
"circuit.add_c_register(pfu)\n",
"\n",
"syn = BitRegister(\"syn\", 2)\n",
"circuit.add_c_register(syn)\n",
"\n",
"syn_old = BitRegister(\"syn_old\", 2)\n",
"circuit.add_c_register(syn_old)\n",
"\n",
"syn_new = BitRegister(\"syn_new\", 2)\n",
"circuit.add_c_register(syn_new);"
"circuit.add_c_register(syn);"
]
},
{
Expand Down Expand Up @@ -597,17 +588,14 @@
"circuit.CX(qreg[1], areg[0])\n",
"circuit.CX(qreg[2], areg[0])\n",
"circuit.Measure(areg[0], syn[1])\n",
"circuit.Reset(areg[0])\n",
"\n",
"circuit.add_classicalexpbox_register(RegXor(syn_old, syn), syn_new)\n",
"circuit.add_c_copyreg(syn, syn_old);"
"circuit.Reset(areg[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `add_wasm_to_reg` method is used to apply the `decode3` function, defined in the Wasm module, to the bit registers, `syn` and `pfu`. The output of `decode3` is written to the bit register, `pfu_old`. `add_c_xor_to_registers` is used to set the value of `pfu_old` to the output of the expression, `pfu_old` ^ `pfu`. `add_c_setreg` is used to set the all the bits in the classical register, `pfu`, to `False`."
"The `add_wasm_to_reg` method is used to apply the `decode3` function, defined in the Wasm module, to the bit registers, `syn` and `pfu`. The output of `decode3` is written to the bit register, `pfu`. `add_c_xor_to_registers` is used to set the value of `pfu` to the output of the expression, `pfu` ^ `pfu`. `add_c_setreg` is used to set the all the bits in the classical register, `pfu`, to `False`."
]
},
{
Expand All @@ -616,9 +604,9 @@
"metadata": {},
"outputs": [],
"source": [
"circuit.add_wasm_to_reg(\"decode3\", wasm_file_handler, [syn_new, pfu_old], [pfu_old])\n",
"circuit.add_wasm_to_reg(\"decode3\", wasm_file_handler, [syn, pfu], [pfu])\n",
"for i in range(3):\n",
" circuit.X(qreg[i], condition_bits=[pfu_old[i]], condition_value=1);"
" circuit.X(qreg[i], condition_bits=[pfu[i]], condition_value=1);"
]
},
{
Expand All @@ -644,12 +632,9 @@
"circuit.Measure(areg[0], syn[1])\n",
"circuit.Reset(areg[0])\n",
"\n",
"circuit.add_classicalexpbox_register(RegXor(syn_old, syn), syn_new)\n",
"circuit.add_c_copyreg(syn, syn_old)\n",
"\n",
"circuit.add_wasm_to_reg(\"decode3\", wasm_file_handler, [syn_new, pfu_old], [pfu_old])\n",
"circuit.add_wasm_to_reg(\"decode3\", wasm_file_handler, [syn, pfu], [pfu])\n",
"for i in range(3):\n",
" circuit.X(qreg[i], condition_bits=[pfu_old[i]], condition_value=1)"
" circuit.X(qreg[i], condition_bits=[pfu[i]], condition_value=1)"
]
},
{
Expand Down Expand Up @@ -692,18 +677,9 @@
"metadata": {},
"outputs": [],
"source": [
"circuit.add_classicalexpbox_register(syn_old ^ syn, syn_new);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"circuit.add_wasm_to_reg(\"decode3\", wasm_file_handler, [syn_new, pfu_old], [pfu_old])\n",
"circuit.add_wasm_to_reg(\"decode3\", wasm_file_handler, [syn, pfu], [pfu])\n",
"for i in range(3):\n",
" circuit.X(qreg[i], condition_bits=[pfu_old[i]], condition_value=1);"
" circuit.X(qreg[i], condition_bits=[pfu[i]], condition_value=1);"
]
},
{
Expand All @@ -712,7 +688,7 @@
"metadata": {},
"outputs": [],
"source": [
"circuit.add_classicalexpbox_register(pfu_old ^ creg, pfu_old);"
"circuit.add_classicalexpbox_register(pfu ^ creg, pfu);"
]
},
{
Expand Down Expand Up @@ -871,7 +847,7 @@
"metadata": {},
"outputs": [],
"source": [
"logical_error(result, n_shots, creg, pfu_old).head()"
"logical_error(result, n_shots, creg, pfu).head()"
]
},
{
Expand Down
22 changes: 13 additions & 9 deletions examples/qec_decoder_toolkit/repetition_code/c/src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ void init() {
return;
}

int decode3(int syn, int pfu_old) {
int pfu;
if (syn == 1) {
pfu = 1;
} else if (syn == 3) {
pfu = 2;
} else if (syn == 2) {
pfu = 4;
int syn_old;

int decode3(int syn, int pfu) {
int syn_new = syn ^ syn_old;
int val;
if (syn_new == 1) {
val = 1;
} else if (syn_new == 3) {
val = 2;
} else if (syn_new == 2) {
val = 4;
} else {
return 0;
}
return pfu_old ^ pfu;
syn_old = syn;
return val ^ pfu;
}
15 changes: 13 additions & 2 deletions examples/qec_decoder_toolkit/repetition_code/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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.
Expand All @@ -11,19 +13,28 @@ fn init(){
fn decode3(syn: i32, pfu: i32) -> i32 { //takes in a string and returns and a string
let mut decoder:HashMap<i32,i32> = HashMap::new();

// let syn_new: i32 = SYN_OLD ^ syn;
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 {
SYN_OLD = syn;
println!("SYN_OLD: {}, syn: {}", SYN_OLD, syn)
}

if syn == 0 {
return pfu;
}
else {
let pfu_old = pfu ^ decoder[&syn];
return pfu_old;
return pfu ^ decoder[&syn];
}
}

fn main() -> i32 {
let pfu: i32 = decode3(1, 2);
return 0;
}

// #[test]
// fn test_decode3() {
Expand Down

0 comments on commit 0a80d45

Please sign in to comment.