qibocal.protocols.characterization.randomized_benchmarking package#

Submodules#

qibocal.protocols.characterization.randomized_benchmarking.circuit_tools module#

Collection of function to generate qibo circuits.

qibocal.protocols.characterization.randomized_benchmarking.circuit_tools.embed_circuit(circuit: Circuit, nqubits: int, qubits: list) Circuit[source]#

Embeds circuit into a larger circuit of size nqubits on the qubits specified by qubits.

Parameters
  • circuit (Circuit) – circuit to be embedded.

  • nqubits (int) – Number qubits in the large circuit.

  • qubits (list) – The qubits indices of larger circuit corresponding to qubits of `circuit.

Returns

Circuit with nqubits many qubits.

Return type

Circuit

qibocal.protocols.characterization.randomized_benchmarking.circuit_tools.layer_circuit(layer_gen: Callable, depth: int) Circuit[source]#

Creates a circuit of depth layers from a generator layer_gen yielding Circuit or Gate.

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.characterization.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.characterization.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.characterization.randomized_benchmarking.data module#

class qibocal.protocols.characterization.randomized_benchmarking.data.RBData(*args, **kwargs)[source]#

Bases: DataFrame

A pandas DataFrame child. The output of the acquisition function.

save(path)[source]#

qibocal.protocols.characterization.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.characterization.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.characterization.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.characterization.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.characterization.randomized_benchmarking.fitting.expn_func(x: Union[ndarray, list], *args) ndarray[source]#

Compute the sum of exponentials \(\sum A_i\cdot f_i^x\)

Parameters
  • x (np.ndarray | list) – list of exponents.

  • *args – Parameters of type float in the order \(A_1\), \(A_2\), \(f_1\), \(f_2\),…

Returns

The resulting sum of exponentials.

qibocal.protocols.characterization.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.characterization.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.characterization.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.characterization.randomized_benchmarking.fitting.fit_expn_func(xdata: Union[ndarray, list], ydata: Union[ndarray, list], n: int = 2, **kwargs) 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.characterization.randomized_benchmarking.fitting.fit_exp2_func(xdata: Union[ndarray, list], ydata: Union[ndarray, list], **kwargs) 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.characterization.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.characterization.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.characterization.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.characterization.randomized_benchmarking.standard_rb module#

class qibocal.protocols.characterization.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.characterization.randomized_benchmarking.standard_rb.StandardRBParameters(depths: ~typing.Union[list, ~qibocal.protocols.characterization.randomized_benchmarking.standard_rb.Depthsdict], niter: int, nshots: int, uncertainties: ~typing.Union[str, float] = 95, n_bootstrap: int = 100, seed: ~typing.Optional[int] = None, noise_model: str = '', noise_params: ~typing.Optional[list] = <factory>)[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.

nshots: int#

For each sequence how many shots for statistics should be performed.

uncertainties: Union[str, float] = 95#

Method of computing the error bars of the signal and uncertainties of the fit. If None, does not compute them. If "std", computes the standard deviation. If float or int between 0 and 100, computes the corresponding confidence interval. Defaults to 95.

n_bootstrap: int = 100#

Number of bootstrap iterations for the fit uncertainties and error bars. If 0, gets the fit uncertainties from the fitting function and the error bars from the distribution of the measurements. Defaults to 100.

seed: Optional[int] = None#

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

noise_model: str = ''#

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

noise_params: Optional[list]#

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

class qibocal.protocols.characterization.randomized_benchmarking.standard_rb.RBData(*args, **kwargs)[source]#

Bases: DataFrame

A pandas DataFrame child. The output of the acquisition function.

save(path)[source]#

Overwrite because qibocal action builder calls this function with a directory.

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

Bases: Results

Standard RB outputs.

fidelity: float#

The overall fidelity of this qubit.

pulse_fidelity: float#

The pulse fidelity of the gates acting on this qubit.

fit_parameters: tuple[float, float, float]#

Raw fitting parameters.

fit_uncertainties: tuple[float, float, float]#

Fitting parameters uncertainties.

error_bars: Optional[Union[float, list[float]]] = None#

Error bars for y.

qibocal.protocols.characterization.randomized_benchmarking.standard_rb.samples_to_p0(samples_list)[source]#

Computes the probabilitiy of 0 from the list of samples.

Parameters

samples_list (list or np.ndarray) – 3d array with ncircuits rows containing nshots lists with nqubits amount of 0 and 1 samples. e.g. samples_list for 1 circuit, 3 shots and 2 qubits looks like [[[0, 0], [0, 1], [1, 0]]] and p0=1/3.

Returns

list of probabilities corresponding to each row.

Return type

list

qibocal.protocols.characterization.randomized_benchmarking.standard_rb.resample_p0(data, sample_size=100, homogeneous: bool = True)[source]#
Preforms parametric resampling of shots with binomial distribution

and returns a list of “corrected” probabilites.

Parameters
  • data (list or np.ndarray) – list of probabilities for the binomial distribution.

  • nshots (int) – sample size for one probability distribution.

Returns

resampled probabilities.

Return type

list

qibocal.protocols.characterization.randomized_benchmarking.standard_rb.setup_scan(params: StandardRBParameters, qubits: Union[dict[Union[str, int], qibolab.qubits.Qubit], list[Union[str, int]]], nqubits: int) Iterable[source]#

Returns an iterator of single-qubit random self-inverting Clifford circuits.

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

  • qubits (dict[int, Union[str, int]] or list[Union[str, int]]) – 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.

Returns

The iterator of circuits.

Return type

Iterable

qibocal.protocols.characterization.randomized_benchmarking.utils module#

qibocal.protocols.characterization.randomized_benchmarking.utils.random_clifford(qubits, seed=None)[source]#

Generates random Clifford operator(s).

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.characterization.randomized_benchmarking.utils.significant_digit(number: Number)[source]#

Computes the position of the first significant digit of a given number.

Parameters

number (Number) – number for which the significant digit is computed. Can be complex.

Returns

position of the first significant digit. Returns -1 if the given number

is >= 1, = 0 or inf.

Return type

int

qibocal.protocols.characterization.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.characterization.randomized_benchmarking.utils.extract_from_data(data: Union[list[dict], DataFrame], output_key: str, groupby_key: str = '', agg_type: Union[str, Callable] = '') Union[ndarray, tuple[numpy.ndarray, numpy.ndarray]][source]#

Return wanted values from list of dictionaries via a dataframe and its properties.

If groupby_key given, aggregate the dataframe, extract the data by which the frame was grouped, what was calculated given the agg_type parameter. Two arrays are returned then, the group values and the grouped (aggregated) data. If no agg_type given use a linear function. If groupby_key not given, only return the extracted data from given key.

Parameters
  • output_key (str) – Key name of the wanted output.

  • groupby_key (str) – If given, group with that key name.

  • agg_type (str) – If given, calcuted aggregation function on groups.

Returns

Either one or two np.ndarrays. If no grouping wanted, just the data. If grouping wanted, the values after which where grouped and the grouped data.