From b2b9b6419a87be96578c98fb247efc7e81dfce3d Mon Sep 17 00:00:00 2001 From: Jacfomg Date: Fri, 10 May 2024 11:52:10 +0400 Subject: [PATCH] fix: improve cov --- .../randomized_benchmarking/circuit_tools.py | 18 +----- .../randomized_benchmarking/standard_rb_2q.py | 24 ++++++-- .../randomized_benchmarking/utils.py | 56 ++++++++----------- 3 files changed, 43 insertions(+), 55 deletions(-) diff --git a/src/qibocal/protocols/characterization/randomized_benchmarking/circuit_tools.py b/src/qibocal/protocols/characterization/randomized_benchmarking/circuit_tools.py index c9ee60912..e16c30ba9 100644 --- a/src/qibocal/protocols/characterization/randomized_benchmarking/circuit_tools.py +++ b/src/qibocal/protocols/characterization/randomized_benchmarking/circuit_tools.py @@ -13,6 +13,7 @@ SINGLE_QUBIT_CLIFFORDS_NAMES, find_cliffords, generate_inv_dict_cliffords_file, + separator, ) GLOBAL_PHASES = [ @@ -144,22 +145,7 @@ def add_inverse_2q_layer(circuit: Circuit, two_qubit_cliffords, file_inv): clifford_gate = [] for clifford in clifford_list: - - # Separate values containing 1 - values_with_1 = [value for value in clifford if "1" in value] - values_with_1 = ",".join(values_with_1) - - # Separate values containing 2 - values_with_2 = [value for value in clifford if "2" in value] - values_with_2 = ",".join(values_with_2) - - # Check if CZ - value_with_CZ = [value for value in clifford if "CZ" in value] - value_with_CZ = len(value_with_CZ) == 1 - - values_with_1 = values_with_1.replace("1", "") - values_with_2 = values_with_2.replace("2", "") - + values_with_1, values_with_2, value_with_CZ = separator(clifford) clifford_gate.append(SINGLE_QUBIT_CLIFFORDS_NAMES[values_with_1](0)) clifford_gate.append(SINGLE_QUBIT_CLIFFORDS_NAMES[values_with_2](1)) if value_with_CZ: diff --git a/src/qibocal/protocols/characterization/randomized_benchmarking/standard_rb_2q.py b/src/qibocal/protocols/characterization/randomized_benchmarking/standard_rb_2q.py index 47613c789..028d1e886 100644 --- a/src/qibocal/protocols/characterization/randomized_benchmarking/standard_rb_2q.py +++ b/src/qibocal/protocols/characterization/randomized_benchmarking/standard_rb_2q.py @@ -29,10 +29,12 @@ from ..utils import table_dict, table_html from .circuit_tools import add_inverse_2q_layer, add_measurement_layer, layer_2q_circuit from .fitting import exp1B_func, fit_exp1B_func -from .utils import data_uncertainties, number_to_str, random_2q_clifford - -NPULSES_PER_CLIFFORD = 8.6 -# NPULSES_PER_CLIFFORD = calculate_pulses_clifford(two_qubit_cliffords) +from .utils import ( + calculate_pulses_clifford, + data_uncertainties, + number_to_str, + random_2q_clifford, +) class Depthsdict(TypedDict): @@ -61,6 +63,8 @@ class RB2QData(RBData): """Raw data acquired.""" circuits: dict[QubitPairId, list[list[int]]] = field(default_factory=dict) """Clifford gate indexes executed.""" + npulses_per_clifford: float = 8.6 # Assuming U3s and 1 pulse CZ + """Number of pulses for an average clifford.""" def extract_probabilities(self, qubits): """Extract the probabilities given (`qubit`, `qubit`)""" @@ -127,6 +131,13 @@ def layer_gen(self): """ return random_2q_clifford(self.random_index, self.two_qubit_cliffords) + def calculate_average_pulses(self): + """ + Returns: + - Average number of pulses per clifford. + """ + return calculate_pulses_clifford(self.two_qubit_cliffords) + def random_circuits( depth: int, @@ -261,6 +272,7 @@ def _acquisition( ), ) data.circuits = indexes + data.npulses_per_clifford = rb_gen.calculate_average_pulses() return data @@ -301,7 +313,7 @@ def _fit(data: RB2QData) -> StandardRB2QResult: # Compute the fidelities infidelity = (1 - popt[1]) / 2 fidelity[qubit] = 1 - infidelity - pulse_fidelity[qubit] = 1 - infidelity / NPULSES_PER_CLIFFORD + pulse_fidelity[qubit] = 1 - infidelity / data.npulses_per_clifford # conversion from np.array to list/tuple error_bars = error_bars.tolist() @@ -416,7 +428,7 @@ def _plot( number_to_str( fit.pulse_fidelity[qubits], np.array(fit.fit_uncertainties[qubits][1]) - / (2 * NPULSES_PER_CLIFFORD), + / (2 * data.npulses_per_clifford), ), ], ) diff --git a/src/qibocal/protocols/characterization/randomized_benchmarking/utils.py b/src/qibocal/protocols/characterization/randomized_benchmarking/utils.py index 1a3225d11..9a36e49af 100644 --- a/src/qibocal/protocols/characterization/randomized_benchmarking/utils.py +++ b/src/qibocal/protocols/characterization/randomized_benchmarking/utils.py @@ -133,29 +133,32 @@ def find_cliffords(cz_list): return clifford_list -def clifford2gates(clifford): - gate_list = clifford.split(",") +def separator(clifford): + # Separate values containing 1 + values_with_1 = [value for value in clifford if "1" in value] + values_with_1 = ",".join(values_with_1) - clifford_list = find_cliffords(gate_list) + # Separate values containing 2 + values_with_2 = [value for value in clifford if "2" in value] + values_with_2 = ",".join(values_with_2) - clifford_gate = [] - for clifford in clifford_list: + # Check if CZ + value_with_CZ = [value for value in clifford if "CZ" in value] + value_with_CZ = len(value_with_CZ) == 1 - # Separate values containing 1 - values_with_1 = [value for value in clifford if "1" in value] - values_with_1 = ",".join(values_with_1) + values_with_1 = values_with_1.replace("1", "") + values_with_2 = values_with_2.replace("2", "") + return values_with_1, values_with_2, value_with_CZ - # Separate values containing 2 - values_with_2 = [value for value in clifford if "2" in value] - values_with_2 = ",".join(values_with_2) - # Check if CZ - value_with_CZ = [value for value in clifford if "CZ" in value] - value_with_CZ = len(value_with_CZ) == 1 +def clifford2gates(clifford): + gate_list = clifford.split(",") - values_with_1 = values_with_1.replace("1", "") - values_with_2 = values_with_2.replace("2", "") + clifford_list = find_cliffords(gate_list) + clifford_gate = [] + for clifford in clifford_list: + values_with_1, values_with_2, value_with_CZ = separator(clifford) clifford_gate.append(SINGLE_QUBIT_CLIFFORDS_NAMES[values_with_1](0)) clifford_gate.append(SINGLE_QUBIT_CLIFFORDS_NAMES[values_with_2](1)) if value_with_CZ: @@ -303,27 +306,14 @@ def clifford_to_pulses(clifford): pulses = 0 for clifford in clifford_list: - # Separate values containing 1 - values_with_1 = [value for value in clifford if "1" in value] - values_with_1 = ",".join(values_with_1) - - # Separate values containing 2 - values_with_2 = [value for value in clifford if "2" in value] - values_with_2 = ",".join(values_with_2) - - # Check if CZ - value_with_CZ = [value for value in clifford if "CZ" in value] - value_with_CZ = len(value_with_CZ) == 1 - - values_with_1 = values_with_1.replace("1", "") - values_with_2 = values_with_2.replace("2", "") + values_with_1, values_with_2, value_with_CZ = separator(clifford) if SINGLE_QUBIT_CLIFFORDS_NAMES[values_with_1](0).name != "id": - pulses += 2 + pulses += 2 # This assumes a U3 transpiled into 2 pulses if SINGLE_QUBIT_CLIFFORDS_NAMES[values_with_2](1).name != "id": - pulses += 2 + pulses += 2 # This assumes a U3 transpiled into 2 pulses if value_with_CZ: - pulses += 1 + pulses += 1 # This assumes a CZ without parking so 1 pulse return pulses