# How to calculate the fidelity of a certain gate of a IBMQ device in Qiskit using randomized benchmarking/tomography?

For example, I want to calculate the fidelity of a 1-qubit and 2-qubit gates (similar to the result shown in figure 2 in this paper). Is there any way to do that in Qiskit? I've gone through the Qiskit Ignis documentation, but I didn't see if it's relevant.

• Until you get a full answer, this will tell you about getting state fidelities: github.com/Qiskit/qiskit-tutorials/blob/…. Gate fidelities are typically a generaliztion of this concept. Commented Jun 4, 2019 at 18:03
• Thank you for your reply. I know I can get state fidelity using simulator backends with some noise model, but is it possible to do the same thing on a real device backend? Commented Jun 4, 2019 at 18:43

Fidelity is a single-number measure of how good a gate is. Since there are many ways that a gate can go wrong, there are multiple ways that the fidelity can be defined. The exact answer to your question will therefore depend on which kind of fidelity you want.

Any measure of fidelity will typically involve comparing the gate that you wanted to the channel that actually happened. This channel can be described by a Choi matrix. More discussion of channels and Choi matrices can be found in the answer to this question.

For a concrete example in Qiskit, see the notebook on how to use the tomography tool from Qiskit Ignis. For example, here is the tomography of a single qubit Hadamard gate.

# Process tomography of a Hadamard gate
q = QuantumRegister(1)
circ = QuantumCircuit(q)
circ.h(q[0])

# Run circuit on unitary simulator to find ideal unitary
job = qiskit.execute(circ, Aer.get_backend('unitary_simulator'))
ideal_unitary = job.result().get_unitary(circ)
# convert to Choi-matrix in column-major convention
choi_ideal = outer(ideal_unitary.ravel(order='F'))

# Generate process tomography circuits and run on qasm simulator
qpt_circs = process_tomography_circuits(circ, q)
job = qiskit.execute(qpt_circs, Aer.get_backend('qasm_simulator'), shots=4000)

# Extract tomography data so that counts are indexed by measurement configuration
qpt_tomo = ProcessTomographyFitter(job.result(), qpt_circs)
qpt_tomo.data


Given the data, we can then find the best fit to a Choi matrix. For example, using the MLE Least-Squares tomographic reconstruction.

choi_lstsq = qpt_tomo.fit(method='lstsq')


By comparing this with the ideal Choi matrix, we can calculate the fidelity using the state_fidelity and process_fidelity tools. For the latter, we'll need to use require_cptp=False in case the Choi matrix doesn't quite describe a cptp map.

print('fit fidelity (state):', state_fidelity(choi_ideal / 2, choi_lstsq.data / 2))
print('fit fidelity (process):', np.real(process_fidelity(choi_ideal, choi_lstsq.data, require_cptp=False)))


This should give an output that is something like

fit fidelity (state): 0.9976767994222256
fit fidelity (process): 0.995358994837865