Unverified Commit 11eb11cb authored by dwuggh's avatar dwuggh
Browse files

update

parent b5b178b8
......@@ -143,6 +143,15 @@ dependencies = [
"termcolor",
]
[[package]]
name = "fastrand"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77b705829d1e87f762c2df6da140b26af5839e1033aa84aa5f56bb688e4e1bdb"
dependencies = [
"instant",
]
[[package]]
name = "fixedbitset"
version = "0.2.0"
......@@ -200,6 +209,15 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
dependencies = [
"cfg-if",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
......@@ -208,9 +226,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.93"
version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "log"
......@@ -411,6 +429,8 @@ dependencies = [
"ansi_term",
"clap",
"env_logger",
"fastrand",
"libc",
"log",
"petgraph",
"rand",
......
......@@ -5,6 +5,7 @@ authors = ["dwuggh <dwuggh@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
build = "build.rs"
[dependencies]
log = "0.4"
......@@ -12,5 +13,7 @@ env_logger = "0.8.3"
ansi_term = "0.12"
petgraph = "0.5.1"
rand = "0.8.3"
fastrand = "1.4.1"
clap = "3.0.0-beta.2"
rayon = "1.5"
libc = "0.2"
File added
use std::fs;
use std::path::PathBuf;
fn main() {
let dir = fs::canonicalize(PathBuf::from("./bin/")).unwrap();
println!("cargo:rustc-link-search={}", dir.to_str().unwrap());
println!("cargo:rustc-link-lib=blossom5")
}
mod interface;
pub mod mwpm;
pub use mwpm::minimum_weight_perfect_match;
pub use interface::mwpm_petgraph;
No preview for this file type
use petgraph::{graph::NodeIndex, Graph, Undirected};
use std::collections::HashMap;
#[link(name = "blossom5")]
extern "C" {
fn minimum_weight_perfect_matching_t(
matching: *mut i32,
node_num: i32,
edge_num_max: i32,
edges: *const i32,
);
}
pub fn mwpm_raw(node_num: i32, edge_num_max: i32, edges: &[i32]) -> Vec<i32> {
unsafe {
let mut matching: Vec<i32> = Vec::with_capacity(node_num as usize);
let p = matching.as_mut_ptr();
minimum_weight_perfect_matching_t(p, node_num, edge_num_max, edges.as_ptr());
matching.set_len(node_num as usize);
return matching;
}
}
pub fn mwpm_petgraph(graph: &Graph<i64, usize, Undirected>) -> Option<HashMap<usize, usize>> {
let node_num = graph.node_count();
if node_num == 0 {
Some(HashMap::new())
} else {
let mut edge_num_max = 0;
let edges_len = graph.edge_count() * 3;
let mut edges: Vec<i32> = vec![0; edges_len];
let mut i = 0;
// log::debug!("{} {}", graph.node_count(), graph.edge_count());
for v1 in graph.node_indices() {
for v2 in graph.node_indices() {
if v1 < v2 {
if let Some(edge) = graph.find_edge(v1, v2) {
edges[i] = v1.index() as i32;
edges[i + 1] = v2.index() as i32;
edges[i + 2] = *graph.edge_weight(edge).unwrap() as i32;
edge_num_max = edge_num_max + 1;
i = i + 3;
}
}
}
}
let matching = mwpm_raw(node_num as i32, edge_num_max as i32, &edges);
let mut result: HashMap<usize, usize> = HashMap::new();
for m1 in 0..matching.len() {
let m2 = matching[m1] as usize;
if m1 < m2 {
let w1 = *graph.node_weight(NodeIndex::new(m1)).unwrap();
let w2 = *graph.node_weight(NodeIndex::new(m2)).unwrap();
if w1 == -w2 - 1 {
let w = w1.abs() as usize;
result.insert(w, w);
} else if w1 >= 0 && w2 >= 0 {
result.insert(w1 as usize, w2 as usize);
}
}
}
Some(result)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mwpm_works() {
let node_num = 6;
let edge_num = 9;
let edges = vec![
0, 1, 3, 0, 3, 10, 0, 4, 7, 1, 2, -1, 1, 4, 5, 1, 5, 4, 2, 5, -7, 3, 4, 0, 4, 5, 4,
];
let result = mwpm_raw(node_num, edge_num, &edges);
assert_eq!(result, vec![1, 0, 5, 4, 3, 2])
}
}
use std::io::Write;
use std::process::{Command, Stdio};
use std::fs;
use std::path::PathBuf;
use petgraph::{graph::NodeIndex, Graph, Undirected};
use std::collections::HashMap;
......@@ -10,7 +13,11 @@ pub fn minimum_weight_perfect_match(
if graph.node_count() == 0 {
return Some(HashMap::new());
}
let mut child = Command::new("/home/dwuggh/Projects/QCQI/blossom5-v2.05.src/blossom5")
let mut pathbuilder = PathBuf::from(file!());
pathbuilder.pop();
pathbuilder.push("blossom5");
let path = fs::canonicalize(pathbuilder).unwrap();
let mut child = Command::new(path.to_str().unwrap())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
......
use rand::Rng;
use rand::{Rng, prelude::ThreadRng};
use crate::model::{BIT_X, BIT_Z};
......@@ -20,12 +20,11 @@ impl NoiseChannel {
NoiseChannel::new(1.0 - p, p / 3.0, p / 3.0, p / 3.0)
}
pub fn apply_mut(&self, qubit: &mut u8) {
*qubit = self.apply(*qubit);
pub fn apply_mut(&self, qubit: &mut u8, rng: &mut ThreadRng) {
*qubit = self.apply_with_rng(*qubit, rng);
}
pub fn apply(&self, qubit: u8) -> u8 {
let mut rng = rand::thread_rng();
pub fn apply_with_rng(&self, qubit: u8, rng: &mut ThreadRng) -> u8 {
let n: f64 = rng.gen_range(0.0..1.0);
if n < self.k {
qubit
......
......@@ -28,6 +28,8 @@ fn naive_runner(xn: usize, zm: usize, p: f64) -> usize {
}
fn main() {
std::env::set_var("LD_LIBRARY_PATH", "bin/");
env_logger::init();
let matches = App::new("surfcode-rs")
.version("0.1.0")
.author("dwuggh <dwuggh@mail.ustc.edu.cn>")
......@@ -35,12 +37,14 @@ fn main() {
.arg(
Arg::new("xn")
.about("N = 2 * xn + 1")
.short('n')
.long("xn")
.default_value("5"),
)
.arg(
Arg::new("zm")
.about("M = 2 * zm + 1")
.short('m')
.long("zm")
.default_value("5"),
)
......
use crate::error::NoiseChannel;
use rand::prelude::{ThreadRng, thread_rng};
/// The Stabilizer Formalism
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
......@@ -33,6 +34,9 @@ pub struct CodeState {
/// the measurement qubits array, `true` for a no defect, `false` for a x(z) defect
pub measures_x: Vec<bool>,
pub measures_z: Vec<bool>,
/// for the sake of efficiency
rng: ThreadRng
}
impl CodeState {
......@@ -41,12 +45,13 @@ impl CodeState {
datas: vec![0; data_num],
measures_x: vec![true; measurex_num],
measures_z: vec![true; measurez_num],
rng: thread_rng()
}
}
pub fn gen_independent_error(&mut self, channel: NoiseChannel) {
for data in self.datas.iter_mut() {
channel.apply_mut(data);
channel.apply_mut(data, &mut self.rng);
}
}
......
use super::codestate::*;
use super::geometry::*;
use crate::algo::minimum_weight_perfect_match;
use crate::algo::{minimum_weight_perfect_match, mwpm_petgraph};
#[derive(Debug)]
pub struct SurfaceCodeModel {
......@@ -97,12 +97,15 @@ impl SurfaceCodeModel {
let graph = geometry.get_defect_graph(&defects);
// print!("{:?}", graph);
let pairs = minimum_weight_perfect_match(&graph).unwrap();
for (&m1, &m2) in pairs.iter() {
let path = geometry.get_path_between_measures(m1, m2);
self.state.flip_path(stabilizer, &path);
if let Some(pairs) = mwpm_petgraph(&graph) {
for (&m1, &m2) in pairs.iter() {
let path = geometry.get_path_between_measures(m1, m2);
self.state.flip_path(stabilizer, &path);
}
} else {
log::error!("{:?}", graph);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment