<commit_before># Copyright 2016 <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

try:
    from QGL import *
    from QGL import config as QGLconfig
    from QGL.BasicSequences.helpers import create_cal_seqs, delay_descriptor, cal_descriptor
except:
    print("Could not find QGL")

import auspex.config as config
from auspex.log import logger
from copy import copy, deepcopy
import os
import pandas as pd

import time
from auspex.exp_factory import QubitExpFactory
from auspex.parameter import FloatParameter
from auspex.filters.plot import ManualPlotter
from auspex.analysis.fits import *
from auspex.analysis.helpers import normalize_data
from matplotlib import cm
import numpy as np
from itertools import product

def calibrate(calibrations, update_settings=True, cal_log=True, leave_plots_open=True):
    """Takes in a qubit (as a string) and list of calibrations (as instantiated classes).
    e.g. calibrate_pulses([RabiAmp("q1"), PhaseEstimation("q1")])"""
    for calibration in calibrations:
        calibration.leave_plots_open = leave_plots_open
        if not isinstance(calibration, PulseCalibration):
            raise TypeError("calibrate_pulses was passed a calibration that is not actually a calibration.")
        try:
            cal_result = calibration.calibrate()
            if update_settings:
                calibration.update_settings()
            if cal_log:
                calibration.write_to_log(cal_result)
        except Exception as ex:
            logger.warning('Calibration {} could not complete: got exception: {}.'.format(type(calibration).__name__, ex))
            try:
                calibration.exp.shutdown()
            except:
                pass #Experiment not yet created, so ignore
            raise Exception("Calibration failure") from ex
        finally:
            time.sleep(0.1) #occasionally ZMQ barfs here
            if hasattr(calibration.exp, 'extra_plot_server'):
                try:
                    if calibration.plot:
                        if self.leave_plots_open:
                            if isinstance(calibration.plot, list):
                                for p in calibration.plot:
                                    p.set_quit()
                            else:
                                calibration.plot.set_quit()
                        calibration.exp.extra_plot_server.shutdown()
                        calibration.exp.extra_plot_desc_server.shutdown()
                except Exception as e:
                    print("Exception while shutting down fitting plots:", e)

class PulseCalibration(object):
    """Base class for calibration of qubit control pulses."""
    def __init__(self, qubit_names, quad="real"):
        super(PulseCalibration, 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.filename         = 'None'
        self.exp              = None
        self.axis_descriptor  = None
        self.leave_plots_open = True
        self.cw_mode          = False
        self.saved_settings   = config.load_meas_file(config.meas_file)
        self.settings         = deepcopy(self.saved_settings) #make a copy for used during calibration
        self.quad             = quad
        if quad == "real":
            self.quad_fun = np.real
        elif quad == "imag":
            self.quad_fun = np.imag
        elif quad == "amp":
            self.quad_fun = np.abs
        elif quad == "phase":
            self.quad_fun = np.angle
        else:
            raise ValueError('Quadrature to calibrate must be one of ("real", "imag", "amp", "phase").')
        self.plot       = self.init_plot()

    def sequence(self):
        """Returns the sequence for the given calibration, must be overridden"""
        return [[Id(self.qubit), MEAS(self.qubit)]]

    def set(self, instrs_to_set = [], exp_step = 0, **params):
        try:
            extra_plot_server = self.exp.extra_plot_server
        except Exception as e:
            pass #no experiment yet created, or plot server not yet started
        if hasattr(self.exp, 'extra_plot_server'):
            extra_plot_server = self.exp.extra_plot_server

        meta_file = compile_to_hardware(self.sequence(**params), fileName=self.filename, axis_descriptor=self.axis_descriptor)
        self.exp = QubitExpFactory.create(meta_file=meta_file, calibration=True, save_data=False, cw_mode=self.cw_mode)
        self.exp.leave_plot_server_open = True
        self.exp.first_exp = not bool(exp_step)

        #Update all instruments that need to keep track of experiment numnber. Adapted from https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-python-dictionaries-and-lists
        def find_all_items(obj, key):
            ret = []
            if key in obj:
                out_path = obj
                ret.append(out_path)
            for k, v in obj.items():
                if isinstance(v, dict):
                    ret += find_all_items(v, key)
            return ret

        for k in find_all_items(self.exp.settings, 'exp_step'):
            k['exp_step'] = exp_step

        try:
            self.exp.extra_plot_server = extra_plot_server
        except:
            pass
        if self.plot:
            [self.exp.add_manual_plotter(p) for p in self.plot] if isinstance(self.plot, list) else self.exp.add_manual_plotter(self.plot)
        
        #sweep instruments for calibration
        for instr_to_set in instrs_to_set:
            par = FloatParameter()
            par.assign_method(getattr(self.exp._instruments[instr_to_set['instr']], instr_to_set['method']))
            if 'sweep_values' in instr_to_set.keys():
                par.value = instr_to_set['sweep_values'][0]
                self.exp.add_sweep(par, instr_to_set['sweep_values'])
            else:
                raise KeyError("Sweep values not defined.")
<commit_msg>Typo in pulse cal for leaving plots open<commit_after># Copyright 2016 <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

try:
    from QGL import *
    from QGL import config as QGLconfig
    from QGL.BasicSequences.helpers import create_cal_seqs, delay_descriptor, cal_descriptor
except:
    print("Could not find QGL")

import auspex.config as config
from auspex.log import logger
from copy import copy, deepcopy
import os
import pandas as pd

import time
from auspex.exp_factory import QubitExpFactory
from auspex.parameter import FloatParameter
from auspex.filters.plot import ManualPlotter
from auspex.analysis.fits import *
from auspex.analysis.helpers import normalize_data
from matplotlib import cm
import numpy as np
from itertools import product

def calibrate(calibrations, update_settings=True, cal_log=True, leave_plots_open=True):
    """Takes in a qubit (as a string) and list of calibrations (as instantiated classes).
    e.g. calibrate_pulses([RabiAmp("q1"), PhaseEstimation("q1")])"""
    for calibration in calibrations:
        calibration.leave_plots_open = leave_plots_open
        if not isinstance(calibration, PulseCalibration):
            raise TypeError("calibrate_pulses was passed a calibration that is not actually a calibration.")
        try:
            cal_result = calibration.calibrate()
            if update_settings:
                calibration.update_settings()
            if cal_log:
                calibration.write_to_log(cal_result)
        except Exception as ex:
            logger.warning('Calibration {} could not complete: got exception: {}.'.format(type(calibration).__name__, ex))
            try:
                calibration.exp.shutdown()
            except:
                pass #Experiment not yet created, so ignore
            raise Exception("Calibration failure") from ex
        finally:
            time.sleep(0.1) #occasionally ZMQ barfs here
            if hasattr(calibration.exp, 'extra_plot_server'):
                try:
                    if calibration.plot:
                        if calibration.leave_plots_open:
                            if isinstance(calibration.plot, list):
                                for p in calibration.plot:
                                    p.set_quit()
                            else:
                                calibration.plot.set_quit()
                        calibration.exp.extra_plot_server.shutdown()
                        calibration.exp.extra_plot_desc_server.shutdown()
                except Exception as e:
                    print("Exception while shutting down fitting plots:", e)

class PulseCalibration(object):
    """Base class for calibration of qubit control pulses."""
    def __init__(self, qubit_names, quad="real"):
        super(PulseCalibration, 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.filename         = 'None'
        self.exp              = None
        self.axis_descriptor  = None
        self.leave_plots_open = True
        self.cw_mode          = False
        self.saved_settings   = config.load_meas_file(config.meas_file)
        self.settings         = deepcopy(self.saved_settings) #make a copy for used during calibration
        self.quad             = quad
        if quad == "real":
            self.quad_fun = np.real
        elif quad == "imag":
            self.quad_fun = np.imag
        elif quad == "amp":
            self.quad_fun = np.abs
        elif quad == "phase":
            self.quad_fun = np.angle
        else:
            raise ValueError('Quadrature to calibrate must be one of ("real", "imag", "amp", "phase").')
        self.plot       = self.init_plot()

    def sequence(self):
        """Returns the sequence for the given calibration, must be overridden"""
        return [[Id(self.qubit), MEAS(self.qubit)]]

    def set(self, instrs_to_set = [], exp_step = 0, **params):
        try:
            extra_plot_server = self.exp.extra_plot_server
        except Exception as e:
            pass #no experiment yet created, or plot server not yet started
        if hasattr(self.exp, 'extra_plot_server'):
            extra_plot_server = self.exp.extra_plot_server

        meta_file = compile_to_hardware(self.sequence(**params), fileName=self.filename, axis_descriptor=self.axis_descriptor)
        self.exp = QubitExpFactory.create(meta_file=meta_file, calibration=True, save_data=False, cw_mode=self.cw_mode)
        self.exp.leave_plot_server_open = True
        self.exp.first_exp = not bool(exp_step)

        #Update all instruments that need to keep track of experiment numnber. Adapted from https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-python-dictionaries-and-lists
        def find_all_items(obj, key):
            ret = []
            if key in obj:
                out_path = obj
                ret.append(out_path)
            for k, v in obj.items():
                if isinstance(v, dict):
                    ret += find_all_items(v, key)
            return ret

        for k in find_all_items(self.exp.settings, 'exp_step'):
            k['exp_step'] = exp_step

        try:
            self.exp.extra_plot_server = extra_plot_server
        except:
            pass
        if self.plot:
            [self.exp.add_manual_plotter(p) for p in self.plot] if isinstance(self.plot, list) else self.exp.add_manual_plotter(self.plot)

        #sweep instruments for calibration
        for instr_to_set in instrs_to_set:
            par = FloatParameter()
            par.assign_method(getattr(self.exp._instruments[instr_to_set['instr']], instr_to_set['method']))
            if 'sweep_values' in instr_to_set.keys():
                par.value = instr_to_set['sweep_values'][0]
                self.exp.add_sweep(par, instr_to_set['sweep_values'])
            else:
                raise KeyError("Sweep values not defined.")

{'fraction_non_alphanumeric': 0.08550955414012738, 'fraction_numerical': 0.0033439490445859874, 'mean_word_length': 2.2390407426508507, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102818', 'n_tokens_mistral': 3626, 'n_tokens_neox': 3440, 'n_words': 933}