Skip to content

Commit

Permalink
Correct qbspec terminology (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegulshen authored and karalekas committed Sep 18, 2019
1 parent e4700cd commit 2f81ff0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
17 changes: 10 additions & 7 deletions docs/examples/qubit_spectroscopy_t2.ipynb

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions forest/benchmarking/analysis/fitting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from numpy import pi
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from lmfit import Model
Expand Down Expand Up @@ -80,23 +81,25 @@ def fit_decay_time_param_decay(x: np.ndarray, y: np.ndarray, weights: np.ndarray
def decaying_cosine(x: np.ndarray, amplitude: float, decay_time: float, offset: float,
baseline: float, frequency: float) -> np.ndarray:
"""
Calculate exponentially decaying sinusoid at a series of points.
Calculate exponentially decaying cosine at a series of points.
:param x: The independent variable with respect to which decay is calculated.
:param amplitude: The amplitude of the decaying sinusoid.
:param amplitude: The amplitude of the decaying cosine.
:param decay_time: The inverse decay rate - e.g. T2 - of the decay curve.
:param offset: The argument offset of the sinusoidal curve, o for sin(x - o)
:param baseline: The baseline of the sinusoid, e.g. b for sin(x) + b
:param frequency: The frequency of the sinusoid, e.g. f for sin(f x)
:return: The exponentially decaying sinusoid evaluated at the point(s) x
:param offset: The argument offset of the cosine, e.g. o for cos(x - o)
:param baseline: The baseline of the cosine, e.g. b for cos(x) + b
:param frequency: The frequency of the cosine, e.g. f for cos(2 pi f x). If decay_time is
indeed a time then the frequency has units of inverse time.
:return: The exponentially decaying cosine evaluated at the point(s) x
"""
return amplitude * np.exp(-1 * x / decay_time) * np.cos(frequency * (x - offset)) + baseline
return amplitude * np.exp(-1 * x / decay_time) * np.cos(2 * pi * frequency * x + offset) + \
baseline


def fit_decaying_cosine(x: np.ndarray, y: np.ndarray, weights: np.ndarray = None,
param_guesses: tuple = (.5, 10, 0.0, 0.5, 5)) -> ModelResult:
"""
Fit experimental data x, y to an exponentially decaying sinusoid.
Fit experimental data x, y to an exponentially decaying cosine.
:param x: The independent variable, e.g. depth or time
:param y: The dependent variable, e.g. probability of measuring 1
Expand All @@ -119,9 +122,9 @@ def shifted_cosine(x: np.ndarray, amplitude: float, offset: float, baseline: flo
:param x: The independent variable;
:param amplitude: The amplitude of the cosine.
:param offset: The argument offset of the sinusoidal curve, o for sin(x - o)
:param baseline: The baseline of the sinusoid, e.g. b for sin(x) + b
:param frequency: The frequency of the sinusoid, e.g. f for sin(f x)
:param offset: The argument offset of the cosine, e.g. o for cos(x - o)
:param baseline: The baseline of the cosine, e.g. b for cos(x) + b
:param frequency: The angular frequency, e.g. f for cos(f x)
:return: The sinusoidal response at the given phases(s).
"""
return amplitude * np.cos(frequency * x + offset) + baseline
Expand Down
7 changes: 4 additions & 3 deletions forest/benchmarking/qubit_spectroscopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def do_t1_or_t2(qc: QuantumComputer, qubits: Sequence[int], times: Sequence[floa
# T2 star and T2 echo functions
# ==================================================================================================
def generate_t2_star_experiments(qubits: Sequence[int], times: Sequence[float],
detuning: float = 5e6) -> List[ObservablesExperiment]:
detuning: float = 1e6) -> List[ObservablesExperiment]:
"""
Return ObservablesExperiments containing programs which constitute a T2 star experiment to
measure the T2 star coherence decay time for each qubit in qubits.
Expand Down Expand Up @@ -233,7 +233,7 @@ def generate_t2_star_experiments(qubits: Sequence[int], times: Sequence[float],


def generate_t2_echo_experiments(qubits: Sequence[int], times: Sequence[float],
detuning: float = 5e6) -> List[ObservablesExperiment]:
detuning: float = 1e6) -> List[ObservablesExperiment]:
"""
Return ObservablesExperiments containing programs which constitute a T2 echo experiment to
measure the T2 echo coherence decay time.
Expand All @@ -260,6 +260,7 @@ def generate_t2_echo_experiments(qubits: Sequence[int], times: Sequence[float],
expts = []
for t in times:
half_time = round(t/2, 7) # enforce 100ns boundaries
t = round(t, 7)
program = Program()
settings = []
for q in qubits:
Expand All @@ -276,7 +277,7 @@ def generate_t2_echo_experiments(qubits: Sequence[int], times: Sequence[float],


def fit_t2_results(times: Sequence[float], y_expectations: Sequence[float],
y_std_errs: Sequence[float] = None, detuning: float = 5e6,
y_std_errs: Sequence[float] = None, detuning: float = 1e6,
param_guesses: tuple = None) -> ModelResult:
"""
Wrapper for fitting the results of a ObservablesExperiment; simply extracts key parameters
Expand Down
16 changes: 7 additions & 9 deletions forest/benchmarking/tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

def test_shifted_cosine():
actual_rabi_period = 2.15 * np.pi # not quite 2pi
mock_rabi = {'rabi_per': actual_rabi_period}
thetas = np.linspace(0, 2 * np.pi, 30)
data = np.asarray([- 0.5 * np.cos(theta * 2 * np.pi / mock_rabi['rabi_per']) + 0.5
data = np.asarray([- 0.5 * np.cos(theta * 2 * np.pi / actual_rabi_period) + 0.5
for theta in thetas])

fit = fit_shifted_cosine(thetas, data)

assert np.isclose(2 * np.pi / fit.params['frequency'], mock_rabi['rabi_per'])
assert np.isclose(fit.params['frequency'], 2 * np.pi / actual_rabi_period)


def test_fit_decay_time_param_decay():
Expand All @@ -29,17 +28,16 @@ def test_fit_decay_time_param_decay():
assert np.isclose(fit.params['decay_time'], mock_t1['T1'])


def test_decaying_sinusoid():
num_points_sampled = 50
def test_decaying_cosine():
num_points_sampled = 1201
true_t2 = 15 # us
qubit_detuning = 2.5 # MHZ
qubit_detuning = 1 # MHZ

mock_t2 = {'T2': true_t2, 'qubit_detuning': qubit_detuning, 'num_points': num_points_sampled}

times = np.linspace(0, 2 * mock_t2['T2'], mock_t2['num_points'])
times = np.linspace(0, 4 * mock_t2['T2'], mock_t2['num_points'])
data = np.asarray([0.5 * np.exp(-1 * t / mock_t2['T2']) *
np.sin(mock_t2['qubit_detuning'] * t) + 0.5 for t in times])

np.cos(2 * np.pi * mock_t2['qubit_detuning'] * t) + 0.5 for t in times])
fit = fit_decaying_cosine(times, data)

assert np.isclose(fit.params['decay_time'], mock_t2['T2'])
Expand Down

0 comments on commit 2f81ff0

Please sign in to comment.