<commit_before># Copyright 2017 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0

from QGL import *
from QGL import config as QGLconfig
# from QGL.BasicSequences.helpers import create_cal_seqs, time_descriptor, cal_descriptor
import auspex.config as config
from copy import copy
import os
import json

from auspex.log import logger
from auspex.exp_factory import QubitExpFactory, QubitExperiment
from auspex.analysis.io import load_from_HDF5
from auspex.parameter import FloatParameter
from auspex.filters.plot import ManualPlotter
from auspex.filters.singleshot import SingleShotMeasurement
from auspex.analysis.fits import *
from auspex.analysis.helpers import normalize_data

class SingleShotFidelityExperiment(QubitExperiment):
    """Experiment to measure single-shot measurement fidelity of a qubit."""

    def __init__(self, qubit_names, num_shots=10000, expname=None, meta_file=None, save_data=False, optimize=False, stream_type = 'Raw'):
        """Create a single shot fidelity measurement experiment. Assumes that there is a single shot measurement
        filter in the filter pipeline.
        Arguments:
            qubit_names: Names of the qubits to be measured. (str)
            num_shots: Total number of 0 and 1 measurements used to reconstruct fidelity histograms (int)
            expname: Experiment name for data saving.
            meta_file: Meta file for defining custom single-shot fidelity experiment.
            save_data: If true, will save the raw or demodulated data.
            optimize: if true, will set the swept parameters to their optimum values"""

        super(SingleShotFidelityExperiment, self).__init__()
        self.qubit_names = qubit_names if isinstance(qubit_names, list) else [qubit_names]
        self.qubit     = [QubitFactory(qubit_name) for qubit_name in qubit_names] if isinstance(qubit_names, list) else QubitFactory(qubit_names)

        self.settings = config.yaml_load(config.configFile)
        self.save_data = save_data
        self.calibration = True
        self.optimize = optimize
        self.name = expname
        self.cw_mode = False
        self.repeats = num_shots
        self.ss_stream_type = stream_type

        if meta_file is None:
            meta_file = SingleShot(self.qubit)

        self._squash_round_robins()

        QubitExpFactory.load_meta_info(self, meta_file)
        QubitExpFactory.load_instruments(self)
        QubitExpFactory.load_qubits(self)
        QubitExpFactory.load_filters(self)
        if 'sweeps' in self.settings:
            QubitExpFactory.load_parameter_sweeps(experiment)
        self.ssf = self.find_single_shot_filter()
        self.leave_plot_server_open = True

    def run_sweeps(self):
        #For now, only update histograms if we don't have a parameter sweep.
        if not self.sweeper.axes:
            self.init_plots()
            self.add_manual_plotter(self.re_plot)
            self.add_manual_plotter(self.im_plot)
        else:
            if any([x.save_kernel.value for x in self.filters.values() if type(x) is SingleShotMeasurement]):
                logger.warning("Kernel saving is not supported if you have parameter sweeps!")

        super(SingleShotFidelityExperiment, self).run_sweeps()

        if not self.sweeper.axes:
            self._update_histogram_plots()

        if self.plotters:
            self.plot_server.stop()

        if self.sweeper.axes and self.optimize:
            #set sweep parameters to the values that maximize fidelity
            for buff in self.buffers: #TODO: select the buffers whose sources are singleshot filters
                dataset, descriptor = buff.get_data(), buff.get_descriptor()
                for k, axis in enumerate(self.sweeper.axes):
                    opt_ind = np.argmax(dataset['Data'], k)
                    instr_tree = axis.parameter.instr_tree
                    param_key = self.settings['instruments']
                    for key in instr_tree[:-1]:
                        #if key not in param_key.keys():
                        #    logger.warning("Sweep parameter not in instrument settings. Added to the sweep")
                        #param_key = param_key.setdefault(key, {})
                        param_key = param_key[key]
                    param_key[instr_tree[-1]] = axis.points[opt_ind]
                config.yaml_dump(self.settings, config.configFile)
    def _update_histogram_plots(self):
        pdf_data = self.get_results()
        self.re_plot.set_data("Ground", pdf_data["I Bins"], pdf_data["Ground I PDF"])
        self.re_plot.set_data("Ground Gaussian Fit", pdf_data["I Bins"], pdf_data["Ground I Gaussian PDF"])
        self.re_plot.set_data("Excited", pdf_data["I Bins"], pdf_data["Excited I PDF"])
        self.re_plot.set_data("Excited Gaussian Fit", pdf_data["I Bins"], pdf_data["Excited I Gaussian PDF"])
        self.im_plot.set_data("Ground", pdf_data["Q Bins"], pdf_data["Ground Q PDF"])
        self.im_plot.set_data("Ground Gaussian Fit", pdf_data["Q Bins"], pdf_data["Ground Q Gaussian PDF"])
        self.im_plot.set_data("Excited", pdf_data["Q Bins"], pdf_data["Excited Q PDF"])
        self.im_plot.set_data("Excited Gaussian Fit", pdf_data["Q Bins"], pdf_data["Excited Q Gaussian PDF"])

    def init_plots(self):
        self.re_plot = ManualPlotter("Fidelity - Real", x_label='Bins', y_label='Real Quadrature')
        self.im_plot = ManualPlotter("Fidelity - Imag", x_label='Bins', y_label='Imag Quadrature')
        self.re_plot.add_trace("Excited", matplotlib_kwargs={'color': 'r', 'linestyle': '-', 'linewidth': 2})
        self.re_plot.add_trace("Excited Gaussian Fit", matplotlib_kwargs={'color': 'r', 'linestyle': '--', 'linewidth': 2})
        self.re_plot.add_trace("Ground", matplotlib_kwargs={'color': 'b', 'linestyle': '-', 'linewidth': 2})
        self.re_plot.add_trace("Ground Gaussian Fit", matplotlib_kwargs={'color': 'b', 'linestyle': '--', 'linewidth': 2})
        self.im_plot.add_trace("Excited", matplotlib_kwargs={'color': 'r', 'linestyle': '-', 'linewidth': 2})
        self.im_plot.add_trace("Excited Gaussian Fit", matplotlib_kwargs={'color': 'r', 'linestyle': '--', 'linewidth': 2})
        self.im_plot.add_trace("Ground", matplotlib_kwargs={'color': 'b', 'linestyle': '-', 'linewidth': 2})
        self.im_plot.add_trace("Ground Gaussian Fit", matplotlib_kwargs={'color': 'b', 'linestyle': '--', 'linewidth': 2})
<commit_msg>Conver to float for yaml<commit_after># Copyright 2017 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0

from QGL import *
from QGL import config as QGLconfig
# from QGL.BasicSequences.helpers import create_cal_seqs, time_descriptor, cal_descriptor
import auspex.config as config
from copy import copy
import os
import json

from auspex.log import logger
from auspex.exp_factory import QubitExpFactory, QubitExperiment
from auspex.analysis.io import load_from_HDF5
from auspex.parameter import FloatParameter
from auspex.filters.plot import ManualPlotter
from auspex.filters.singleshot import SingleShotMeasurement
from auspex.analysis.fits import *
from auspex.analysis.helpers import normalize_data

class SingleShotFidelityExperiment(QubitExperiment):
    """Experiment to measure single-shot measurement fidelity of a qubit."""

    def __init__(self, qubit_names, num_shots=10000, expname=None, meta_file=None, save_data=False, optimize=False, stream_type = 'Raw'):
        """Create a single shot fidelity measurement experiment. Assumes that there is a single shot measurement
        filter in the filter pipeline.
        Arguments:
            qubit_names: Names of the qubits to be measured. (str)
            num_shots: Total number of 0 and 1 measurements used to reconstruct fidelity histograms (int)
            expname: Experiment name for data saving.
            meta_file: Meta file for defining custom single-shot fidelity experiment.
            save_data: If true, will save the raw or demodulated data.
            optimize: if true, will set the swept parameters to their optimum values"""

        super(SingleShotFidelityExperiment, self).__init__()
        self.qubit_names = qubit_names if isinstance(qubit_names, list) else [qubit_names]
        self.qubit     = [QubitFactory(qubit_name) for qubit_name in qubit_names] if isinstance(qubit_names, list) else QubitFactory(qubit_names)

        self.settings = config.yaml_load(config.configFile)
        self.save_data = save_data
        self.calibration = True
        self.optimize = optimize
        self.name = expname
        self.cw_mode = False
        self.repeats = num_shots
        self.ss_stream_type = stream_type

        if meta_file is None:
            meta_file = SingleShot(self.qubit)

        self._squash_round_robins()

        QubitExpFactory.load_meta_info(self, meta_file)
        QubitExpFactory.load_instruments(self)
        QubitExpFactory.load_qubits(self)
        QubitExpFactory.load_filters(self)
        if 'sweeps' in self.settings:
            QubitExpFactory.load_parameter_sweeps(experiment)
        self.ssf = self.find_single_shot_filter()
        self.leave_plot_server_open = True

    def run_sweeps(self):
        #For now, only update histograms if we don't have a parameter sweep.
        if not self.sweeper.axes:
            self.init_plots()
            self.add_manual_plotter(self.re_plot)
            self.add_manual_plotter(self.im_plot)
        else:
            if any([x.save_kernel.value for x in self.filters.values() if type(x) is SingleShotMeasurement]):
                logger.warning("Kernel saving is not supported if you have parameter sweeps!")

        super(SingleShotFidelityExperiment, self).run_sweeps()

        if not self.sweeper.axes:
            self._update_histogram_plots()

        if self.plotters:
            self.plot_server.stop()

        if self.sweeper.axes and self.optimize:
            #set sweep parameters to the values that maximize fidelity
            for buff in self.buffers: #TODO: select the buffers whose sources are singleshot filters
                dataset, descriptor = buff.get_data(), buff.get_descriptor()
                for k, axis in enumerate(self.sweeper.axes):
                    opt_ind = np.argmax(dataset['Data'], k)
                    instr_tree = axis.parameter.instr_tree
                    param_key = self.settings['instruments']
                    for key in instr_tree[:-1]:
                        #if key not in param_key.keys():
                        #    logger.warning("Sweep parameter not in instrument settings. Added to the sweep")
                        #param_key = param_key.setdefault(key, {})
                        param_key = param_key[key]
                    param_key[instr_tree[-1]] = float(axis.points[opt_ind])
                config.yaml_dump(self.settings, config.configFile)
    def _update_histogram_plots(self):
        pdf_data = self.get_results()
        self.re_plot.set_data("Ground", pdf_data["I Bins"], pdf_data["Ground I PDF"])
        self.re_plot.set_data("Ground Gaussian Fit", pdf_data["I Bins"], pdf_data["Ground I Gaussian PDF"])
        self.re_plot.set_data("Excited", pdf_data["I Bins"], pdf_data["Excited I PDF"])
        self.re_plot.set_data("Excited Gaussian Fit", pdf_data["I Bins"], pdf_data["Excited I Gaussian PDF"])
        self.im_plot.set_data("Ground", pdf_data["Q Bins"], pdf_data["Ground Q PDF"])
        self.im_plot.set_data("Ground Gaussian Fit", pdf_data["Q Bins"], pdf_data["Ground Q Gaussian PDF"])
        self.im_plot.set_data("Excited", pdf_data["Q Bins"], pdf_data["Excited Q PDF"])
        self.im_plot.set_data("Excited Gaussian Fit", pdf_data["Q Bins"], pdf_data["Excited Q Gaussian PDF"])

    def init_plots(self):
        self.re_plot = ManualPlotter("Fidelity - Real", x_label='Bins', y_label='Real Quadrature')
        self.im_plot = ManualPlotter("Fidelity - Imag", x_label='Bins', y_label='Imag Quadrature')
        self.re_plot.add_trace("Excited", matplotlib_kwargs={'color': 'r', 'linestyle': '-', 'linewidth': 2})
        self.re_plot.add_trace("Excited Gaussian Fit", matplotlib_kwargs={'color': 'r', 'linestyle': '--', 'linewidth': 2})
        self.re_plot.add_trace("Ground", matplotlib_kwargs={'color': 'b', 'linestyle': '-', 'linewidth': 2})
        self.re_plot.add_trace("Ground Gaussian Fit", matplotlib_kwargs={'color': 'b', 'linestyle': '--', 'linewidth': 2})
        self.im_plot.add_trace("Excited", matplotlib_kwargs={'color': 'r', 'linestyle': '-', 'linewidth': 2})
        self.im_plot.add_trace("Excited Gaussian Fit", matplotlib_kwargs={'color': 'r', 'linestyle': '--', 'linewidth': 2})
        self.im_plot.add_trace("Ground", matplotlib_kwargs={'color': 'b', 'linestyle': '-', 'linewidth': 2})
        self.im_plot.add_trace("Ground Gaussian Fit", matplotlib_kwargs={'color': 'b', 'linestyle': '--', 'linewidth': 2})

{'fraction_non_alphanumeric': 0.1075105996365839, 'fraction_numerical': 0.003937007874015748, 'mean_word_length': 3.147252747252747, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3292039', 'n_tokens_mistral': 4117, 'n_tokens_neox': 3889, 'n_words': 1016}