<jupyter_start><jupyter_text>Qubit spectroscopy: CZ Ramsey measurement exampleSimilar to a $T_2^*$ Ramsey experiment, a CZ Ramsey experiment measures fringes resulting from induced Z rotations, which can result in non-unitary CZs. To rectify this non-unitarity, we determine the correction we need to apply to each qubit in the form of `RZ` rotations. If a CZ is perfectly unitary (or has been compensated for adequately with `RZ` pulses), a CZ Ramsey experiment should return 0 radians for each qubit. If, however, some correction is required, these angles will be non-zero. A sample QUIL program at one data point (specified by the equatorial Z rotation which maximizes excited state visibility when equal to the required `RZ` correction) between qubits 0 and 1 would look like     DECLARE ro BIT[1]    DECLARE theta REAL[1]    RX(pi/2) 0    CZ 0 1    RZ(theta) 0    RX(pi/2) 0    MEASURE 0 ro[0]     Setup<jupyter_code>from typing import Tuple

from matplotlib import pyplot as plt
import numpy as np

from pyquil import Program
from pyquil.api import get_qc, QuantumComputer
from forest.benchmarking.qubit_spectroscopy import *
#qc = get_qc('Aspen-1-15Q-A')
qc = get_qc('3q-noisy-qvm') # will run on a QVM, but not very meaningfully since there is no cross-talk.
graph = qc.qubit_topology()
edges = list(graph.edges())
edges
# if you are on the QPU you can look at `qc.device.specs`
# qc.device.specs<jupyter_output><empty_output><jupyter_text>Generate CZ Ramsey experiments<jupyter_code>import numpy as np
from numpy import pi
angles = np.linspace(0, 2*pi, 15)
edge = (0, 1)
# generate a set of len(angles) experiments for each measurement qubit on edge (0, 1).
edge_0_1_expts = [generate_cz_phase_ramsey_experiments(edge, measure_q, angles) for measure_q in edge]<jupyter_output><empty_output><jupyter_text>Acquire dataCollect our Ramsey raw data using `estimate_observables`.<jupyter_code>from forest.benchmarking.observable_estimation import estimate_observables
# acquire results for each measurement qubit on each edge.
results = []
for angle_expts in zip(*edge_0_1_expts):
    angle_results = []
    for meas_q_expt in angle_expts:
        angle_results += estimate_observables(qc, meas_q_expt, num_shots=500)
    results.append(angle_results)<jupyter_output><empty_output><jupyter_text>Analyze and plot**Use the results to produce estimates of Ramsey-acquired compensatory RZ phases for this edge**In the cell below we first extract the expectation and std_err for each measurement and store these results in lists separately for each qubit measured. For each qubit measured we then fit a sinusoid to the data from which we can determine the `offset` of the maximum excited state visibility, which tells us the effective imparted phase. Finally we plot the CZ Ramsey fringes for each qubit with respect to increasing applied contrast phase.<jupyter_code>from forest.benchmarking.plotting import plot_figure_for_fit

stats_by_qubit = get_stats_by_qubit(results)
for q, stats in stats_by_qubit.items():
    fit = fit_cz_phase_ramsey_results(angles, stats['expectation'], stats['std_err'])
    fig, axs = plot_figure_for_fit(fit, title=f'Q{q} Data and Fit', xlabel="RX angle [rad]",
                           ylabel=r"Pr($|1\rangle$)")
    frequency = fit.params['frequency'].value # ratio of actual angle over intended control angle
    amplitude = fit.params['amplitude'].value # (P(1 given 1) - P(1 given 0)) / 2
    baseline = fit.params['baseline'].value # amplitude + P(1 given 0)
    offset = fit.params['offset'].value # effective imparted phase on the qubit from the CZ, relative to the true qubit frequency<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07576586433260393, 'fraction_numerical': 0.011487964989059081, 'mean_word_length': 5.004926108374384, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '433440', 'license': 'Apache-2.0', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1072, 'n_words': 427, 'path': 'docs/examples/qubit_spectroscopy_cz_ramsey.ipynb', 'repo_name': 'stjordanis/forest-benchmarking', 'size': '3656'}