# T2 Ramsey Characterization¶

The purpose of the \(T_2\)Ramsey experiment is to determine two of
the qubit’s properties: *Ramsey* or *detuning frequency* and
\(T_2^\ast\). The rough frequency of the qubit was already
determined previously. The control pulses are based on this frequency.

In this experiment, we would like to get a more precise estimate of the
qubit’s frequency. The difference between the frequency used for the
control rotation pulses, and the precise frequency is called the
*detuning frequency*. This part of the experiment is called a *Ramsey
Experiment*. \(T_2^\ast\) represents the rate of decay toward a
mixed state, when the qubit is initialized to the
\(\left|1\right\rangle\) state.

Since the detuning frequency is relatively small, we add a phase gate to
the circuit to enable better measurement. The actual frequency measured
is the sum of the detuning frequency and the user induced *oscillation
frequency* (`osc_freq`

parameter).

```
import numpy as np
import qiskit
from qiskit_experiments.library import T2Ramsey
```

The circuit used for the experiment comprises the following:

```
1. Hadamard gate
2. delay
3. RZ gate that rotates the qubit in the x-y plane
4. Hadamard gate
5. measurement
```

The user provides as input a series of delays (in seconds) and the oscillation frequency (in Hz). During the delay, we expect the qubit to precess about the z-axis. If the p gate and the precession offset each other perfectly, then the qubit will arrive at the \(\left|0\right\rangle\) state (after the second Hadamard gate). By varying the extension of the delays, we get a series of oscillations of the qubit state between the \(\left|0\right\rangle\) and \(\left|1\right\rangle\) states. We can draw the graph of the resulting function, and can analytically extract the desired values.

```
qubit = 0
# set the desired delays
delays = list(np.arange(1e-6, 50e-6, 2e-6))
```

```
# Create a T2Ramsey experiment. Print the first circuit as an example
exp1 = T2Ramsey(qubit, delays, osc_freq=1e5)
print(exp1.circuits()[0])
```

```
┌────┐┌─────────────────┐┌─────────┐ ░ ┌────┐ ░ ┌─┐
q: ┤ √X ├┤ Delay(1e-06[s]) ├┤ Rz(π/5) ├─░─┤ √X ├─░─┤M├
└────┘└─────────────────┘└─────────┘ ░ └────┘ ░ └╥┘
c: 1/═════════════════════════════════════════════════╩═
0
```

We run the experiment on a simulated backend using Qiskit Aer with a pure T1/T2 relaxation noise model.

```
# A T1 simulator
from qiskit.providers.fake_provider import FakeVigo
from qiskit.providers.aer import AerSimulator
from qiskit.providers.aer.noise import NoiseModel
# Create a pure relaxation noise model for AerSimulator
noise_model = NoiseModel.from_backend(
FakeVigo(), thermal_relaxation=True, gate_error=False, readout_error=False
)
# Create a fake backend simulator
backend = AerSimulator.from_backend(FakeVigo(), noise_model=noise_model)
```

The resulting graph will have the form:
\(f(t) = a^{-t/T_2*} \cdot \cos(2 \pi f t + \phi) + b\) where *t* is
the delay, \(T_2^\ast\) is the decay factor, and *f* is the detuning
frequency.

```
# Set scheduling method so circuit is scheduled for delay noise simulation
exp1.set_transpile_options(scheduling_method='asap')
# Run experiment
expdata1 = exp1.run(backend=backend, shots=2000, seed_simulator=101)
expdata1.block_for_results() # Wait for job/analysis to finish.
# Display the figure
display(expdata1.figure(0))
```

```
# Print results
for result in expdata1.analysis_results():
print(result)
```

```
AnalysisResult
- name: @Parameters_T2RamseyAnalysis
- value: CurveFitResult:
- fitting method: least_squares
- number of sub-models: 1
* F_cos_decay(x) = amp * exp(-x / tau) * cos(2 * pi * freq * x + phi) + base
- success: True
- number of function evals: 13
- degree of freedom: 20
- chi-square: 19.727984297150364
- reduced chi-square: 0.9863992148575182
- Akaike info crit.: 4.0790581396457295
- Bayesian info crit.: 10.173437263986733
- init params:
* amp = 0.5
* tau = 1.2542773918739039e-05
* freq = 80000.0000000005
* phi = -1.5707963267948966
* base = 0.5218890554722638
- fit params:
* amp = 0.483693641870265 ± 0.008712967658575342
* tau = 1.7680488197010216e-05 ± 5.413428886962965e-07
* freq = 99654.76706678561 ± 297.66472541489793
* phi = 0.012228798382429332 ± 0.022015115194344893
* base = 0.5018645927174161 ± 0.0021644273733284827
- correlations:
* (freq, phi) = -0.7328437437053252
* (amp, tau) = -0.7118909156617429
* (amp, freq) = -0.28069292711866495
* (tau, phi) = -0.2654103124687229
* (freq, base) = -0.19362041713010686
* (tau, base) = -0.0011829695966609076
* (amp, base) = 0.04438400915614297
* (tau, freq) = 0.17870993155878892
* (phi, base) = 0.24250195352715564
* (amp, phi) = 0.401378566878557
- quality: good
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: Frequency
- value: (9.965+/-0.030)e+04
- χ²: 0.9863992148575182
- quality: good
- extra: <1 items>
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: T2star
- value: (1.77+/-0.05)e-05
- χ²: 0.9863992148575182
- quality: good
- extra: <1 items>
- device_components: ['Q0']
- verified: False
```

## Providing initial user estimates¶

The user can provide initial estimates for the parameters to help the
analysis process. Because the curve is expected to decay toward
\(0.5\), the natural choice for parameters \(A\) and \(B\)
is \(0.5\). Varying the value of \(\phi\) will shift the graph
along the x-axis. Since this is not of interest to us, we can safely
initialize \(\phi\) to 0. In this experiment, `t2ramsey`

and `f`

are the parameters of interest. Good estimates for them are values
computed in previous experiments on this qubit or a similar values
computed for other qubits.

```
user_p0={
"A": 0.5,
"T2star": 20e-6,
"f": 110000,
"phi": 0,
"B": 0.5
}
exp_with_p0 = T2Ramsey(qubit, delays, osc_freq=1e5)
exp_with_p0.analysis.set_options(p0=user_p0)
exp_with_p0.set_transpile_options(scheduling_method='asap')
expdata_with_p0 = exp_with_p0.run(backend=backend, shots=2000, seed_simulator=101)
expdata_with_p0.block_for_results()
# Display fit figure
display(expdata_with_p0.figure(0))
```

```
# Print results
for result in expdata_with_p0.analysis_results():
print(result)
```

```
AnalysisResult
- name: @Parameters_T2RamseyAnalysis
- value: CurveFitResult:
- fitting method: least_squares
- number of sub-models: 1
* F_cos_decay(x) = amp * exp(-x / tau) * cos(2 * pi * freq * x + phi) + base
- success: True
- number of function evals: 7
- degree of freedom: 20
- chi-square: 19.727984299452505
- reduced chi-square: 0.9863992149726253
- Akaike info crit.: 4.079058142563086
- Bayesian info crit.: 10.173437266904088
- init params:
* amp = 0.5
* tau = 1.2542773918739039e-05
* freq = 100000.0000000005
* phi = 0.0
* base = 0.5218890554722638
- fit params:
* amp = 0.4836937664489146 ± 0.008712971534857684
* tau = 1.7680481279302734e-05 ± 5.413426702364316e-07
* freq = 99654.75586938199 ± 297.6647536837113
* phi = 0.012229423006551327 ± 0.022015108515465436
* base = 0.5018646081414204 ± 0.0021644274029482733
- correlations:
* (freq, phi) = -0.7328436584512659
* (amp, tau) = -0.7118910036757513
* (amp, freq) = -0.2806930188002508
* (tau, phi) = -0.2654104633128858
* (freq, base) = -0.19362042503134277
* (tau, base) = -0.001182975284761959
* (amp, base) = 0.04438410776725264
* (tau, freq) = 0.17870999922891095
* (phi, base) = 0.24250200111665654
* (amp, phi) = 0.4013787713098917
- quality: good
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: Frequency
- value: (9.965+/-0.030)e+04
- χ²: 0.9863992149726253
- quality: good
- extra: <1 items>
- device_components: ['Q0']
- verified: False
AnalysisResult
- name: T2star
- value: (1.77+/-0.05)e-05
- χ²: 0.9863992149726253
- quality: good
- extra: <1 items>
- device_components: ['Q0']
- verified: False
```

```
import qiskit.tools.jupyter
%qiskit_copyright
```

### This code is a part of Qiskit

© Copyright IBM 2017, 2022.

This code is licensed under the Apache License, Version 2.0. You may

obtain a copy of this license in the LICENSE.txt file in the root directory

of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.

Any modifications or derivative works of this code must retain this

copyright notice, and modified files need to carry a notice indicating

that they have been altered from the originals.