Skip to content

Commit

Permalink
Merge pull request #20 from oscarhiggott/rename-qubit-id
Browse files Browse the repository at this point in the history
Rename qubit_id
  • Loading branch information
oscarhiggott committed Dec 11, 2021
2 parents b15ff0d + 1db014c commit 38dd9bc
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 307 deletions.
54 changes: 37 additions & 17 deletions docs/usage.ipynb

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions src/pymatching/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
Initialises a WeightedEdgeData object
)")
.def(py::init<std::set<int>, double, double, bool>(),
"qubit_ids"_a, "weight"_a, "error_probability"_a, "has_error_probability"_a, u8R"(
"fault_ids"_a, "weight"_a, "error_probability"_a, "has_error_probability"_a, u8R"(
Initialises a WeightedEdgeData object
Parameters
----------
qubit_ids: set[int]
A set of qubit ids
fault_ids: set[int]
A set of fault IDs
weight: float
The edge weight
error_probability: float
Expand All @@ -43,7 +43,7 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
Whether the edge has an error_probability
)")
.def("__repr__", &WeightedEdgeData::repr)
.def_readwrite("qubit_ids", &WeightedEdgeData::qubit_ids)
.def_readwrite("fault_ids", &WeightedEdgeData::fault_ids)
.def_readwrite("weight", &WeightedEdgeData::weight)
.def_readwrite("error_probability", &WeightedEdgeData::error_probability)
.def_readwrite("has_error_probability", &WeightedEdgeData::has_error_probability);
Expand All @@ -57,13 +57,13 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
>>> from pymatching._cpp_mwpm import MatchingGraph, set_seed
>>> set_seed(0)
>>> graph = MatchingGraph()
>>> graph.add_edge(0, 1, qubit_ids={0}, weight=math.log((1-0.1)/0.1), error_probability=0.1, has_error_probability=True)
>>> graph.add_edge(1, 2, qubit_ids={1}, weight=math.log((1-0.2)/0.2), error_probability=0.2, has_error_probability=True)
>>> graph.add_edge(2, 3, qubit_ids={2}, weight=math.log((1-0.4)/0.4), error_probability=0.4, has_error_probability=True)
>>> graph.add_edge(3, 4, qubit_ids={3}, weight=math.log((1-0.05)/0.05), error_probability=0.05, has_error_probability=True)
>>> graph.add_edge(0, 1, fault_ids={0}, weight=math.log((1-0.1)/0.1), error_probability=0.1, has_error_probability=True)
>>> graph.add_edge(1, 2, fault_ids={1}, weight=math.log((1-0.2)/0.2), error_probability=0.2, has_error_probability=True)
>>> graph.add_edge(2, 3, fault_ids={2}, weight=math.log((1-0.4)/0.4), error_probability=0.4, has_error_probability=True)
>>> graph.add_edge(3, 4, fault_ids={3}, weight=math.log((1-0.05)/0.05), error_probability=0.05, has_error_probability=True)
>>> graph.set_boundary({0, 4})
>>> graph
<pymatching._cpp_mwpm.MatchingGraph object with 4 qubits, 5 nodes, 4 edges and 2 boundary nodes>
<pymatching._cpp_mwpm.MatchingGraph object with 5 nodes, 4 edges and 2 boundary nodes>
>>> graph.get_path(1, 4)
[1, 2, 3, 4]
>>> graph.get_nearest_neighbours(source=2, num_neighbours=2, defect_id=[-1, 0, 1, 2, -1])
Expand Down Expand Up @@ -105,7 +105,7 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
>>> graph.all_edges_have_error_probabilities()
False
)")
.def("add_edge", &MatchingGraph::AddEdge, "node1"_a, "node2"_a, "qubit_ids"_a,
.def("add_edge", &MatchingGraph::AddEdge, "node1"_a, "node2"_a, "fault_ids"_a,
"weight"_a, "error_probability"_a=-1.0, "has_error_probability"_a=false, u8R"(
Adds an edge to the matching graph
Expand All @@ -115,8 +115,8 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
The id of the first node in the edge to be added
node2: int
The id of the second node in the edge to be added
qubit_ids: set[int]
The ids of the qubits associated with the edge
fault_ids: set[int]
The ids of the self-inverse faults that flip if the edge is flipped
weight: float
The weight of the edge
error_probability: float
Expand All @@ -136,7 +136,7 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
>>> graph.add_edge(2, 3, {3}, math.log((1-0.2)/0.2), 0.2, True)
>>> graph.add_edge(3, 0, set(), 0, -1, False)
>>> graph.set_boundary({0, 3})
>>> graph.get_num_qubits()
>>> graph.get_num_fault_ids()
4
>>> graph.get_num_edges()
4
Expand All @@ -149,8 +149,8 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
-------
numpy.ndarray
A binary array (of dtype numpy.uint8) specifying whether each
qubit has been flipped. Element i is one if the qubit with
qubit_id==i has been flipped, and is zero otherwise.
fault has occurred. Element i is one if the fault with
fault ID `i`` has been flipped, and is zero otherwise.
numpy.ndarray
A binary array (of dtype numpy.uint8) with length equal to the
number of nodes in the matching graph, specifying the syndrome.
Expand Down Expand Up @@ -372,8 +372,8 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
>>> graph.shortest_path(1, 2)
[1, 2]
)")
.def("qubit_ids", &MatchingGraph::QubitIDs, "node1"_a, "node2"_a, u8R"(
Returns the qubit_ids associated with the edge (node1, node2)
.def("fault_ids", &MatchingGraph::FaultIDs, "node1"_a, "node2"_a, u8R"(
Returns the fault_ids associated with the edge (node1, node2)
Parameters
----------
Expand All @@ -385,28 +385,28 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
Returns
-------
set[int]
The qubit_ids associated with the edge (node1, node2)
The fault_ids associated with the edge (node1, node2)
Examples
--------
>>> from pymatching._cpp_mwpm import MatchingGraph
>>> graph = MatchingGraph()
>>> graph.add_edge(0, 1, {0}, 1)
>>> graph.add_edge(1, 2, {1, 2, 3}, 1)
>>> graph.qubit_ids(0, 1)
>>> graph.fault_ids(0, 1)
{0}
>>> graph.qubit_ids(1, 2)
>>> graph.fault_ids(1, 2)
{1, 2, 3}
>>> graph.qubit_ids(1, 0)
>>> graph.fault_ids(1, 0)
{0}
)")
.def("get_num_qubits", &MatchingGraph::GetNumQubits, u8R"(
Returns the number of qubits associated with edges in the matching graph.
.def("get_num_fault_ids", &MatchingGraph::GetNumFaultIDs, u8R"(
Returns the number of distinct fault_ids associated with edges in the matching graph.
Returns
-------
int
The number of qubits in the matching graph
The number of distinct fault_ids in the matching graph
Examples
--------
Expand All @@ -415,7 +415,7 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
>>> graph.add_edge(0, 1, {0}, 1)
>>> graph.add_edge(1, 2, {1, 2, 3}, 1)
>>> graph.add_edge(2, 3, {4, 5}, 1)
>>> graph.get_num_qubits()
>>> graph.get_num_fault_ids()
6
)")
.def("get_num_nodes", &MatchingGraph::GetNumNodes, u8R"(
Expand Down Expand Up @@ -592,7 +592,7 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
-------
MatchingResult
The recovery operator (and, optionally, weight of the matching).
`MatchingResult.correction[i]` is 1 if qubit_id 1
`MatchingResult.correction[i]` is 1 if fault_ids 1
should be flipped when applying the minimum weight correction, and 0
otherwise. `MatchingResult.weight` gives the sum of the weights of the
edges included in the minimum-weight perfect matching correction if
Expand Down Expand Up @@ -642,7 +642,7 @@ PYBIND11_MODULE(_cpp_mwpm, m) {
-------
MatchingResult
The recovery operator (and, optionally, weight of the matching).
`MatchingResult.correction[i]` is 1 if qubit_id 1
`MatchingResult.correction[i]` is 1 if fault_ids 1
should be flipped when applying the minimum weight correction, and 0
otherwise. `MatchingResult.weight` gives the sum of the weights of the
edges included in the minimum-weight perfect matching correction if
Expand Down
8 changes: 4 additions & 4 deletions src/pymatching/lemon_mwpm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ MatchingResult ExactMatching(
throw BlossomFailureException();
}

int N = graph.GetNumQubits();
int N = graph.GetNumFaultIDs();
auto correction = new std::vector<int>(N, 0);
std::set<int> qids;
for (py::size_t i = 0; i<num_defects; i++){
Expand All @@ -146,7 +146,7 @@ MatchingResult ExactMatching(
defects_vec[i], defects_vec[j]
);
for (std::vector<int>::size_type k=0; k<path.size()-1; k++){
qids = graph.QubitIDs(path[k], path[k+1]);
qids = graph.FaultIDs(path[k], path[k+1]);
for (auto qid : qids){
if ((qid != -1) && (qid >= 0) && (qid < N)){
(*correction)[qid] = ((*correction)[qid] + 1) % 2;
Expand Down Expand Up @@ -260,7 +260,7 @@ MatchingResult LemonDecodeMatchNeighbourhood(
throw BlossomFailureException();
}

int N = graph.GetNumQubits();
int N = graph.GetNumFaultIDs();
auto correction = new std::vector<int>(N, 0);

std::set<int> remaining_defects;
Expand All @@ -278,7 +278,7 @@ MatchingResult LemonDecodeMatchNeighbourhood(
remaining_defects.erase(j);
path = graph.GetPath(defects_vec[i], defects_vec[j]);
for (std::vector<int>::size_type k=0; k<path.size()-1; k++){
qids = graph.QubitIDs(path[k], path[k+1]);
qids = graph.FaultIDs(path[k], path[k+1]);
for (auto qid : qids){
if ((qid != -1) && (qid >= 0) && (qid < N)){
(*correction)[qid] = ((*correction)[qid] + 1) % 2;
Expand Down
6 changes: 3 additions & 3 deletions src/pymatching/lemon_mwpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct MatchingResult {
MatchingResult(py::array_t<std::uint8_t> correction, double weight);
/**
* @brief The correction operator corresponding to the minimum-weight perfect matching.
* correction[i] is 1 if the ith qubit is flipped and correction[i] is 0 otherwise.
* correction[i] is 1 if the fault with fault ID i is flipped and correction[i] is 0 otherwise.
*
*/
py::array_t<std::uint8_t> correction;
Expand All @@ -57,7 +57,7 @@ struct MatchingResult {
* distances and shortest paths between nodes in the matching graph `graph` are all precomputed and this
* method returns the exact minimum-weight perfect matching. As a result it is suitable for matching graphs
* with a few thousand nodes or less, but will be very memory and compute intensive for larger matching graphs.
* Returns a noise vector N for which N[i]=1 if qubit_id appeared an odd number of times in the minimum weight
* Returns a noise vector N for which N[i]=1 if fault_ids appeared an odd number of times in the minimum weight
* perfect matching and N[i]=0 otherwise.
*
* @param graph A matching graph
Expand All @@ -71,7 +71,7 @@ MatchingResult ExactMatching(MatchingGraph& graph, const py::array_t<int>& defec
* a chosen `num_neighbours`, find the minimum weight perfect matching in the graph V where each defect node
* is connected by an edge to each of the `num_neighbours` nearest other defect nodes in graph, and where the
* weight of each edge is the distance between the two defect nodes in `graph`.
* Returns a noise vector N for which N[i]=1 if qubit_id appeared an odd number of times in the minimum weight
* Returns a noise vector N for which N[i]=1 if fault_ids appeared an odd number of times in the minimum weight
* perfect matching and N[i]=0 otherwise.
*
* @param graph A matching graph
Expand Down

0 comments on commit 38dd9bc

Please sign in to comment.