Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quantum-kittens
GitHub Repository: quantum-kittens/platypus
Path: blob/main/notebooks/quantum-hardware/randomized-benchmarking.ipynb
3855 views
Kernel: Python 3

Randomized Benchmarking

Introduction

One of the main challenges in building a quantum information processor is the non-scalability of completely characterizing the noise affecting a quantum system via process tomography. In addition, process tomography is sensitive to noise in the pre- and post rotation gates plus the measurements (SPAM errors). Gateset tomography can take these errors into account, but the scaling is even worse. A complete characterization of the noise is useful because it allows for the determination of good error-correction schemes, and thus the possibility of reliable transmission of quantum information.

Since complete process tomography is infeasible for large systems, there is growing interest in scalable methods for partially characterizing the noise affecting a quantum system. A scalable (in the number nn of qubits comprising the system) and robust algorithm for benchmarking the full set of Clifford gates by a single parameter using randomization techniques was presented in [1]. The concept of using randomization methods for benchmarking quantum gates is commonly called Randomized Benchmarking (RB).

The Randomized Benchmarking Protocol

We should first import the relevant qiskit classes for the demonstration:

#Import general libraries (needed for functions) import numpy as np import matplotlib.pyplot as plt from IPython import display #Import the RB Functions import qiskit.ignis.verification.randomized_benchmarking as rb #Import Qiskit classes import qiskit from qiskit.providers.aer.noise import NoiseModel from qiskit.providers.aer.noise.errors.standard_errors import depolarizing_error, thermal_relaxation_error

A RB protocol (see [1,2]) consists of the following steps:

Step 1: Generate RB sequences

The RB sequences consist of random Clifford elements chosen uniformly from the Clifford group on nn-qubits, including a computed reversal element, that should return the qubits to the initial state.

More precisely, for each length mm, we choose KmK_m RB sequences. Each such sequence contains mm random elements CijC_{i_j} chosen uniformly from the Clifford group on nn-qubits, and the m+1m+1 element is defined as follows: Cim+1=(Ci1ā‹…...ā‹…Cim)āˆ’1C_{i_{m+1}} = (C_{i_1}\cdot ... \cdot C_{i_m})^{-1}. It can be found efficiently by the Gottesmann-Knill theorem.

For example, we generate below several sequences of 2-qubit Clifford circuits.

#Generate RB circuits (2Q RB) #number of qubits nQ=2 rb_opts = {} #Number of Cliffords in the sequence rb_opts['length_vector'] = [1, 10, 20, 50, 75, 100, 125, 150, 175, 200] #Number of seeds (random sequences) rb_opts['nseeds'] = 5 #Default pattern rb_opts['rb_pattern'] = [[0, 1]] rb_circs, xdata = rb.randomized_benchmarking_seq(**rb_opts)

As an example, we print the circuit corresponding to the first RB sequence

rb_circs[0][0].draw()
Image in a Jupyter notebook

One can verify that the Unitary representing each RB circuit should be the identity (with a global phase). We simulate this using Aer unitary simulator.

# Create a new circuit without the measurement qregs = rb_circs[0][-1].qregs cregs = rb_circs[0][-1].cregs qc = qiskit.QuantumCircuit(*qregs, *cregs) for i in rb_circs[0][-1][0:-nQ]: qc.data.append(i)
# The Unitary is an identity (with a global phase) backend = qiskit.Aer.get_backend('unitary_simulator') basis_gates = ['u1','u2','u3','cx'] # use U,CX for now job = qiskit.execute(qc, backend=backend, basis_gates=basis_gates) from qiskit_textbook.tools import array_to_latex array_to_latex(np.around(job.result().get_unitary(),3), pretext="\\text{Unitary} = ")

Unitary=[1000010000100001]\displaystyle \text{Unitary} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} LaTeX\LaTeX

Step 2: Execute the RB sequences (with some noise)

We can execute the RB sequences either using Qiskit Aer Simulator (with some noise model) or using IBMQ provider, and obtain a list of results.

By assumption each operation CijC_{i_j} is allowed to have some error, represented by Λij,j\Lambda_{i_j,j}, and each sequence can be modeled by the operation:

Sim=ā—Æj=1m+1(Ī›ij,j∘Cij)\textit{S}_{i_m} = \bigcirc_{j=1}^{m+1} (\Lambda_{i_j,j} \circ C_{i_j})

where im=(i1,...,im){i_m} = (i_1,...,i_m) and im+1i_{m+1} is uniquely determined by im{i_m}.

# Run on a noisy simulator noise_model = NoiseModel() # Depolarizing error on the gates u2, u3 and cx (assuming the u1 is virtual-Z gate and no error) p1Q = 0.002 p2Q = 0.01 noise_model.add_all_qubit_quantum_error(depolarizing_error(p1Q, 1), 'u2') noise_model.add_all_qubit_quantum_error(depolarizing_error(2 * p1Q, 1), 'u3') noise_model.add_all_qubit_quantum_error(depolarizing_error(p2Q, 2), 'cx') backend = qiskit.Aer.get_backend('qasm_simulator')

Step 3: Get statistics about the survival probabilities

For each of the KmK_m sequences the survival probability Tr[EψSim(ρψ)]Tr[E_\psi \textit{S}_{\textbf{i}_\textbf{m}}(\rho_\psi)] is measured. Here ρψ\rho_\psi is the initial state taking into account preparation errors and EψE_\psi is the POVM element that takes into account measurement errors. In the ideal (noise-free) case ρψ=Eψ=∣ψ⟩⟨ψ∣\rho_\psi = E_\psi = | \psi {\rangle} {\langle} \psi |.

In practice one can measure the probability to go back to the exact initial state, i.e. all the qubits in the ground state ∣00...0⟩ {|} 00...0 {\rangle} or just the probability for one of the qubits to return back to the ground state. Measuring the qubits independently can be more convenient if a correlated measurement scheme is not possible. Both measurements will fit to the same decay parameter according to the properties of the twirl.

Step 4: Find the averaged sequence fidelity

Average over the KmK_m random realizations of the sequence to find the averaged sequence fidelity,

Fseq(m,∣ψ⟩)=Tr[EψSKm(ρψ)]F_{seq}(m,|\psi{\rangle}) = Tr[E_\psi \textit{S}_{K_m}(\rho_\psi)]

where

SKm=1Kmāˆ‘imSim\textit{S}_{K_m} = \frac{1}{K_m} \sum_{\textbf{i}_\textbf{m}} \textit{S}_{\textbf{i}_\textbf{m}}

is the average sequence operation.

Step 5: Fit the results

Repeat Steps 1 through 4 for different values of mm and fit the results for the averaged sequence fidelity to the model:

Fseq(0)(m,∣ψ⟩)=A0αm+B0\textit{F}_{seq}^{(0)} \big(m,{|}\psi {\rangle} \big) = A_0 \alpha^m +B_0

where A0A_0 and B0B_0 absorb state preparation and measurement errors as well as an edge effect from the error on the final gate.

α\alpha determines the average error-rate rr, which is also called Error per Clifford (EPC) according to the relation

r=1āˆ’Ī±āˆ’1āˆ’Ī±2n=2nāˆ’12n(1āˆ’Ī±)r = 1-\alpha-\frac{1-\alpha}{2^n} = \frac{2^n-1}{2^n}(1-\alpha)

(where n=nQn=nQ is the number of qubits).

As an example, we calculate the average sequence fidelity for each of the RB sequences, fit the results to the exponential curve, and compute the parameters α\alpha and EPC.

# Create the RB fitter backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1','u2','u3','cx'] shots = 200 transpiled_circs_list = [] rb_fit = rb.RBFitter(None, xdata, rb_opts['rb_pattern']) for rb_seed, rb_circ_seed in enumerate(rb_circs): print('Compiling seed %d'%rb_seed) new_rb_circ_seed = qiskit.compiler.transpile(rb_circ_seed, basis_gates=basis_gates) transpiled_circs_list.append(new_rb_circ_seed) print('Simulating seed %d'%rb_seed) job = qiskit.execute(new_rb_circ_seed, backend, shots=shots, noise_model=noise_model, backend_options={'max_parallel_experiments': 0}) # Add data to the fitter rb_fit.add_data(job.result()) print('After seed %d, alpha: %f, EPC: %f'%(rb_seed,rb_fit.fit[0]['params'][1], rb_fit.fit[0]['epc']))

Extra Step: Plot the results

plt.figure(figsize=(8, 6)) ax = plt.subplot(1, 1, 1) # Plot the essence by calling plot_rb_data rb_fit.plot_rb_data(0, ax=ax, add_label=True, show_plt=False) # Add title and label ax.set_title('%d Qubit RB'%(nQ), fontsize=18) plt.show()
Image in a Jupyter notebook

The Intuition Behind RB

The depolarizing quantum channel has a parameter α\alpha, and works like this: with probability α\alpha, the state remains the same as before; with probability 1āˆ’Ī±1-\alpha, the state becomes the totally mixed state, namely:

ρf=αρi+1āˆ’Ī±2nāˆ—I\rho_f = \alpha \rho_i + \frac{1-\alpha}{2^n} * \mathbf{I}

Suppose that we have a sequence of mm gates, not necessarily Clifford gates, where the error channel of the gates is a depolarizing channel with parameter α\alpha (same α\alpha for all the gates). Then with probability αm\alpha^m the state is correct at the end of the sequence, and with probability 1āˆ’Ī±m1-\alpha^m it becomes the totally mixed state, therefore:

ρfm=αmρi+1āˆ’Ī±m2nāˆ—I\rho_f^m = \alpha^m \rho_i + \frac{1-\alpha^m}{2^n} * \mathbf{I}

Now suppose that in addition we start with the ground state; that the entire sequence amounts to the identity; and that we measure the state at the end of the sequence with the standard basis. We derive that the probability of success at the end of the sequence is:

αm+1āˆ’Ī±m2n=2nāˆ’12nαm+12n=A0αm+B0\alpha^m + \frac{1-\alpha^m}{2^n} = \frac{2^n-1}{2^n}\alpha^m + \frac{1}{2^n} = A_0\alpha^m + B_0

It follows that the probability of success, aka fidelity, decays exponentially with the sequence length, with exponent α\alpha.

The last statement is not necessarily true when the channel is other than the depolarizing channel. However, it turns out that if the gates are uniformly-randomized Clifford gates, then the noise of each gate behaves on average as if it was the depolarizing channel, with some parameter that can be computed from the channel, and we obtain the exponential decay of the fidelity.

Formally, taking an average over a finite group GG (like the Clifford group) of a quantum channel Λˉ\bar \Lambda is also called a twirl:

WG(Λˉ)1∣Gāˆ£āˆ‘u∈GUā€ āˆ˜Ī›Ė‰āˆ˜UW_G(\bar \Lambda) \frac{1}{|G|} \sum_{u \in G} U^{\dagger} \circ \bar \Lambda \circ U

Twirling over the entire unitary group yields exactly the same result as the Clifford group. The Clifford group is a 2-design of the unitary group.

Simultaneous Randomized Benchmarking

RB is designed to address fidelities in multiqubit systems in two ways. For one, RB over the full nn-qubit space can be performed by constructing sequences from the nn-qubit Clifford group. Additionally, the nn-qubit space can be subdivided into sets of qubits {ni} \{n_i\} and nin_i-qubit RB performed in each subset simultaneously [4]. Both methods give metrics of fidelity in the nn-qubit space.

For example, it is common to perform 2Q RB on the subset of two-qubits defining a CNOT gate while the other qubits are quiescent. As explained in [4], this RB data will not necessarily decay exponentially because the other qubit subspaces are not twirled. Subsets are more rigorously characterized by simultaneous RB, which also measures some level of crosstalk error since all qubits are active.

An example of simultaneous RB (1Q RB and 2Q RB) can be found in: https://github.com/Qiskit/qiskit-tutorials/blob/master/tutorials/noise/4_randomized_benchmarking.ipynb

Predicted Gate Fidelity

If we know the errors on the underlying gates (the gateset) we can predict the EPC without running RB experiment. This calculation verifies that your RB experiment followed by fitting yields correct EPC value. First we need to count the number of these gates per Clifford.

Then, the two qubit Clifford gate error function calculate_2q_epc gives the error per 2Q Clifford. It assumes that the error in the underlying gates is depolarizing. This function is derived in the supplement to [5].

# count the number of single and 2Q gates in the 2Q Cliffords qubits = rb_opts['rb_pattern'][0] gate_per_cliff = rb.rb_utils.gates_per_clifford( transpiled_circuits_list=transpiled_circs_list, clifford_lengths=xdata[0], basis=basis_gates, qubits=qubits) for basis_gate in basis_gates: print("Number of %s gates per Clifford: %f"%( basis_gate, np.mean([gate_per_cliff[qubit][basis_gate] for qubit in qubits])))
Number of u1 gates per Clifford: 0.120087 Number of u2 gates per Clifford: 1.656878 Number of u3 gates per Clifford: 0.158406 Number of cx gates per Clifford: 1.464847
# convert from depolarizing error to epg (1Q) epg_q0 = {'u1': 0, 'u2': p1Q/2, 'u3': 2 * p1Q/2} epg_q1 = {'u1': 0, 'u2': p1Q/2, 'u3': 2 * p1Q/2} # convert from depolarizing error to epg (2Q) epg_q01 = 3/4 * p2Q # calculate the predicted epc from underlying gate errors pred_epc = rb.rb_utils.calculate_2q_epc( gate_per_cliff=gate_per_cliff, epg_2q=epg_q01, qubit_pair=qubits, list_epgs_1q=[epg_q0, epg_q1]) print("Predicted 2Q Error per Clifford: %e (qasm simulator result: %e)" % (pred_epc, rb_fit.fit[0]['epc']))
Predicted 2Q Error per Clifford: 1.561771e-02 (qasm simulator result: 1.673676e-02)

On the other hand, we can calculate the errors on the underlying gates (the gateset) from the experimentally obtained EPC. Given that we know the errors on the every single-qubit gates in the RB sequence, we can predict 2Q gate error from the EPC of two qubit RB experiment.

The two qubit gate error function calculate_2q_epg gives the estimate of error per 2Q gate. In this section we prepare single-qubit errors using the deporalizing error model. If the error model is unknown, EPGs of those gates, for example [u1, u2, u3], can be estimated with a separate 1Q RB experiment with the utility function calculate_1q_epg.

# use 2Q EPC from qasm simulator result and 1Q EPGs from depolarizing error model pred_epg = rb.rb_utils.calculate_2q_epg( gate_per_cliff=gate_per_cliff, epc_2q=rb_fit.fit[0]['epc'], qubit_pair=qubits, list_epgs_1q=[epg_q0, epg_q1]) print("Predicted 2Q Error per gate: %e (gate error model: %e)" % (pred_epg, epg_q01))
Predicted 2Q Error per gate: 8.251320e-03 (gate error model: 7.500000e-03)

References

  1. Easwar Magesan, J. M. Gambetta, and Joseph Emerson, Robust randomized benchmarking of quantum processes, https://arxiv.org/pdf/1009.3639

  2. Easwar Magesan, Jay M. Gambetta, and Joseph Emerson, Characterizing Quantum Gates via Randomized Benchmarking, https://arxiv.org/pdf/1109.6887

  3. A. D. C'orcoles, Jay M. Gambetta, Jerry M. Chow, John A. Smolin, Matthew Ware, J. D. Strand, B. L. T. Plourde, and M. Steffen, Process verification of two-qubit quantum gates by randomized benchmarking, https://arxiv.org/pdf/1210.7011

  4. Jay M. Gambetta, A. D. C“orcoles, S. T. Merkel, B. R. Johnson, John A. Smolin, Jerry M. Chow, Colm A. Ryan, Chad Rigetti, S. Poletto, Thomas A. Ohki, Mark B. Ketchen, and M. Steffen, Characterization of addressability by simultaneous randomized benchmarking, https://arxiv.org/pdf/1204.6308

  5. David C. McKay, Sarah Sheldon, John A. Smolin, Jerry M. Chow, and Jay M. Gambetta, Three Qubit Randomized Benchmarking, https://arxiv.org/pdf/1712.06550

import qiskit qiskit.__qiskit_version__