Skip to content

Commit 283ead9

Browse files
committed
Review comments
1 parent 0cf2cad commit 283ead9

File tree

3 files changed

+97
-41
lines changed

3 files changed

+97
-41
lines changed

cirq/experiments/xeb_fitting.py

+44-28
Original file line numberDiff line numberDiff line change
@@ -324,25 +324,17 @@ def _mean_infidelity(angles):
324324
)
325325

326326

327+
@dataclass(frozen=True)
327328
class _CharacterizePhasedFsimParametersWithXebClosure:
328329
"""A closure object to wrap `characterize_phased_fsim_parameters_with_xeb` for use in
329330
multiprocessing."""
330331

331-
def __init__(
332-
self,
333-
parameterized_circuits: List['cirq.Circuit'],
334-
cycle_depths: Sequence[int],
335-
phased_fsim_options: XEBPhasedFSimCharacterizationOptions,
336-
initial_simplex_step_size: float = 0.1,
337-
xatol: float = 1e-3,
338-
fatol: float = 1e-3,
339-
):
340-
self.parameterized_circuits = parameterized_circuits
341-
self.cycle_depths = cycle_depths
342-
self.phased_fsim_options = phased_fsim_options
343-
self.initial_simplex_step_size = initial_simplex_step_size
344-
self.xatol = xatol
345-
self.fatol = fatol
332+
parameterized_circuits: List['cirq.Circuit']
333+
cycle_depths: Sequence[int]
334+
phased_fsim_options: XEBPhasedFSimCharacterizationOptions
335+
initial_simplex_step_size: float = 0.1
336+
xatol: float = 1e-3
337+
fatol: float = 1e-3
346338

347339
def __call__(self, sampled_df) -> XEBCharacterizationResult:
348340
return characterize_phased_fsim_parameters_with_xeb(
@@ -419,13 +411,36 @@ def characterize_phased_fsim_parameters_with_xeb_by_pair(
419411
)
420412

421413

422-
def exponential_decay(cycle_depths: np.ndarray, A: float, layer_fid: float) -> np.ndarray:
423-
"""An exponential decay for fitting."""
424-
return A * layer_fid ** cycle_depths
414+
def exponential_decay(cycle_depths: np.ndarray, a: float, layer_fid: float) -> np.ndarray:
415+
"""An exponential decay for fitting.
416+
417+
This computes `a * layer_fid**cycle_depths`
418+
419+
Args:
420+
cycle_depths: The various depths at which fidelity was estimated. This is the independent
421+
variable in the exponential function.
422+
a: A scale parameter in the exponential function.
423+
layer_fid: The base of the exponent in the exponential function.
424+
"""
425+
return a * layer_fid ** cycle_depths
425426

426427

427428
def _fit_exponential_decay(cycle_depths: np.ndarray, fidelities: np.ndarray) -> Tuple[float, float]:
428-
"""Fit an exponential model fidelities = A * layer_fid**x using nonlinear least squares."""
429+
"""Fit an exponential model fidelity = a * layer_fid**x using nonlinear least squares.
430+
431+
This uses `exponential_decay` as the function to fit with parameters `a` and `layer_fid`.
432+
433+
Args:
434+
cycle_depths: The various depths at which fidelity was estimated. Each element is `x`
435+
in the fit expression.
436+
fidelities: The estimated fidelities for each cycle depth. Each element is `fidelity`
437+
in the fit expression.
438+
439+
Returns:
440+
a: The first fit parameter that scales the exponential function, perhaps accounting for
441+
state prep and measurement (SPAM) error.
442+
layer_fid: The second fit parameters which serves as the base of the exponential.
443+
"""
429444
cycle_depths = np.asarray(cycle_depths)
430445
fidelities = np.asarray(fidelities)
431446

@@ -435,12 +450,12 @@ def _fit_exponential_decay(cycle_depths: np.ndarray, fidelities: np.ndarray) ->
435450
log_fidelities = np.log(fidelities[positives])
436451
slope, intercept, _, _, _ = scipy.stats.linregress(cycle_depths_pos, log_fidelities)
437452
layer_fid_0 = np.clip(np.exp(slope), 0, 1)
438-
A_0 = np.clip(np.exp(intercept), 0, 1)
453+
a_0 = np.clip(np.exp(intercept), 0, 1)
439454

440-
(A, layer_fid), _ = scipy.optimize.curve_fit(
441-
exponential_decay, cycle_depths, fidelities, p0=(A_0, layer_fid_0), bounds=((0, 0), (1, 1))
455+
(a, layer_fid), _ = scipy.optimize.curve_fit(
456+
exponential_decay, cycle_depths, fidelities, p0=(a_0, layer_fid_0), bounds=((0, 0), (1, 1))
442457
)
443-
return A, layer_fid
458+
return a, layer_fid
444459

445460

446461
def _one_unique(df, name, default):
@@ -463,16 +478,16 @@ def fit_exponential_decays(fidelities_df: pd.DataFrame) -> pd.DataFrame:
463478
464479
Returns:
465480
A new, aggregated dataframe with index given by (pair, layer_i, pair_i); columns
466-
for the fit parameters "A" and "layer_fid"; and nested "cycles_depths" and "fidelities"
481+
for the fit parameters "a" and "layer_fid"; and nested "cycles_depths" and "fidelities"
467482
lists (now grouped by pair).
468483
"""
469484
records = []
470485
for pair in fidelities_df['pair'].unique():
471486
f1 = fidelities_df[fidelities_df['pair'] == pair]
472-
A, layer_fid = _fit_exponential_decay(f1['cycle_depth'], f1['fidelity'])
487+
a, layer_fid = _fit_exponential_decay(f1['cycle_depth'], f1['fidelity'])
473488
record = {
474489
'pair': pair,
475-
'A': A,
490+
'a': a,
476491
'layer_fid': layer_fid,
477492
'cycle_depths': f1['cycle_depth'].values,
478493
'fidelities': f1['fidelity'].values,
@@ -506,8 +521,9 @@ def before_and_after_characterization(
506521
joined_df['characterized_angles'] = [
507522
characterization_result.final_params[pair] for pair, _, _ in joined_df.index
508523
]
509-
angle_names = list(list(characterization_result.final_params.values())[0].keys())
510-
for angle_name in angle_names:
524+
# Take any `final_params` (for any pair). We just need the angle names.
525+
fp, *_ = characterization_result.final_params.values()
526+
for angle_name in fp.keys():
511527
joined_df[angle_name] = [
512528
characterization_result.final_params[pair][angle_name] for pair, _, _ in joined_df.index
513529
]

docs/characterization/isolated_xeb.ipynb

+26-6
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,33 @@
240240
},
241241
"outputs": [],
242242
"source": [
243-
"from cirq.experiments.xeb_fitting import \\\n",
244-
" parameterize_phased_fsim_circuit, characterize_phased_fsim_parameters_with_xeb, SqrtISwapXEBOptions\n",
243+
"from cirq.experiments.xeb_fitting import (\n",
244+
" parameterize_phased_fsim_circuit, \n",
245+
" characterize_phased_fsim_parameters_with_xeb, \n",
246+
" SqrtISwapXEBOptions,\n",
247+
")\n",
245248
"\n",
246-
"options = SqrtISwapXEBOptions()\n",
249+
"# Set which angles we want to characterize (all)\n",
250+
"options = SqrtISwapXEBOptions(\n",
251+
" characterize_theta = True,\n",
252+
" characterize_zeta = True,\n",
253+
" characterize_chi = True,\n",
254+
" characterize_gamma = True,\n",
255+
" characterize_phi = True\n",
256+
")\n",
257+
"# Parameterize the sqrt(iswap)s in our circuit library\n",
247258
"pcircuits = [parameterize_phased_fsim_circuit(circuit, options) for circuit in circuits]\n",
259+
"\n",
260+
"# Run the characterization loop\n",
248261
"characterization_result = characterize_phased_fsim_parameters_with_xeb(\n",
249-
" sampled_df, pcircuits, cycle_depths, options, pool=pool, fatol=5e-3, xatol=5e-3)"
262+
" sampled_df,\n",
263+
" pcircuits,\n",
264+
" cycle_depths,\n",
265+
" options,\n",
266+
" pool=pool,\n",
267+
" # ease tolerance so it converges faster:\n",
268+
" fatol=5e-3, xatol=5e-3\n",
269+
")"
250270
]
251271
},
252272
{
@@ -300,8 +320,8 @@
300320
" plt.plot(row['cycle_depths_c'], row['fidelities_c'], 'o', color='blue')\n",
301321
"\n",
302322
" xx = np.linspace(0, np.max(row['cycle_depths_0']))\n",
303-
" plt.plot(xx, exponential_decay(xx, A=row['A_0'], layer_fid=row['layer_fid_0']), color='red')\n",
304-
" plt.plot(xx, exponential_decay(xx, A=row['A_c'], layer_fid=row['layer_fid_c']), color='blue')\n",
323+
" plt.plot(xx, exponential_decay(xx, a=row['a_0'], layer_fid=row['layer_fid_0']), color='red')\n",
324+
" plt.plot(xx, exponential_decay(xx, a=row['a_c'], layer_fid=row['layer_fid_c']), color='blue')\n",
305325
" \n",
306326
" plt.show()"
307327
]

docs/characterization/parallel_xeb.ipynb

+27-7
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
" plt.axhline(1, color='grey', ls='--')\n",
278278
" plt.plot(record['cycle_depths'], record['fidelities'], 'o')\n",
279279
" xx = np.linspace(0, np.max(record['cycle_depths']))\n",
280-
" plt.plot(xx, exponential_decay(xx, A=record['A'], layer_fid=record['layer_fid']))\n",
280+
" plt.plot(xx, exponential_decay(xx, a=record['a'], layer_fid=record['layer_fid']))\n",
281281
" plt.show()"
282282
]
283283
},
@@ -343,13 +343,33 @@
343343
},
344344
"outputs": [],
345345
"source": [
346-
"from cirq.experiments.xeb_fitting import \\\n",
347-
" parameterize_phased_fsim_circuit, characterize_phased_fsim_parameters_with_xeb_by_pair, SqrtISwapXEBOptions\n",
346+
"from cirq.experiments.xeb_fitting import (\n",
347+
" parameterize_phased_fsim_circuit, \n",
348+
" characterize_phased_fsim_parameters_with_xeb_by_pair, \n",
349+
" SqrtISwapXEBOptions,\n",
350+
")\n",
348351
"\n",
349-
"options = SqrtISwapXEBOptions()\n",
352+
"# Set which angles we want to characterize (all)\n",
353+
"options = SqrtISwapXEBOptions(\n",
354+
" characterize_theta = True,\n",
355+
" characterize_zeta = True,\n",
356+
" characterize_chi = True,\n",
357+
" characterize_gamma = True,\n",
358+
" characterize_phi = True\n",
359+
")\n",
360+
"# Parameterize the sqrt(iswap)s in our circuit library\n",
350361
"pcircuits = [parameterize_phased_fsim_circuit(circuit, options) for circuit in circuit_library]\n",
362+
"\n",
363+
"# Run the characterization loop\n",
351364
"characterization_result = characterize_phased_fsim_parameters_with_xeb_by_pair(\n",
352-
" sampled_df, pcircuits, cycle_depths, options, pool=pool, fatol=5e-3, xatol=5e-3)"
365+
" sampled_df,\n",
366+
" pcircuits,\n",
367+
" cycle_depths,\n",
368+
" options,\n",
369+
" pool=pool,\n",
370+
" # ease tolerance so it converges faster:\n",
371+
" fatol=5e-3, xatol=5e-3\n",
372+
")"
353373
]
354374
},
355375
{
@@ -401,9 +421,9 @@
401421
" plt.plot(row['cycle_depths_c'], row['fidelities_c'], 'o', color='blue')\n",
402422
"\n",
403423
" xx = np.linspace(0, np.max(row['cycle_depths_0']))\n",
404-
" plt.plot(xx, exponential_decay(xx, A=row['A_0'], layer_fid=row['layer_fid_0']), \n",
424+
" plt.plot(xx, exponential_decay(xx, a=row['a_0'], layer_fid=row['layer_fid_0']), \n",
405425
" color='red', label=f'f_0 = {row[\"layer_fid_0\"]:.3f}')\n",
406-
" plt.plot(xx, exponential_decay(xx, A=row['A_c'], layer_fid=row['layer_fid_c']),\n",
426+
" plt.plot(xx, exponential_decay(xx, a=row['a_c'], layer_fid=row['layer_fid_c']),\n",
407427
" color='blue', label=f'f_c = {row[\"layer_fid_c\"]:.3f}')\n",
408428
" \n",
409429
" plt.xlabel('Cycle Depth')\n",

0 commit comments

Comments
 (0)