<commit_before># Copyright 2016 by <NAME> Corp.  All Rights Reserved.
'''
Utilities for creating a basic channel configuration for testing.
'''

from QGL.ChannelLibraries import EdgeFactory, MeasFactory, QubitFactory
from QGL import ChannelLibraries
from QGL.Channels import Edge, Measurement
from QGL.PulseSequencer import Pulse, CompositePulse
from QGL.PatternUtils import flatten
from QGL.PulsePrimitives import Id, X, MEAS
from QGL.ControlFlow import qsync, qwait, ControlInstruction, Goto, Barrier
from QGL.BlockLabel import BlockLabel

import collections
from math import pi

def channel_setup(new=True):
    # new indicates replace any existing library
    # Otherwise if there is an existing library, use it
    if not new and len(ChannelLibraries.channelLib.keys()) != 0:
        create_channel_library(ChannelLibraries.channelLib.channelDict)
    else:
        create_channel_library(new=True)

# Create a basic channel library
# Code stolen from QGL's test_Sequences
# It creates channels that are taken from test_Sequences APS2Helper
def create_channel_library(channels=dict(), new=False):
    from QGL.Channels import LogicalMarkerChannel, PhysicalQuadratureChannel, PhysicalMarkerChannel

    ChannelLibraries.ChannelLibrary(blank=True)

    qubit_names = ['q1','q2','q3']
    logical_names = ['digitizerTrig', 'slaveTrig']

    for name in logical_names:
        channels[name] = LogicalMarkerChannel(label=name)

    for name in qubit_names:
        mName = 'M-' + name
        mgName = 'M-' + name + '-gate'
        qgName = name + '-gate'

        mg = LogicalMarkerChannel(label=mgName)
        qg = LogicalMarkerChannel(label=qgName)

        m = MeasFactory(label=mName, gate_chan = mg, trig_chan=channels['digitizerTrig'])

        q = QubitFactory(label=name, gate_chan=qg)
        q.pulse_params['length'] = 30e-9
        q.pulse_params['phase'] = pi/2

        channels[name] = q
        channels[mName] = m
        channels[mgName]  = mg
        channels[qgName]  = qg

    # this block depends on the existence of q1 and q2
    channels['cr-gate'] = LogicalMarkerChannel(label='cr-gate')

    q1, q2 = channels['q1'], channels['q2']
    cr = None
    try:
        cr = EdgeFactory(q1, q2)
    except:
        cr = Edge(label="cr", source = q1, target = q2, gate_chan = channels['cr-gate'] )
    cr.pulse_params['length'] = 30e-9
    cr.pulse_params['phase'] = pi/4
    channels["cr"] = cr

    mq1q2g = LogicalMarkerChannel(label='M-q1q2-gate')
    channels['M-q1q2-gate']  = mq1q2g
    channels['M-q1q2']       = Measurement(label='M-q1q2', gate_chan = mq1q2g, trig_chan=channels['digitizerTrig'])

    # Add a 2nd edge from q2 back to q1 to support edgeTest4 (which is weird)
    channels['cr2-gate'] = LogicalMarkerChannel(label='cr2-gate')
    cr2 = None
    try:
        cr2 = EdgeFactory(q2, q1)
    except:
        cr2 = Edge(label="cr2", source = q2, target = q1, gate_chan = channels['cr2-gate'] )
    cr2.pulse_params['length'] = 30e-9
    cr2.pulse_params['phase'] = pi/4
    channels["cr2"] = cr2

    mq2q1g = LogicalMarkerChannel(label='M-q2q1-gate')
    channels['M-q2q1-gate']  = mq2q1g
    channels['M-q2q1']       = Measurement(label='M-q2q1', gate_chan = mq2q1g, trig_chan=channels['digitizerTrig'])

    # Now assign physical channels
    for name in ['APS1', 'APS2', 'APS3', 'APS4', 'APS5', 'APS6',
                 'APS7', 'APS8', 'APS9', 'APS10']:
        channelName = name + '-12'
        channel = PhysicalQuadratureChannel(label=channelName)
        channel.sampling_rate = 1.2e9
        channel.instrument = name
        channel.translator = 'APS2Pattern'
        channels[channelName] = channel

        for m in range(1,5):
            channelName = "{0}-12m{1}".format(name,m)
            channel = PhysicalMarkerChannel(label=channelName)
            channel.sampling_rate = 1.2e9
            channel.instrument = name
            channel.translator = 'APS2Pattern'
            channels[channelName] = channel

    mapping = {	'digitizerTrig' : 'APS1-12m1',
                'slaveTrig'     : 'APS1-12m2',
                'q1'            : 'APS1-12',
                'q1-gate'       : 'APS1-12m3',
                'M-q1'          : 'APS2-12',
                'M-q1-gate'     : 'APS2-12m1',
                'q2'            : 'APS3-12',
                'q2-gate'       : 'APS3-12m1',
                'M-q2'          : 'APS4-12',
                'M-q2-gate'     : 'APS4-12m1',
                'q3'            : 'APS7-12',
                'q3-gate'       : 'APS7-12m1',
                'M-q3'          : 'APS8-12',
                'M-q3-gate'     : 'APS8-12m1',
                'cr'            : 'APS5-12',
                'cr-gate'       : 'APS5-12m1',
                'M-q1q2'        : 'APS6-12',
                'M-q1q2-gate'   : 'APS6-12m1',
                'cr2'           : 'APS9-12',
                'cr2-gate'      : 'APS9-12m1',
                'M-q2q1'        : 'APS10-12',
                'M-q2q1-gate'   : 'APS10-12m1'}

    finalize_map(mapping, channels, new)
    return channels

# Store the given channels in the QGL ChannelLibraries
def finalize_map(mapping, channels, new=False):
    for name,value in mapping.items():
        channels[name].phys_chan = channels[value]

    if new:
        ChannelLibraries.channelLib = ChannelLibraries.ChannelLibrary(blank=True)
    ChannelLibraries.channelLib.channelDict = channels
    ChannelLibraries.channelLib.build_connectivity_graph()

def discard_zero_Ids(seqs):
    # assume seqs has a structure like [[entry0, entry1, ..., entryN]]
    for seq in seqs:
        ct = 0
        while ct < len(seq):
            entry = seq[ct]
            if isinstance(entry, Pulse) and entry.label == "Id" and entry.length == 0:
                del seq[ct]
            else:
                ct += 1

# Things like echoCR create lists of pulses that need to be flattened
# before calling compile_to_hardware
def flattenSeqs(seq):
    hasList = False
    for el in seq:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, Pulse, CompositePulse)) :
            hasList = True
            break
    if hasList:
        newS = []
        for el in flatten(seq):
            newS.append(el)
        return newS
    else:<commit_msg>Use new test_cl to create the testing ChannelLibrary<commit_after># Copyright 2016 by <NAME> Corp.  All Rights Reserved.
'''
Utilities for creating a basic channel configuration for testing.
'''

from QGL.ChannelLibraries import EdgeFactory, MeasFactory, QubitFactory
from QGL import ChannelLibraries
from QGL.Channels import Edge, Measurement
from QGL.PulseSequencer import Pulse, CompositePulse
from QGL.PatternUtils import flatten
from QGL.PulsePrimitives import Id, X, MEAS
from QGL.ControlFlow import qsync, qwait, ControlInstruction, Goto, Barrier
from QGL.BlockLabel import BlockLabel

from pyqgl2.test_cl import create_default_channelLibrary

import collections
from math import pi

def channel_setup(new=True):
    # new indicates replace any existing library
    # Otherwise if there is an existing library, use it
    # FIXME: For now, supplying first arg false meaning do not create physical channel mappings
    if not new and ChannelLibraries.channelLib is not None and len(ChannelLibraries.channelLib.keys()) != 0:
        create_default_channelLibrary(False, False)
        # create_channel_library(ChannelLibraries.channelLib.channelDict)
    else:
        create_default_channelLibrary(False, True)
        # create_channel_library(new=True)

# # OBE: Create a basic channel library
# # Code stolen from QGL's test_Sequences
# # It creates channels that are taken from test_Sequences APS2Helper
# def create_channel_library(channels=dict(), new=False):
#     from QGL.Channels import LogicalMarkerChannel, PhysicalQuadratureChannel, PhysicalMarkerChannel

#     ChannelLibraries.ChannelLibrary(blank=True)

#     qubit_names = ['q1','q2','q3']
#     logical_names = ['digitizerTrig', 'slaveTrig']

#     for name in logical_names:
#         channels[name] = LogicalMarkerChannel(label=name)

#     for name in qubit_names:
#         mName = 'M-' + name
#         mgName = 'M-' + name + '-gate'
#         qgName = name + '-gate'

#         mg = LogicalMarkerChannel(label=mgName)
#         qg = LogicalMarkerChannel(label=qgName)

#         m = MeasFactory(label=mName, gate_chan = mg, trig_chan=channels['digitizerTrig'])

#         q = QubitFactory(label=name, gate_chan=qg)
#         q.pulse_params['length'] = 30e-9
#         q.pulse_params['phase'] = pi/2

#         channels[name] = q
#         channels[mName] = m
#         channels[mgName]  = mg
#         channels[qgName]  = qg

#     # this block depends on the existence of q1 and q2
#     channels['cr-gate'] = LogicalMarkerChannel(label='cr-gate')

#     q1, q2 = channels['q1'], channels['q2']
#     cr = None
#     try:
#         cr = EdgeFactory(q1, q2)
#     except:
#         cr = Edge(label="cr", source = q1, target = q2, gate_chan = channels['cr-gate'] )
#     cr.pulse_params['length'] = 30e-9
#     cr.pulse_params['phase'] = pi/4
#     channels["cr"] = cr

#     mq1q2g = LogicalMarkerChannel(label='M-q1q2-gate')
#     channels['M-q1q2-gate']  = mq1q2g
#     channels['M-q1q2']       = Measurement(label='M-q1q2', gate_chan = mq1q2g, trig_chan=channels['digitizerTrig'])

#     # Add a 2nd edge from q2 back to q1 to support edgeTest4 (which is weird)
#     channels['cr2-gate'] = LogicalMarkerChannel(label='cr2-gate')
#     cr2 = None
#     try:
#         cr2 = EdgeFactory(q2, q1)
#     except:
#         cr2 = Edge(label="cr2", source = q2, target = q1, gate_chan = channels['cr2-gate'] )
#     cr2.pulse_params['length'] = 30e-9
#     cr2.pulse_params['phase'] = pi/4
#     channels["cr2"] = cr2

#     mq2q1g = LogicalMarkerChannel(label='M-q2q1-gate')
#     channels['M-q2q1-gate']  = mq2q1g
#     channels['M-q2q1']       = Measurement(label='M-q2q1', gate_chan = mq2q1g, trig_chan=channels['digitizerTrig'])

#     # Now assign physical channels
#     for name in ['APS1', 'APS2', 'APS3', 'APS4', 'APS5', 'APS6',
#                  'APS7', 'APS8', 'APS9', 'APS10']:
#         channelName = name + '-12'
#         channel = PhysicalQuadratureChannel(label=channelName)
#         channel.sampling_rate = 1.2e9
#         channel.instrument = name
#         channel.translator = 'APS2Pattern'
#         channels[channelName] = channel

#         for m in range(1,5):
#             channelName = "{0}-12m{1}".format(name,m)
#             channel = PhysicalMarkerChannel(label=channelName)
#             channel.sampling_rate = 1.2e9
#             channel.instrument = name
#             channel.translator = 'APS2Pattern'
#             channels[channelName] = channel

#     mapping = {	'digitizerTrig' : 'APS1-12m1',
#                 'slaveTrig'     : 'APS1-12m2',
#                 'q1'            : 'APS1-12',
#                 'q1-gate'       : 'APS1-12m3',
#                 'M-q1'          : 'APS2-12',
#                 'M-q1-gate'     : 'APS2-12m1',
#                 'q2'            : 'APS3-12',
#                 'q2-gate'       : 'APS3-12m1',
#                 'M-q2'          : 'APS4-12',
#                 'M-q2-gate'     : 'APS4-12m1',
#                 'q3'            : 'APS7-12',
#                 'q3-gate'       : 'APS7-12m1',
#                 'M-q3'          : 'APS8-12',
#                 'M-q3-gate'     : 'APS8-12m1',
#                 'cr'            : 'APS5-12',
#                 'cr-gate'       : 'APS5-12m1',
#                 'M-q1q2'        : 'APS6-12',
#                 'M-q1q2-gate'   : 'APS6-12m1',
#                 'cr2'           : 'APS9-12',
#                 'cr2-gate'      : 'APS9-12m1',
#                 'M-q2q1'        : 'APS10-12',
#                 'M-q2q1-gate'   : 'APS10-12m1'}

#     finalize_map(mapping, channels, new)
#     return channels

# # OBE: Store the given channels in the QGL ChannelLibraries
# def finalize_map(mapping, channels, new=False):
#     for name,value in mapping.items():
#         channels[name].phys_chan = channels[value]

#     if new:
#         ChannelLibraries.channelLib = ChannelLibraries.ChannelLibrary(blank=True)
#     ChannelLibraries.channelLib.channelDict = channels
#     ChannelLibraries.channelLib.build_connectivity_graph()



def discard_zero_Ids(seqs):
    # assume seqs has a structure like [[entry0, entry1, ..., entryN]]
    for seq in seqs:
        ct = 0
        while ct < len(seq):
            entry = seq[ct]
            if isinstance(entry, Pulse) and entry.label == "Id" and entry.length == 0:
                del seq[ct]
            else:
                ct += 1

# Things like echoCR create lists of pulses that need to be flattened
# before calling compile_to_hardware
def flattenSeqs(seq):
    hasList = False
    for el in seq:
        if isinstance(el, collections.Iterable) and not isinstance(el, (str, Pulse, CompositePulse)) :
            hasList = True
            break
    if hasList:
        newS = []
        for el in flatten(seq):
            newS.append(el)
        return newS
    else:
{'fraction_non_alphanumeric': 0.1215926493108729, 'fraction_numerical': 0.032542113323124046, 'mean_word_length': 2.313292744799594, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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}}
{'dir': 'git-commits-cleaned', 'id': '298412', 'n_tokens_mistral': 4519, 'n_tokens_neox': 4249, 'n_words': 967}