Skip to content

Commit

Permalink
linted
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace committed Sep 14, 2023
1 parent ca3052f commit 827cc08
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 181 deletions.
3 changes: 0 additions & 3 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,6 @@ disable=raw-checker-failed,
missing-raises-doc,
logging-too-many-args,
spelling, # way too noisy
no-self-use, # disabled as it is too verbose
bad-continuation, bad-whitespace # differences of opinion with black

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
Expand Down
178 changes: 113 additions & 65 deletions src/qiskit_qec/circuits/repetition_code.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/qiskit_qec/circuits/surface_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def check_nodes(self, nodes, ignore_extra_boundary=False, minimal=False):
p = y
else:
p = x
num_errors = min(num_errors, min(p + 1, self.d - p))
num_errors = min(num_errors, p + 1, self.d - p)
flipped_logicals = {1 - int(p < (self.d - 1) / 2)}

# if unneeded logical zs are given, cluster is not neutral
Expand Down
62 changes: 41 additions & 21 deletions src/qiskit_qec/decoders/decoding_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
"""
import itertools
import logging
from typing import List, Tuple
from typing import List, Tuple, Union

import numpy as np
import rustworkx as rx

from qiskit_qec.analysis.faultenumerator import FaultEnumerator
from qiskit_qec.exceptions import QiskitQECError
from qiskit_qec.utils import DecodingGraphNode, DecodingGraphEdge
from qiskit_qec.utils import DecodingGraphEdge, DecodingGraphNode


class DecodingGraph:
Expand Down Expand Up @@ -98,7 +99,8 @@ def _make_syndrome_graph(self):
n1 = graph.nodes().index(target)
qubits = []
if not (source.is_boundary and target.is_boundary):
qubits = list(set(source.qubits).intersection(target.qubits))
qubits = list(
set(source.qubits).intersection(target.qubits))
if not qubits:
continue
if (
Expand Down Expand Up @@ -161,7 +163,8 @@ def get_error_probs(

for string in counts:
# list of i for which v_i=1
error_nodes = set(self.code.string2nodes(string, logical=logical))
error_nodes = set(self.code.string2nodes(
string, logical=logical))

for node0 in error_nodes:
n0 = self.graph.nodes().index(node0)
Expand Down Expand Up @@ -191,9 +194,11 @@ def get_error_probs(
boundary.append(n0)
else:
if (1 - 2 * av_xor[n0, n1]) != 0:
x = (av_vv[n0, n1] - av_v[n0] * av_v[n1]) / (1 - 2 * av_xor[n0, n1])
x = (av_vv[n0, n1] - av_v[n0] * av_v[n1]) / \
(1 - 2 * av_xor[n0, n1])
if x < 0.25:
error_probs[n0, n1] = max(0, 0.5 - np.sqrt(0.25 - x))
error_probs[n0, n1] = max(
0, 0.5 - np.sqrt(0.25 - x))
else:
error_probs[n0, n1] = np.nan
else:
Expand Down Expand Up @@ -222,7 +227,8 @@ def get_error_probs(
for edge in self.graph.edge_list()
}
for string in counts:
error_nodes = set(self.code.string2nodes(string, logical=logical))
error_nodes = set(self.code.string2nodes(
string, logical=logical))
for edge in self.graph.edge_list():
element = ""
for j in range(2):
Expand Down Expand Up @@ -293,12 +299,13 @@ def weight_syndrome_graph(self, counts, method: str = METHOD_SPITZ):
edge_data.weight = w
self.graph.update_edge(edge[0], edge[1], edge_data)

def make_error_graph(self, data, all_logicals=True):
def make_error_graph(self, data: Union[str, List], all_logicals=True):
"""Returns error graph.
Args:
data: Either an ouput string of the code, or a list of
nodes for the code.
all_logicals(bool): Whether to do all logicals
Returns:
The subgraph of graph which corresponds to the non-trivial
Expand All @@ -322,7 +329,8 @@ def make_error_graph(self, data, all_logicals=True):
def weight_fn(edge):
return int(edge.weight)

distance_matrix = rx.graph_floyd_warshall_numpy(self.graph, weight_fn=weight_fn)
distance_matrix = rx.graph_floyd_warshall_numpy(
self.graph, weight_fn=weight_fn)

for source_index in E.node_indexes():
for target_index in E.node_indexes():
Expand All @@ -333,9 +341,11 @@ def weight_fn(edge):
nt = self.graph.nodes().index(target)
distance = distance_matrix[ns][nt]
if np.isfinite(distance):
qubits = list(set(source.qubits).intersection(target.qubits))
qubits = list(
set(source.qubits).intersection(target.qubits))
distance = int(distance)
E.add_edge(source_index, target_index, DecodingGraphEdge(qubits, distance))
E.add_edge(source_index, target_index,
DecodingGraphEdge(qubits, distance))
return E


Expand Down Expand Up @@ -367,7 +377,8 @@ def __init__(
self.round_schedule = round_schedule
self.basis = basis

self.layer_types = self._layer_types(self.blocks, self.round_schedule, self.basis)
self.layer_types = self._layer_types(
self.blocks, self.round_schedule, self.basis)

self._decoding_graph()

Expand Down Expand Up @@ -442,7 +453,8 @@ def _decoding_graph(self):
idx += 1
for index, supp in enumerate(boundary):
# Add optional is_boundary property for pymatching
node = DecodingGraphNode(is_boundary=True, qubits=supp, index=index)
node = DecodingGraphNode(
is_boundary=True, qubits=supp, index=index)
node.properties["highlighted"] = False
graph.add_node(node)
logging.debug("boundary %d t=%d %s", idx, time, supp)
Expand Down Expand Up @@ -478,9 +490,11 @@ def _decoding_graph(self):
edge.properties["highlighted"] = False
edge.properties["measurement_error"] = 0
graph.add_edge(
idxmap[(time, tuple(op_g))], idxmap[(time, tuple(op_h))], edge
idxmap[(time, tuple(op_g))], idxmap[(
time, tuple(op_h))], edge
)
logging.debug("spacelike t=%d (%s, %s)", time, op_g, op_h)
logging.debug("spacelike t=%d (%s, %s)",
time, op_g, op_h)
logging.debug(
" qubits %s",
[com[0]],
Expand All @@ -497,8 +511,10 @@ def _decoding_graph(self):
edge = DecodingGraphEdge(qubits=[], weight=0)
edge.properties["highlighted"] = False
edge.properties["measurement_error"] = 0
graph.add_edge(idxmap[(time, tuple(bound_g))], idxmap[(time, tuple(bound_h))], edge)
logging.debug("spacelike boundary t=%d (%s, %s)", time, bound_g, bound_h)
graph.add_edge(idxmap[(time, tuple(bound_g))],
idxmap[(time, tuple(bound_h))], edge)
logging.debug("spacelike boundary t=%d (%s, %s)",
time, bound_g, bound_h)

# Add (space)time-like edges from t to t-1
# By construction, the qubit sets of pairs of vertices at graph and T
Expand Down Expand Up @@ -543,25 +559,29 @@ def _decoding_graph(self):
idxmap[(time, tuple(op_g))],
edge,
)
logging.debug("timelike t=%d (%s, %s)", time, op_g, op_h)
logging.debug(
"timelike t=%d (%s, %s)", time, op_g, op_h)
else: # Case (b)
edge = DecodingGraphEdge(qubits=[com[0]], weight=1)
edge = DecodingGraphEdge(
qubits=[com[0]], weight=1)
edge.properties["highlighted"] = False
edge.properties["measurement_error"] = 1
graph.add_edge(
idxmap[(time - 1, tuple(op_h))],
idxmap[(time, tuple(op_g))],
edge,
)
logging.debug("spacetime hook t=%d (%s, %s)", time, op_g, op_h)
logging.debug(
"spacetime hook t=%d (%s, %s)", time, op_g, op_h)
logging.debug(" qubits %s", [com[0]])
# Add a single time-like edge between boundary vertices at
# time t-1 and t
edge = DecodingGraphEdge(qubits=[], weight=0)
edge.properties["highlighted"] = False
edge.properties["measurement_error"] = 0
graph.add_edge(
idxmap[(time - 1, tuple(boundary[0]))], idxmap[(time, tuple(boundary[0]))], edge
idxmap[(time - 1, tuple(boundary[0]))
], idxmap[(time, tuple(boundary[0]))], edge
)
logging.debug("boundarylink t=%d", time)

Expand Down
Loading

0 comments on commit 827cc08

Please sign in to comment.