From 3858c7a4783102f72ca852b0ba16e9004ee61902 Mon Sep 17 00:00:00 2001 From: James Wootton Date: Tue, 1 Feb 2022 10:42:45 +0100 Subject: [PATCH 1/3] fix linting issues --- qiskit_qec/circuits/__init__.py | 2 +- qiskit_qec/circuits/repetition_code.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit_qec/circuits/__init__.py b/qiskit_qec/circuits/__init__.py index 1abc6ad4..76111436 100644 --- a/qiskit_qec/circuits/__init__.py +++ b/qiskit_qec/circuits/__init__.py @@ -12,4 +12,4 @@ """Circuits.""" -from . import repetition_code \ No newline at end of file +from . import repetition_code diff --git a/qiskit_qec/circuits/repetition_code.py b/qiskit_qec/circuits/repetition_code.py index 8d8228be..f89162b7 100755 --- a/qiskit_qec/circuits/repetition_code.py +++ b/qiskit_qec/circuits/repetition_code.py @@ -422,4 +422,4 @@ def get_processed_results(self, encoded=0): ) % 2 ) - return processed_result \ No newline at end of file + return processed_result From 50fadfd40729f997584f362d0d61f5de0e6319ad Mon Sep 17 00:00:00 2001 From: James Wootton Date: Tue, 1 Feb 2022 10:43:15 +0100 Subject: [PATCH 2/3] fix linting issues --- qiskit_qec/decoders/__init__.py | 2 +- qiskit_qec/decoders/graph_decoder.py | 31 +++++++++++++++++++--------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/qiskit_qec/decoders/__init__.py b/qiskit_qec/decoders/__init__.py index 1ff1092a..81c71254 100644 --- a/qiskit_qec/decoders/__init__.py +++ b/qiskit_qec/decoders/__init__.py @@ -12,4 +12,4 @@ """Circuits.""" -from . import graph_decoder \ No newline at end of file +from . import graph_decoder diff --git a/qiskit_qec/decoders/graph_decoder.py b/qiskit_qec/decoders/graph_decoder.py index 6582850e..16bcf5cd 100755 --- a/qiskit_qec/decoders/graph_decoder.py +++ b/qiskit_qec/decoders/graph_decoder.py @@ -78,6 +78,15 @@ def _separate_string(self, string): return separated_string def string2nodes(self, string, logical="0"): + """ + Generate probabilities of single error events from result counts. + Args: + string (string): Processed results string to convert. + logical (string): Logical value whose results are used. + Returns: + dict: List of nodes corresponding to to the non-trivial + elements in the string. + """ separated_string = self._separate_string(string) nodes = [] @@ -157,7 +166,9 @@ def _make_syndrome_graph(self, results=None): for j in range(depth): gate = qc.data[j][0].name qubits = qc.data[j][1] - errors = ["x", "y", "z"]*(gate not in ['reset', 'measure']) + ["x_id", "x_x"]*(gate=="measure") + errors = ["x", "y", "z"]\ + * (gate not in ['reset', 'measure'])\ + + ["x_id", "x_x"]*(gate=="measure") for error in errors: for qubit in qubits: raw_results = {} @@ -200,16 +211,17 @@ def get_error_probs(self, results, logical="0", use_old=False): results (dict): A results dictionary, as produced by the `process_results` method of the code. logical (string): Logical value whose results are used. + use_old (bool): Whether to use the old calculation method. Returns: dict: Keys are the edges for specific error events, and values are the calculated probabilities Additional information: Uses `results` to estimate the probability of the errors that create the pairs of nodes specified by the edge. - Calculation done using the method of Spitz, et al. + Default calculation method is that of Spitz, et al. https://doi.org/10.1002/qute.201800012 """ - + if not use_old: results = results[logical] @@ -284,9 +296,9 @@ def get_error_probs(self, results, logical="0", use_old=False): for node0 in boundary: error_probs[node0, node0] = 0.5 + (av_v[node0] - 0.5) / prod[node0] - + else: - + results = results[logical] shots = sum(results.values()) @@ -308,7 +320,6 @@ def get_error_probs(self, results, logical="0", use_old=False): error_probs = {} for edge in self.S.edge_list(): - edge_data = self.S.get_edge_data(edge[0], edge[1]) ratios = [] for elements in [('00', '11'), ('11', '00'), ('01', '10'), ('10', '01')]: @@ -522,11 +533,11 @@ def get_logical_prob(self, results, algorithm="matching"): Warning, ) - for string in corrected_results: - shots += corrected_results[string] + for string, samples in corrected_results.items(): + shots += samples if string[0] != str(log): - incorrect_shots += corrected_results[string] + incorrect_shots += samples logical_prob[log] = incorrect_shots / shots - return logical_prob \ No newline at end of file + return logical_prob From d68a0e864a732b507a310da85783f91ea7d1dba3 Mon Sep 17 00:00:00 2001 From: James Wootton Date: Tue, 1 Feb 2022 10:46:47 +0100 Subject: [PATCH 3/3] fix linting issues --- tests/repetiton_codes/test_codes.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/repetiton_codes/test_codes.py b/tests/repetiton_codes/test_codes.py index 0b381934..407e673d 100644 --- a/tests/repetiton_codes/test_codes.py +++ b/tests/repetiton_codes/test_codes.py @@ -17,18 +17,19 @@ """Run codes and decoders.""" import unittest -import retworkx as rx import sys -sys.path.append('../../') -import qiskit_qec -from qiskit_qec.circuits.repetition_code import RepetitionCodeCircuit as RepetitionCode -from qiskit_qec.decoders.graph_decoder import GraphDecoder + +import retworkx as rx from qiskit import execute, Aer, QuantumCircuit from qiskit.providers.aer.noise import NoiseModel -from qiskit.providers.aer.noise.errors import pauli_error, depolarizing_error +from qiskit.providers.aer.noise.errors import depolarizing_error +from qiskit_qec.circuits.repetition_code import RepetitionCodeCircuit as RepetitionCode +from qiskit_qec.decoders.graph_decoder import GraphDecoder + +sys.path.append('../../') def get_syndrome(code, noise_model, shots=1024): """Runs a code to get required results.""" @@ -49,7 +50,6 @@ def get_syndrome(code, noise_model, shots=1024): def get_noise(p_meas, p_gate): """Define a noise model.""" - error_meas = pauli_error([("X", p_meas), ("I", 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) @@ -140,10 +140,11 @@ def test_graph_construction(self): for xbasis in [False, True]: for resets in [False, True]: for delay in [0, 16]: - codes[d,T,xbasis,resets,delay] = RepetitionCode(d, T, xbasis=xbasis, resets=resets, delay=delay) - for params in codes: + codes[d,T,xbasis,resets,delay] = RepetitionCode( + d, T, xbasis=xbasis, resets=resets, + delay=delay) + for params, code in codes.items(): d,T = params[0:2] - code = codes[params] self.single_error_test(code) if len(params)==5: d,T,xbasis,resets,delay = params @@ -169,8 +170,6 @@ def test_weight(self): def test_rep_probs(self): """Repetition code test.""" matching_probs = {} - lookup_probs = {} - post_probs = {} max_dist = 5 @@ -219,7 +218,8 @@ def test_graph(self): for resets in [True, False]: error = ( "Error: The analytical SyndromeGraph does not coincide " - + "with the brute force SyndromeGraph in d=7, T=2, resets="+str(resets)+" RepetitionCode." + + "with the brute force SyndromeGraph in d=7, T=2, resets="\ + + str(resets)+" RepetitionCode." ) code = RepetitionCode(7, 2, resets=resets) graph_new = GraphDecoder(code, brute=False).S