See also

A Jupyter notebook version of this tutorial can be downloaded here.

image0

[1]:
from __future__ import annotations

import numpy as np
import rich  # noqa:F401
from pycqed_randomized_benchmarking.utils import RBAnalysis, randomized_benchmarking_schedule
from qcodes import ManualParameter
from simulated_data import ErrorModel, simulated_rb_get

import quantify_core.data.handling as dh
from quantify_scheduler import QuantumDevice
from quantify_scheduler.gettables import ScheduleGettable

from utils import (  # noqa:F401  # noqa:F401
    display_dict,
    initialize_hardware,
    run_schedule,
    show_connectivity,
)
Generating Clifford hash tables.
Successfully generated Clifford hash tables.
[2]:
hw_config_path = "configs/tuning_transmon_coupled_pair_hardware_config.json"
device_path = "devices/transmon_device_2q.json"
[3]:
# Enter your own dataset directory here!
dh.set_datadir(dh.default_datadir())
Data will be saved in:
/root/quantify-data
[4]:
quantum_device = QuantumDevice.from_json_file(device_path)
qubit = quantum_device.get_element("q0")
quantum_device.hardware_config.load_from_json_file(hw_config_path)
cluster_ip = None
meas_ctrl, inst_coord, cluster = initialize_hardware(quantum_device, ip=cluster_ip)

Randomized benchmarking#

This application example is qubit type agnostic, i.e. it can be applied for any type of qubit (e.g. transmon, spin, etc.).

For demonstration, we will assume that the qubit type is flux-tunable transmon.

The experiments can also be executed using a dummy Qblox device that is created via an instance of the Cluster class, and is initialized with a dummy configuration. However, when using a dummy device, the analysis will not work because the experiments will return np.nan values.

Setup#

In this section we configure the hardware configuration which specifies the connectivity of our system.

The experiments of this tutorial are meant to be executed with a Qblox Cluster controlling a transmon system. The experiments can also be executed using a dummy Qblox device that is created via an instance of the Cluster class, and is initialized with a dummy configuration. When using a dummy device, the analysis will not work because the experiments will return np.nan values.

Configuration file#

This is a template hardware configuration file for a 2-qubit system with a flux-control line which can be used to tune the qubit frequency. We will only work with qubit 0.

The hardware connectivity is as follows, by cluster slot:

  • QCM (Slot 2)

    • \(\text{O}^{1}\): Flux line for q0.

    • \(\text{O}^{2}\): Flux line for q1.

  • QCM-RF (Slot 6)

    • \(\text{O}^{1}\): Drive line for q0 using fixed 80 MHz IF.

    • \(\text{O}^{2}\): Drive line for q1 using fixed 80 MHz IF.

  • QRM-RF (Slot 8)

    • \(\text{O}^{1}\) and \(\text{I}^{1}\): Shared readout line for q0/q1 using a fixed LO set at 7.5 GHz.

Note that in the hardware configuration below the mixers are uncorrected, but for high fidelity experiments this should also be done for all the modules.

Quantum device settings#

Here we initialize our QuantumDevice and our qubit parameters, checkout this tutorial for further details.

In short, a QuantumDevice contains device elements where we save our found parameters. Here we are loading a template for 2 qubits, but we will only use qubit 0.

[5]:
cluster_ip = None  # To run this tutorial on hardware, fill in the IP address of the cluster here
[6]:
q0 = quantum_device.get_element("q0")
q1 = quantum_device.get_element("q1")

Configure external flux control#

We need to have some way of controlling the external flux.

This can be done by setting an output bias on a module of the cluster which is then connected to the flux-control line.

Here we are nullifying the external flux on both qubits.

[7]:
flux_settables = {q0.name: cluster.module2.out0_offset, q1.name: cluster.module2.out1_offset}

for flux_settable in flux_settables.values():
    flux_settable.inter_delay = 100e-9  # Delay time in seconds between consecutive set operations.
    flux_settable.step = 0.3e-3  # Stepsize in V that this Parameter uses during set operation.
    flux_settable()  # Get before setting to avoid jumps.
    flux_settable(0.0)
[8]:
flux_settables[q0.name](0.0)  # enter your own value here for qubit 0
flux_settables[q1.name](0.0)  # enter your own value here for qubit 1

Single qubit RB example#

Set up the measurement control gettables#

[9]:
length = ManualParameter(name="length", unit="#", label="Number of Clifford gates")
length.batched = True
length_setpoints = np.arange(1, 40, 2)

seed = ManualParameter(name="seed", unit="", label="Seeds")
seed.batched = True
seed_setpoints = np.random.randint(0, 2**31 - 1, size=32, dtype=np.int32)

rb_sched_kwargs = {
    "qubit_specifier": q0.name,
    "lengths": length,
    "seeds": seed,
}

gettable = ScheduleGettable(
    quantum_device,
    schedule_function=randomized_benchmarking_schedule,
    schedule_kwargs=rb_sched_kwargs,
    real_imag=False,
    batched=True,
    num_channels=1,
    max_batch_size=16,
)
error_model = ErrorModel()
# replace the get method for the gettable in case the cluster is a dummy
if cluster_ip is None:
    gettable.error_model = error_model
    gettable.old_get = gettable.get
    gettable.get = lambda: simulated_rb_get(gettable)
meas_ctrl.gettables(gettable)

meas_ctrl.settables([length, seed])
meas_ctrl.setpoints_grid([length_setpoints, seed_setpoints])

quantum_device.cfg_sched_repetitions(32)

Example: coherent phase error (simulated data is generated only when cluster_ip = None)#

Run the experiment#

[10]:
error_model.reset()  # Set all errors to 0
error_model.coherent_phase_error = 0.025 * np.pi
rb_coherent_error_ds = meas_ctrl.run(
    "Randomized benchmarking on " + rb_sched_kwargs["qubit_specifier"]
)
Starting batched measurement...
Iterative settable(s) [outer loop(s)]:
         --- (None) ---
Batched settable(s):
         length, seed
Batch size limit: 16

Analyze the results#

Here we fit the decay to

\[P = A\,\alpha^m+B,\]

where the depolarizing parameter \(\alpha\) is related to the error per Clifford \(r\) by (arxiv:1712.06550 eq. 1)

\[r = \dfrac{2^n-1}{2^n}(1-\alpha),\]

and the fidelity is

\[F_\text{RB}=1-r.\]
[11]:
rb_coherent_error_result = RBAnalysis(
    dataset=rb_coherent_error_ds,
    label="Randomized benchmarking",
    settings_overwrite={"mpl_transparent_background": False},
    repetitions=gettable.compiled_schedule.repetitions,
).run()
print(f"Depolarizing parameter: \u03b1={rb_coherent_error_result.alpha}")
print(f"Error per Clifford: r={rb_coherent_error_result.r}")
print(f"Fidelity: F={1 - rb_coherent_error_result.r}")
print(
    f"Sample size explains {100 * rb_coherent_error_result.sample_size_variance:.1f} (+{100 * rb_coherent_error_result.szv_hi_err:.1f}, -{100 * rb_coherent_error_result.szv_lo_err:.1f})% of the variance (2\u03c3 confidence). High variance may indicate coherent errors."
)
rb_coherent_error_result.display_figs_mpl()
Depolarizing parameter: α=0.9909174884171136
Error per Clifford: r=0.0045412557914432106
Fidelity: F=0.9954587442085567
Sample size explains 46.5 (+12.8, -8.3)% of the variance (2σ confidence). High variance may indicate coherent errors.
../../../_images/applications_quantify_transmon_randomized_benchmarking_20_1.png
../../../_images/applications_quantify_transmon_randomized_benchmarking_20_2.png

Example: incoherent depolarizing error (simulated data is generated only when cluster_ip = None)#

Run the experiment#

[12]:
error_model.reset()  # Set all errors to 0
error_model.depolarizing_error = 0.01 * np.pi
rb_depolarizing_error_ds = meas_ctrl.run(
    "Randomized benchmarking on " + rb_sched_kwargs["qubit_specifier"]
)
Starting batched measurement...
Iterative settable(s) [outer loop(s)]:
         --- (None) ---
Batched settable(s):
         length, seed
Batch size limit: 16

Analyze the results#

[13]:
rb_depolarizing_error_result = RBAnalysis(
    dataset=rb_depolarizing_error_ds,
    label="Randomized benchmarking",
    settings_overwrite={"mpl_transparent_background": False},
    repetitions=gettable.compiled_schedule.repetitions,
).run()
print(f"Depolarizing parameter: \u03b1={rb_depolarizing_error_result.alpha}")
print(f"Error per Clifford: r={rb_depolarizing_error_result.r}")
print(f"Fidelity: F={1 - rb_depolarizing_error_result.r}")
print(
    f"Sample size explains {100 * rb_depolarizing_error_result.sample_size_variance:.1f} (+{100 * rb_depolarizing_error_result.szv_hi_err:.1f}, -{100 * rb_depolarizing_error_result.szv_lo_err:.1f})% of the variance (2\u03c3 confidence). High variance may indicate coherent errors."
)
rb_depolarizing_error_result.display_figs_mpl()
Depolarizing parameter: α=0.9325000537261352
Error per Clifford: r=0.0337499731369324
Fidelity: F=0.9662500268630676
Sample size explains 95.8 (+13.9, -10.8)% of the variance (2σ confidence). High variance may indicate coherent errors.
../../../_images/applications_quantify_transmon_randomized_benchmarking_25_1.png
../../../_images/applications_quantify_transmon_randomized_benchmarking_25_2.png
[14]:
inst_coord.last_schedule.compiled_instructions
[15]:
display_dict(quantum_device.hardware_config())
[15]:
[16]:
quantum_device.to_json_file("devices/")
[16]:
'devices/device_2q_2025-06-23_09-11-52_UTC.json'