Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.03k stars 2.32k forks source link

How do I perform a Ramsey experiment in other backends than IBMQX2? #4683

Closed SergioUnoxx closed 4 years ago

SergioUnoxx commented 4 years ago

Hi! Recently, I performed a Ramsey experiment in the IBMQX2 backend, using the following code (https://quantum-computing.ibm.com/docs/guide/wwwq/decoherence) and obtained this figure:

image

However, Qiskit won't let me measure more times, and I like to observe the fringes' decay at long times. To do so, I tried to perform the same experiment on another backend, Burlington, but I just get gibberish like this:

burlington gib

What am I doing wrong? I'm just changing the backend's name, but I assume that is not the only thing I have to do. Could anybody tell me? Also, I know I can do it on Armonk with pulses, but I'm not interested on that. If there's another way to observe the fringes' decay at long times, I'm listening.

taalexander commented 4 years ago

This could be related to additional coherent noise playing a role at long time frames. Are you running an echoed sequence? See here for more detail on how to estimate T2

SergioUnoxx commented 4 years ago

If with echoed sequence you are referring to a Hahn echo experiment, no, it is a Ramsey experiment. I'm applying a pi/2 gate, and then another one after a certain time for each measure. The link you shared shows how to perform decoherence experiments in a backend simulator, but I'm only interested in performing them in real qubits. Is that tutorial valid for real backends, like ibmq_essex or ibmqx2?

taalexander commented 4 years ago

Yes it should be valid for real backends as well. There is a difference between T2* and T2 times, and you may be running into coherent noise.

SergioUnoxx commented 4 years ago

I'm replacing 'qasm_simulator' for, say, 'ibmq_essex' , but it yields an error. I'm also trying to use the backend lines of code from the cod I posted first, but it's no good either. What's the problem? Sorry if my questions seem to be very basic, I haven't been using Qiskit for too long. And by the way, thanks for your help!

taalexander commented 4 years ago

Could you please post a stack trace of the error and how you are calling it? You likely are providing unnecessary settings to execute that are simulation only.

SergioUnoxx commented 4 years ago

This is the code:

import numpy as np
import matplotlib.pyplot as plt

import qiskit
from qiskit.providers.aer.noise.errors.standard_errors import thermal_relaxation_error
from qiskit.providers.aer.noise import NoiseModel

from qiskit.ignis.characterization.coherence import T1Fitter, T2StarFitter, T2Fitter
from qiskit.ignis.characterization.coherence import t1_circuits, t2_circuits, t2star_circuits

num_of_gates = (np.linspace(10, 300, 50)).astype(int)
gate_time = 0.1

# Note that it is possible to measure several qubits in parallel
qubits = [0, 2]

t1_circs, t1_xdata = t1_circuits(num_of_gates, gate_time, qubits)
t2star_circs, t2star_xdata, osc_freq = t2star_circuits(num_of_gates, gate_time, qubits, nosc=5)
t2echo_circs, t2echo_xdata = t2_circuits(np.floor(num_of_gates/2).astype(int), 
                                         gate_time, qubits)
t2cpmg_circs, t2cpmg_xdata = t2_circuits(np.floor(num_of_gates/6).astype(int), 
                                         gate_time, qubits, 
                                         n_echos=5, phase_alt_echo=True)

from qiskit import IBMQ
IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q', group='open', project='main')
backend = provider.get_backend('ibmq_essex')
shots = 400

# Let the simulator simulate the following times for qubits 0 and 2:
t_q0 = 25.0
t_q2 = 15.0

# Define T1 and T2 noise:
t1_noise_model = NoiseModel()
t1_noise_model.add_quantum_error(
    thermal_relaxation_error(t_q0, 2*t_q0, gate_time), 
    'id', [0])
t1_noise_model.add_quantum_error(
    thermal_relaxation_error(t_q2, 2*t_q2, gate_time), 
    'id', [2])

t2_noise_model = NoiseModel()
t2_noise_model.add_quantum_error(
    thermal_relaxation_error(np.inf, t_q0, gate_time, 0.5), 
    'id', [0])
t2_noise_model.add_quantum_error(
    thermal_relaxation_error(np.inf, t_q2, gate_time, 0.5), 
    'id', [2])

# Run the simulator
t1_backend_result = qiskit.execute(t1_circs, backend, shots=shots,
                                   noise_model=t1_noise_model, optimization_level=0).result()
t2star_backend_result = qiskit.execute(t2star_circs, backend, shots=shots,
                                       noise_model=t2_noise_model, optimization_level=0).result()
t2echo_backend_result = qiskit.execute(t2echo_circs, backend, shots=shots,
                                       noise_model=t2_noise_model, optimization_level=0).result()

# It is possible to split the circuits into multiple jobs and then give the results to the fitter as a list:
t2cpmg_backend_result1 = qiskit.execute(t2cpmg_circs[0:5], backend,
                                        shots=shots, noise_model=t2_noise_model,
                                        optimization_level=0).result()
t2cpmg_backend_result2 = qiskit.execute(t2cpmg_circs[5:], backend,
                                        shots=shots, noise_model=t2_noise_model,
                                        optimization_level=0).result()

And this is the error:

ibmqfactory.load_account:WARNING:2020-07-13 17:59:39,106: Credentials are already in use. The existing account in the session will be replaced.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-109c7ba74f94> in <module>
     27 
     28 # Run the simulator
---> 29 t1_backend_result = qiskit.execute(t1_circs, backend, shots=shots,
     30                                    noise_model=t1_noise_model, optimization_level=0).result()
     31 t2star_backend_result = qiskit.execute(t2star_circs, backend, shots=shots,

~\Anaconda3\envs\name_of_my_env\lib\site-packages\qiskit\execute.py in execute(experiments, backend, basis_gates, coupling_map, backend_properties, initial_layout, seed_transpiler, optimization_level, pass_manager, qobj_id, qobj_header, shots, memory, max_credits, seed_simulator, default_qubit_los, default_meas_los, schedule_los, meas_level, meas_return, memory_slots, memory_slot_size, rep_time, parameter_binds, schedule_circuit, inst_map, meas_map, scheduling_method, **run_config)
    281     # executing the circuits on the backend and returning the job
    282     start_time = time()
--> 283     job = backend.run(qobj, **run_config)
    284     end_time = time()
    285     _log_submission_time(start_time, end_time)

TypeError: run() got an unexpected keyword argument 'noise_model'
taalexander commented 4 years ago

Yes, please remove the noise model. It is only for an Aer backend. You want something like:

t2cpmg_backend_result2 = qiskit.execute(t2cpmg_circs[5:], backend,
                                        shots=shots, optimization_level=0).result()
SergioUnoxx commented 4 years ago

I see. The noise model is not necessary since I'm performing experiments on real backends. Thanks to your comment, I was able to make it work and measure T1 for Q1 on ibmq_london! However, I run into some troubles while measuring T2*: I use the following formula to calculate it theoretically from the T1 and T2 values for Q1 which appear in the backend's .csv on https://quantum-computing.ibm.com/:

image

According to that, T2 should yield 292 us for Q1 (according to Jul 17 9:21 calibrations, T1=49.639167544994976 T2=74.09267435485005). However, the T2 fit for the code returns T2=15.7 us. Is something wrong in the code, or is it that Qiskit uses another formula to calculate T2?

Also, the tutorial shows figures for estimating "T2 CPMG", what's the difference between that and T2 echo? Could it be related to my problem?

Here is the code, for which the fit returns T1 just fine:

import numpy as np
import matplotlib.pyplot as plt

import qiskit
from qiskit.providers.aer.noise.errors.standard_errors import thermal_relaxation_error
from qiskit.providers.aer.noise import NoiseModel

from qiskit.ignis.characterization.coherence import T1Fitter, T2StarFitter, T2Fitter
from qiskit.ignis.characterization.coherence import t1_circuits, t2_circuits, t2star_circuits

num_of_gates = (np.linspace(10, 1500, 50)).astype(int)
gate_time = 0.03555555555555556
tq_0 = 30

# Note that it is possible to measure several qubits in parallel
qubits = [1]

t1_circs, t1_xdata = t1_circuits(num_of_gates, gate_time, qubits)
t2star_circs, t2star_xdata, osc_freq = t2star_circuits(num_of_gates, gate_time, qubits, nosc=5)
t2echo_circs, t2echo_xdata = t2_circuits(np.floor(num_of_gates/2).astype(int), 
                                         gate_time, qubits)
t2cpmg_circs, t2cpmg_xdata = t2_circuits(np.floor(num_of_gates/6).astype(int), 
                                         gate_time, qubits, 
                                         n_echos=5, phase_alt_echo=True)

from qiskit import IBMQ
IBMQ.load_account()
provider = IBMQ.get_provider(hub='ibm-q', group='open', project='main')
backend = provider.get_backend('ibmq_london')

shots = 400
# Run the simulator
t1_backend_result = qiskit.execute(t1_circs, backend, shots=shots,
                                  optimization_level=0).result()
t2star_backend_result = qiskit.execute(t2star_circs, backend, shots=shots,
                                      optimization_level=0).result()
t2echo_backend_result = qiskit.execute(t2echo_circs, backend, shots=shots,
                                       optimization_level=0).result()

# Fitting T1

%matplotlib inline

plt.figure(figsize=(15, 6))

t1_fit = T1Fitter(t1_backend_result, t1_xdata, qubits,
                  fit_p0=[1, t_q0, 0],
                  fit_bounds=([0, 0, -1], [2, 200, 1]))
print(t1_fit.time())
print(t1_fit.time_err())
print(t1_fit.params)
print(t1_fit.params_err)
for i in range(1):
    ax = plt.subplot(1, 2, i+1)
    t1_fit.plot(i, ax=ax)
plt.show()

I ran this following code three or so more times to obtain more statistics:

t1_backend_result_new = qiskit.execute(t1_circs, backend,
                                       shots=shots,
                                       optimization_level=0).result()
t1_fit.add_data(t1_backend_result_new)

plt.figure(figsize=(15, 6))
for i in range(1):
    ax = plt.subplot(1, 2, i+1)
    t1_fit.plot(i, ax=ax)    
plt.show()

image

However, I plot T2* then and it yields a value different from my expected one:

# Fitting T2*

%matplotlib inline

t2star_fit = T2StarFitter(t2star_backend_result, t2star_xdata, qubits,
                          fit_p0=[0.5, t_q0, osc_freq, 0, 0.5],
                          fit_bounds=([-0.5, 0, 0, -np.pi, -0.5],
                                      [1.5, 1000, 2*osc_freq, np.pi, 1.5]))

plt.figure(figsize=(15, 6))
for i in range(1):
    ax = plt.subplot(1, 2, i+1)
    t2star_fit.plot(i, ax=ax)    
plt.show()

Again, I run the following code for more statistics three more times or so:

t2star_backend_result_new = qiskit.execute(t2star_circs, backend,
                                       shots=shots,
                                       optimization_level=0).result()
t2star_fit.add_data(t2star_backend_result_new)

plt.figure(figsize=(15, 6))
for i in range(1):
    ax = plt.subplot(1, 2, i+1)
    t1_fit.plot(i, ax=ax)    
plt.show()

image

taalexander commented 4 years ago

IIRC, IBMQC is reporting T2 in the calibrations and $1/T2* = 1/T2 + 1/T2_phi$ where parts of T2_phi are echoed away by the Hahn/CPMG sequences.

SergioUnoxx commented 4 years ago

So you're saying the T2* in the definition I'm using is not the same that Qiskit uses?

SergioUnoxx commented 4 years ago

Ok, I solved what T2* is the code measuring thanks to this source code: https://qiskit.org/documentation/_modules/qiskit/ignis/characterization/coherence/fitters.html

Anyways, I got the answers I asked for. Thank you very much for your help! I'd like to ask a few more questions, but I guess I'll have to open up another issue and close this one.