qibocal.protocols.randomized_benchmarking package#

Submodules#

qibocal.protocols.randomized_benchmarking.circuit_tools module#

Collection of function to generate qibo circuits.

qibocal.protocols.randomized_benchmarking.circuit_tools.layer_circuit(rb_gen: Callable, depth: int, qubit) tuple[qibo.models.circuit.Circuit, dict][source]#

Creates a circuit of depth layers from a generator layer_gen yielding Circuit or Gate and a dictionary with random indexes used to select the clifford gates.

Parameters:
  • layer_gen (Callable) – Should return gates or a full circuit specifying a layer.

  • depth (int) – Number of layers.

Returns:

with depth many layers.

Return type:

Circuit

qibocal.protocols.randomized_benchmarking.circuit_tools.add_inverse_layer(circuit: Circuit, single_qubit=True)[source]#

Adds an inverse gate/inverse gates at the end of a circuit (in place).

Parameters:

circuit (Circuit) – circuit

qibocal.protocols.randomized_benchmarking.circuit_tools.add_measurement_layer(circuit: Circuit)[source]#

Adds a measurement layer at the end of the circuit.

Parameters:

circuit (Circuit) – Measurement gates added in place to end of this circuit.

qibocal.protocols.randomized_benchmarking.filtered_rb module#

class qibocal.protocols.randomized_benchmarking.filtered_rb.FilteredRBParameters(depths: ~typing.Union[list, ~qibocal.protocols.randomized_benchmarking.standard_rb.Depthsdict], niter: int, uncertainties: ~typing.Optional[float] = None, unrolling: bool = False, seed: ~typing.Optional[int] = None, noise_model: ~typing.Optional[str] = None, noise_params: ~typing.Optional[list] = <factory>, nshots: int = 10)[source]#

Bases: StandardRBParameters

Filtered Randomized Benchmarking runcard inputs.

class qibocal.protocols.randomized_benchmarking.filtered_rb.FilteredRBResult[source]#

Bases: Results

Filtered RB outputs.

qibocal.protocols.randomized_benchmarking.fitting module#

In this python script the fitting methods for the gate set protocols are defined. They consist mostly of exponential decay fitting.

qibocal.protocols.randomized_benchmarking.fitting.exp1_func(x: ndarray, A: float, f: float) ndarray[source]#

Return \(A\cdot f^x\) where x is an np.ndarray and A, f are floats

qibocal.protocols.randomized_benchmarking.fitting.exp1B_func(x: ndarray, A: float, f: float, B: float) ndarray[source]#

Return \(A\cdot f^x+B\) where x is an np.ndarray and A, f, B are floats

qibocal.protocols.randomized_benchmarking.fitting.exp2_func(x: ndarray, A1: float, A2: float, f1: float, f2: float) ndarray[source]#

Return \(A_1\cdot f_1^x+A_2\cdot f_2^x\) where x is an np.ndarray and A1, f1, A2, f2 are floats. There is no linear offsett B.

qibocal.protocols.randomized_benchmarking.fitting.esprit(xdata: ndarray, ydata: ndarray, num_decays: int, hankel_dim: Optional[int] = None) ndarray[source]#

Implements the ESPRIT algorithm for peak detection.

Parameters:
  • xdata (np.ndarray) – Labels of data. Has to be equally spaced.

  • ydata (np.ndarray) – The data where multiple decays are fitted in.

  • num_decays (int) – How many decays should be fitted.

  • hankel_dim (int | None, optional) – The Hankel dimension. Defaults to None.

Returns:

The decay parameters.

Return type:

np.ndarray

Raises:

ValueError – When the x-labels are not equally spaced the algorithm does not work.

qibocal.protocols.randomized_benchmarking.fitting.fit_exp1B_func(xdata: Union[ndarray, list], ydata: Union[ndarray, list], **kwargs) tuple[tuple, tuple][source]#

Calculate an single exponential A*p^m+B fit to the given ydata.

Parameters:
  • xdata (Union[np.ndarray, list]) – The x-labels.

  • ydata (Union[np.ndarray, list]) – The data to be fitted.

Returns:

The fitting parameters (A, p, B) and the estimated error

(A_err, p_err, B_err)

Return type:

tuple[tuple, tuple]

qibocal.protocols.randomized_benchmarking.fitting.fit_exp1_func(xdata: Union[ndarray, list], ydata: Union[ndarray, list], **kwargs) tuple[tuple, tuple][source]#

Calculate an single exponential A*p^m fit to the given ydata, no linear offset.

Parameters:
  • xdata (Union[np.ndarray, list]) – The x-labels.

  • ydata (Union[np.ndarray, list]) – The data to be fitted.

Returns:

The fitting parameters (A, p) and the estimated error (A_err, p_err).

Return type:

tuple[tuple, tuple]

qibocal.protocols.randomized_benchmarking.fitting.fit_expn_func(xdata: Union[ndarray, list], ydata: Union[ndarray, list], n: int = 2) tuple[tuple, tuple][source]#

Calculate n exponentials on top of each other, fit to the given ydata. No linear offset, the ESPRIT algorithm is used to identify n exponential decays.

Parameters:
  • xdata (Union[np.ndarray, list]) – The x-labels.

  • ydata (Union[np.ndarray, list]) – The data to be fitted.

  • n (int) – number of decays to fit. Default is 2.

Returns:

(A1, …, An, f1, …, fn) with f* the decay parameters.

Return type:

tuple[tuple, tuple]

qibocal.protocols.randomized_benchmarking.fitting.fit_exp2_func(xdata: Union[ndarray, list], ydata: Union[ndarray, list]) tuple[tuple, tuple][source]#

Calculate 2 exponentials on top of each other, fit to the given ydata.

No linear offset, the ESPRIT algorithm is used to identify the two exponential decays.

Parameters:
  • xdata (Union[np.ndarray, list]) – The x-labels.

  • ydata (Union[np.ndarray, list]) – The data to be fitted

Returns:

(A1, A2, f1, f2) with f* the decay parameters.

Return type:

tuple[tuple, tuple]

qibocal.protocols.randomized_benchmarking.noisemodels module#

Custom error models are build here for making it possible to pass strings describing the error model via runcards in qibocal. They inherit from the qibo noise NoiseModel module and are prebuild.

class qibocal.protocols.randomized_benchmarking.noisemodels.PauliErrorOnAll(probabilities: Optional[list] = None)[source]#

Bases: NoiseModel

Builds a noise model with pauli flips acting on all gates in a Circuit. If no initial parameters for px, py, pz are given, random values are drawn (in sum not bigger than 1).

build()[source]#
class qibocal.protocols.randomized_benchmarking.noisemodels.PauliErrorOnX(probabilities: Optional[list] = None)[source]#

Bases: PauliErrorOnAll

Builds a noise model with pauli flips acting on X gates. Inherited from PauliErrorOnAll but the build method is overwritten to act on X gates. If no initial parameters for px, py, pz are given, random values are drawn (in sum not bigger than 1).

build()[source]#

qibocal.protocols.randomized_benchmarking.standard_rb module#

class qibocal.protocols.randomized_benchmarking.standard_rb.Depthsdict[source]#

Bases: TypedDict

dictionary used to build a list of depths as range(start, stop, step).

start: int#
stop: int#
step: int#
class qibocal.protocols.randomized_benchmarking.standard_rb.StandardRBParameters(depths: ~typing.Union[list, ~qibocal.protocols.randomized_benchmarking.standard_rb.Depthsdict], niter: int, uncertainties: ~typing.Optional[float] = None, unrolling: bool = False, seed: ~typing.Optional[int] = None, noise_model: ~typing.Optional[str] = None, noise_params: ~typing.Optional[list] = <factory>, nshots: int = 10)[source]#

Bases: Parameters

Standard Randomized Benchmarking runcard inputs.

depths: Union[list, Depthsdict]#

A list of depths/sequence lengths. If a dictionary is given the list will be build.

niter: int#

Sets how many iterations over the same depth value.

uncertainties: Optional[float] = None#

Method of computing the error bars of the signal and uncertainties of the fit. If None, it computes the standard deviation. Otherwise it computes the corresponding confidence interval. Defaults None.

unrolling: bool = False#

If True it uses sequence unrolling to deploy multiple circuits in a single instrument call. Defaults to False.

seed: Optional[int] = None#

A fixed seed to initialize np.random.Generator. If None, uses a random seed. Defaults is None.

noise_model: Optional[str] = None#

For simulation purposes, string has to match what is in qibocal.protocols.randomized_benchmarking.noisemodels

noise_params: Optional[list]#

With this the noise model will be initialized, if not given random values will be used.

nshots: int = 10#

Just to add the default value

class qibocal.protocols.randomized_benchmarking.standard_rb.StandardRBResult(fidelity: dict[Union[str, int], float], pulse_fidelity: dict[Union[str, int], float], fit_parameters: dict[Union[str, int], tuple[float, float, float]], fit_uncertainties: dict[Union[str, int], tuple[float, float, float]], error_bars: Optional[dict[Union[str, int], Union[float, list[float], NoneType]]] = None)[source]#

Bases: Results

Standard RB outputs.

fidelity: dict[Union[str, int], float]#

The overall fidelity of this qubit.

pulse_fidelity: dict[Union[str, int], float]#

The pulse fidelity of the gates acting on this qubit.

fit_parameters: dict[Union[str, int], tuple[float, float, float]]#

Raw fitting parameters.

fit_uncertainties: dict[Union[str, int], tuple[float, float, float]]#

Fitting parameters uncertainties.

error_bars: dict[Union[str, int], Union[float, list[float], NoneType]] = None#

Error bars for y.

qibocal.protocols.randomized_benchmarking.utils module#

qibocal.protocols.randomized_benchmarking.utils.random_clifford(random_index_gen)[source]#

Generates random Clifford operator.

Parameters:
  • qubits (int or list or ndarray) – if int, the number of qubits for the Clifford. If list or ndarray, indexes of the qubits for the Clifford to act on.

  • seed (int or numpy.random.Generator, optional) – Either a generator of random numbers or a fixed seed to initialize a generator. If None, initializes a generator with a random seed. Default is None.

Returns:

Random Clifford operator(s).

Return type:

(list of qibo.gates.Gate)

qibocal.protocols.randomized_benchmarking.utils.number_to_str(value: Number, uncertainty: Optional[Union[Number, list, tuple, ndarray]] = None, precision: Optional[int] = None)[source]#

Converts a number into a string.

Parameters:
  • value (Number) – the number to display

  • uncertainty (Number or list or tuple or np.ndarray, optional) – number or 2-element interval with the low and high uncertainties of value. Defaults to None.

  • precision (int, optional) – nonnegative number of floating points of the displayed value. If None, defaults to the second significant digit of uncertainty or 3 if uncertainty is None. Defaults to None.

Returns:

The number expressed as a string, with the uncertainty if given.

Return type:

str

qibocal.protocols.randomized_benchmarking.utils.data_uncertainties(data, method=None, data_median=None, homogeneous=True)[source]#

Compute the uncertainties of the median (or specified) values.

Parameters:
  • data (list or np.ndarray) – 2d array with rows containing data points from which the median value is extracted.

  • method (float, optional) – method of computing the method. If it is None, computes the standard deviation, otherwise it computes the corresponding confidence interval using np.percentile. Defaults to None.

  • data_median (list or np.ndarray, optional) – 1d array for computing the errors from the confidence interval. If None, the median values are computed from data.

  • homogeneous (bool) – if True, assumes that all rows in data are of the same size and returns np.ndarray. Default is True.

Returns:

uncertainties of the data.

Return type:

np.ndarray

class qibocal.protocols.randomized_benchmarking.utils.RB_Generator(seed)[source]#

Bases: object

This class generates random single qubit cliffords for randomized benchmarking.

random_index(gate_list)[source]#

Generates a random index within the range of the given gate list.

Parameters: - gate_list (list): Dict of gates.

Returns: - int: Random index.

layer_gen()[source]#

Returns: - Gate: Random single-qubit clifford .

qibocal.protocols.randomized_benchmarking.utils.random_circuits(depth: int, targets: list[Union[str, int]], niter, rb_gen, noise_model=None, inverse_layer=True) Iterable[source]#

Returns single-qubit random self-inverting Clifford circuits.

Parameters:
  • params (StandardRBParameters) – Parameters of the RB protocol.

  • targets (list[QubitId]) – list of qubits the circuit is executed on.

  • nqubits (int, optional) – Number of qubits of the resulting circuits. If None, sets len(qubits). Defaults to None.

  • inverse_layer (bool) – If True a layer inverting the circuit is added. Default to True.

Returns:

The iterator of circuits.

Return type:

Iterable

qibocal.protocols.randomized_benchmarking.utils.RBType = dtype([('samples', '<i4')])#

Custom dtype for RB.

class qibocal.protocols.randomized_benchmarking.utils.RBData(depths: list, uncertainties: ~typing.Optional[float], seed: ~typing.Optional[int], nshots: int, niter: int, data: dict[typing.Union[str, int], numpy.ndarray[typing.Any, numpy.dtype[dtype([('samples', '<i4')])]]] = <factory>, circuits: dict[typing.Union[str, int], list[list[int]]] = <factory>)[source]#

Bases: Data

The output of the acquisition function.

depths: list#

Circuits depths.

uncertainties: Optional[float]#

Parameters uncertainties.

seed: Optional[int]#
nshots: int#

Number of shots.

niter: int#

Number of iterations for each depth.

data: dict[typing.Union[str, int], numpy.ndarray[typing.Any, numpy.dtype[dtype([('samples', '<i4')])]]]#

Raw data acquired.

circuits: dict[Union[str, int], list[list[int]]]#

Clifford gate indexes executed.

extract_probabilities(qubit)[source]#

Extract the probabilities given qubit

qibocal.protocols.randomized_benchmarking.utils.rb_acquisition(params: Parameters, targets: list[Union[str, int]], add_inverse_layer: bool = True) Data[source]#

RB data acquisition function.

Parameters:
Returns:

The depths, samples and ground state probability of each experiment in the scan.

Return type:

RBData