async def multiple_request(self, urls, is_gather=False, **kwargs):
        """For crawling multiple urls"""
        if is_gather:
            resp_results = await asyncio.gather(
                *[
                    self.handle_request(self.request(url=url, **kwargs))
                    for url in urls
                ],
                return_exceptions=True)
            for index, task_result in enumerate(resp_results):
                if not isinstance(task_result, RuntimeError) and task_result:
                    _, response = task_result
                    response.index = index
                    yield response
        else:
            for index, url in enumerate(urls):
                _, response = await self.handle_request(
                    self.request(url=url, **kwargs))
                response.index = index
                yield response

async def multiple_request ( self , urls , is_gather = False , * * kwargs ) : if is_gather : resp_results = await asyncio . gather ( * [ self . handle_request ( self . request ( url = url , * * kwargs ) ) for url in urls ] , return_exceptions = True ) for index , task_result in enumerate ( resp_results ) : if not isinstance ( task_result , RuntimeError ) and task_result : _ , response = task_result response . index = index yield response else : for index , url in enumerate ( urls ) : _ , response = await self . handle_request ( self . request ( url = url , * * kwargs ) ) response . index = index yield response


def request(self,
                url: str,
                method: str = 'GET',
                *,
                callback=None,
                encoding: typing.Optional[str] = None,
                headers: dict = None,
                metadata: dict = None,
                request_config: dict = None,
                request_session=None,
                **kwargs):
        """Init a Request class for crawling html"""
        headers = headers or {}
        metadata = metadata or {}
        request_config = request_config or {}
        request_session = request_session or self.request_session

        headers.update(self.headers.copy())
        request_config.update(self.request_config.copy())
        kwargs.update(self.kwargs.copy())

        return Request(
            url=url,
            method=method,
            callback=callback,
            encoding=encoding,
            headers=headers,
            metadata=metadata,
            request_config=request_config,
            request_session=request_session,
            **kwargs)

def request ( self , url : str , method : str = 'GET' , * , callback = None , encoding : typing . Optional [ str ] = None , headers : dict = None , metadata : dict = None , request_config : dict = None , request_session = None , * * kwargs ) : headers = headers or { } metadata = metadata or { } request_config = request_config or { } request_session = request_session or self . request_session headers . update ( self . headers . copy ( ) ) request_config . update ( self . request_config . copy ( ) ) kwargs . update ( self . kwargs . copy ( ) ) return Request ( url = url , method = method , callback = callback , encoding = encoding , headers = headers , metadata = metadata , request_config = request_config , request_session = request_session , * * kwargs )


async def start_master(self):
        """Actually start crawling."""
        for url in self.start_urls:
            request_ins = self.request(
                url=url, callback=self.parse, metadata=self.metadata)
            self.request_queue.put_nowait(self.handle_request(request_ins))
        workers = [
            asyncio.ensure_future(self.start_worker())
            for i in range(self.worker_numbers)
        ]
        for worker in workers:
            self.logger.info(f"Worker started: {id(worker)}")
        await self.request_queue.join()

        if not self.is_async_start:
            await self.stop(SIGINT)
        else:
            await self._cancel_tasks()

async
def
start_master
(
self
)
:
for
url
in
self
.
start_urls
:
request_ins
=
self
.
request
(
url
=
url
,
callback
=
self
.
parse
,
metadata
=
self
.
metadata
)
self
.
request_queue
.
put_nowait
(
self
.
handle_request
(
request_ins
)
)
workers
=
[
asyncio
.
ensure_future
(
self
.
start_worker
(
)
)
for
i
in
range
(
self
.
worker_numbers
)
]
for
worker
in
workers
:
self
.
logger
.
info
(
f"Worker started: {id(worker)}"
)
await
self
.
request_queue
.
join
(
)
if
not
self
.
is_async_start
:
await
self
.
stop
(
SIGINT
)
else
:
await
self
.
_cancel_tasks
(
)


def normalize_task_v2(task):
    '''Ensures tasks have an action key and strings are converted to python objects'''

    result = dict()
    mod_arg_parser = ModuleArgsParser(task)
    try:
        action, arguments, result['delegate_to'] = mod_arg_parser.parse()
    except AnsibleParserError as e:
        try:
            task_info = "%s:%s" % (task[FILENAME_KEY], task[LINE_NUMBER_KEY])
            del task[FILENAME_KEY]
            del task[LINE_NUMBER_KEY]
        except KeyError:
            task_info = "Unknown"
        try:
            import pprint
            pp = pprint.PrettyPrinter(indent=2)
            task_pprint = pp.pformat(task)
        except ImportError:
            task_pprint = task
        raise SystemExit("Couldn't parse task at %s (%s)\n%s" % (task_info, e.message, task_pprint))

    # denormalize shell -> command conversion
    if '_uses_shell' in arguments:
        action = 'shell'
        del(arguments['_uses_shell'])

    for (k, v) in list(task.items()):
        if k in ('action', 'local_action', 'args', 'delegate_to') or k == action:
            # we don't want to re-assign these values, which were
            # determined by the ModuleArgsParser() above
            continue
        else:
            result[k] = v

    result['action'] = dict(__ansible_module__=action)

    if '_raw_params' in arguments:
        result['action']['__ansible_arguments__'] = arguments['_raw_params'].split(' ')
        del(arguments['_raw_params'])
    else:
        result['action']['__ansible_arguments__'] = list()

    if 'argv' in arguments and not result['action']['__ansible_arguments__']:
        result['action']['__ansible_arguments__'] = arguments['argv']
        del(arguments['argv'])

    result['action'].update(arguments)
    return result


def parse_yaml_linenumbers(data, filename):
    """Parses yaml as ansible.utils.parse_yaml but with linenumbers.

    The line numbers are stored in each node's LINE_NUMBER_KEY key.
    """

    def compose_node(parent, index):
        # the line number where the previous token has ended (plus empty lines)
        line = loader.line
        node = Composer.compose_node(loader, parent, index)
        node.__line__ = line + 1
        return node

    def construct_mapping(node, deep=False):
        if ANSIBLE_VERSION < 2:
            mapping = Constructor.construct_mapping(loader, node, deep=deep)
        else:
            mapping = AnsibleConstructor.construct_mapping(loader, node, deep=deep)
        if hasattr(node, '__line__'):
            mapping[LINE_NUMBER_KEY] = node.__line__
        else:
            mapping[LINE_NUMBER_KEY] = mapping._line_number
        mapping[FILENAME_KEY] = filename
        return mapping

    try:
        if ANSIBLE_VERSION < 2:
            loader = yaml.Loader(data)
        else:
            import inspect
            kwargs = {}
            if 'vault_password' in inspect.getargspec(AnsibleLoader.__init__).args:
                kwargs['vault_password'] = DEFAULT_VAULT_PASSWORD
            loader = AnsibleLoader(data, **kwargs)
        loader.compose_node = compose_node
        loader.construct_mapping = construct_mapping
        data = loader.get_single_data()
    except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
        raise SystemExit("Failed to parse YAML in %s: %s" % (filename, str(e)))
    return data

Parses yaml as ansible . utils . parse_yaml but with linenumbers .


def wheel_dist_name(self):
        """Return distribution full name with - replaced with _"""
        return '-'.join((safer_name(self.distribution.get_name()),
                         safer_version(self.distribution.get_version())))

def wheel_dist_name ( self ) : return '-' . join ( ( safer_name ( self . distribution . get_name ( ) ) , safer_version ( self . distribution . get_version ( ) ) ) )


def get_archive_basename(self):
        """Return archive name without extension"""

        impl_tag, abi_tag, plat_tag = self.get_tag()

        archive_basename = "%s-%s-%s-%s" % (
            self.wheel_dist_name,
            impl_tag,
            abi_tag,
            plat_tag)
        return archive_basename

def get_archive_basename ( self ) : impl_tag , abi_tag , plat_tag = self . get_tag ( ) archive_basename = "%s-%s-%s-%s" % ( self . wheel_dist_name , impl_tag , abi_tag , plat_tag ) return archive_basename


def add_requirements(self, metadata_path):
        """Add additional requirements from setup.cfg to file metadata_path"""
        additional = list(self.setupcfg_requirements())
        if not additional: return
        pkg_info = read_pkg_info(metadata_path)
        if 'Provides-Extra' in pkg_info or 'Requires-Dist' in pkg_info:
            warnings.warn('setup.cfg requirements overwrite values from setup.py')
            del pkg_info['Provides-Extra']
            del pkg_info['Requires-Dist']
        for k, v in additional:
            pkg_info[k] = v
        write_pkg_info(metadata_path, pkg_info)

Add additional requirements from setup . cfg to file metadata_path


def egg2dist(self, egginfo_path, distinfo_path):
        """Convert an .egg-info directory into a .dist-info directory"""
        def adios(p):
            """Appropriately delete directory, file or link."""
            if os.path.exists(p) and not os.path.islink(p) and os.path.isdir(p):
                shutil.rmtree(p)
            elif os.path.exists(p):
                os.unlink(p)

        adios(distinfo_path)

        if not os.path.exists(egginfo_path):
            # There is no egg-info. This is probably because the egg-info
            # file/directory is not named matching the distribution name used
            # to name the archive file. Check for this case and report
            # accordingly.
            import glob
            pat = os.path.join(os.path.dirname(egginfo_path), '*.egg-info')
            possible = glob.glob(pat)
            err = "Egg metadata expected at %s but not found" % (egginfo_path,)
            if possible:
                alt = os.path.basename(possible[0])
                err += " (%s found - possible misnamed archive file?)" % (alt,)

            raise ValueError(err)

        if os.path.isfile(egginfo_path):
            # .egg-info is a single file
            pkginfo_path = egginfo_path
            pkg_info = self._pkginfo_to_metadata(egginfo_path, egginfo_path)
            os.mkdir(distinfo_path)
        else:
            # .egg-info is a directory
            pkginfo_path = os.path.join(egginfo_path, 'PKG-INFO')
            pkg_info = self._pkginfo_to_metadata(egginfo_path, pkginfo_path)

            # ignore common egg metadata that is useless to wheel
            shutil.copytree(egginfo_path, distinfo_path,
                            ignore=lambda x, y: set(('PKG-INFO',
                                                     'requires.txt',
                                                     'SOURCES.txt',
                                                     'not-zip-safe',)))

            # delete dependency_links if it is only whitespace
            dependency_links_path = os.path.join(distinfo_path, 'dependency_links.txt')
            with open(dependency_links_path, 'r') as dependency_links_file:
                dependency_links = dependency_links_file.read().strip()
            if not dependency_links:
                adios(dependency_links_path)

        write_pkg_info(os.path.join(distinfo_path, 'METADATA'), pkg_info)

        # XXX deprecated. Still useful for current distribute/setuptools.
        metadata_path = os.path.join(distinfo_path, 'METADATA')
        self.add_requirements(metadata_path)

        # XXX intentionally a different path than the PEP.
        metadata_json_path = os.path.join(distinfo_path, 'metadata.json')
        pymeta = pkginfo_to_dict(metadata_path,
                                 distribution=self.distribution)

        if 'description' in pymeta:
            description_filename = 'DESCRIPTION.rst'
            description_text = pymeta.pop('description')
            description_path = os.path.join(distinfo_path,
                                            description_filename)
            with open(description_path, "wb") as description_file:
                description_file.write(description_text.encode('utf-8'))
            pymeta['extensions']['python.details']['document_names']['description'] = description_filename

        # XXX heuristically copy any LICENSE/LICENSE.txt?
        license = self.license_file()
        if license:
            license_filename = 'LICENSE.txt'
            shutil.copy(license, os.path.join(self.distinfo_dir, license_filename))
            pymeta['extensions']['python.details']['document_names']['license'] = license_filename

        with open(metadata_json_path, "w") as metadata_json:
            json.dump(pymeta, metadata_json, sort_keys=True)

        adios(egginfo_path)

Convert an . egg - info directory into a . dist - info directory


def text(text: str, speak: str = None, input_hint: Union[InputHints, str] = InputHints.accepting_input) -> Activity:
        """
        Returns a simple text message.

        :Example:
        message = MessageFactory.text('Greetings from example message')
        await context.send_activity(message)

        :param text:
        :param speak:
        :param input_hint:
        :return:
        """
        message = Activity(type=ActivityTypes.message, text=text, input_hint=input_hint)
        if speak:
            message.speak = speak

        return message

def text ( text : str , speak : str = None , input_hint : Union [ InputHints , str ] = InputHints . accepting_input ) -> Activity : message = Activity ( type = ActivityTypes . message , text = text , input_hint = input_hint ) if speak : message . speak = speak return message


def suggested_actions(actions: List[CardAction], text: str = None, speak: str = None,
                          input_hint: Union[InputHints, str] = InputHints.accepting_input) -> Activity:
        """
        Returns a message that includes a set of suggested actions and optional text.

        :Example:
        message = MessageFactory.suggested_actions([CardAction(title='a', type=ActionTypes.im_back, value='a'),
                                                    CardAction(title='b', type=ActionTypes.im_back, value='b'),
                                                    CardAction(title='c', type=ActionTypes.im_back, value='c')], 'Choose a color')
        await context.send_activity(message)

        :param actions:
        :param text:
        :param speak:
        :param input_hint:
        :return:
        """
        actions = SuggestedActions(actions=actions)
        message = Activity(type=ActivityTypes.message, input_hint=input_hint, suggested_actions=actions)
        if text:
            message.text = text
        if speak:
            message.speak = speak
        return message

def suggested_actions ( actions : List [ CardAction ] , text : str = None , speak : str = None , input_hint : Union [ InputHints , str ] = InputHints . accepting_input ) -> Activity : actions = SuggestedActions ( actions = actions ) message = Activity ( type = ActivityTypes . message , input_hint = input_hint , suggested_actions = actions ) if text : message . text = text if speak : message . speak = speak return message

Returns a message that includes a set of suggested actions and optional text .


def attachment(attachment: Attachment, text: str = None, speak: str = None,
                   input_hint: Union[InputHints, str] = None):
        """
        Returns a single message activity containing an attachment.

        :Example:
        message = MessageFactory.attachment(CardFactory.hero_card(HeroCard(title='White T-Shirt',
                                                                  images=[CardImage(url='https://example.com/whiteShirt.jpg')],
                                                                  buttons=[CardAction(title='buy')])))
        await context.send_activity(message)

        :param attachment:
        :param text:
        :param speak:
        :param input_hint:
        :return:
        """
        return attachment_activity(AttachmentLayoutTypes.list, [attachment], text, speak, input_hint)

def attachment ( attachment : Attachment , text : str = None , speak : str = None , input_hint : Union [ InputHints , str ] = None ) : return attachment_activity ( AttachmentLayoutTypes . list , [ attachment ] , text , speak , input_hint )

Returns a single message activity containing an attachment .


def list(attachments: List[Attachment], text: str = None, speak: str = None,
             input_hint: Union[InputHints, str] = None) -> Activity:
        """
        Returns a message that will display a set of attachments in list form.

        :Example:
        message = MessageFactory.list([CardFactory.hero_card(HeroCard(title='title1',
                                                             images=[CardImage(url='imageUrl1')],
                                                             buttons=[CardAction(title='button1')])),
                                       CardFactory.hero_card(HeroCard(title='title2',
                                                             images=[CardImage(url='imageUrl2')],
                                                             buttons=[CardAction(title='button2')])),
                                       CardFactory.hero_card(HeroCard(title='title3',
                                                             images=[CardImage(url='imageUrl3')],
                                                             buttons=[CardAction(title='button3')]))])
        await context.send_activity(message)

        :param attachments:
        :param text:
        :param speak:
        :param input_hint:
        :return:
        """
        return attachment_activity(AttachmentLayoutTypes.list, attachments, text, speak, input_hint)

def list ( attachments : List [ Attachment ] , text : str = None , speak : str = None , input_hint : Union [ InputHints , str ] = None ) -> Activity : return attachment_activity ( AttachmentLayoutTypes . list , attachments , text , speak , input_hint )

Returns a message that will display a set of attachments in list form .


def content_url(url: str, content_type: str, name: str = None, text: str = None, speak: str = None,
                    input_hint: Union[InputHints, str] = None):
        """
        Returns a message that will display a single image or video to a user.

        :Example:
        message = MessageFactory.content_url('https://example.com/hawaii.jpg', 'image/jpeg',
                                             'Hawaii Trip', 'A photo from our family vacation.')
        await context.send_activity(message)

        :param url:
        :param content_type:
        :param name:
        :param text:
        :param speak:
        :param input_hint:
        :return:
        """
        attachment = Attachment(content_type=content_type, content_url=url)
        if name:
            attachment.name = name
        return attachment_activity(AttachmentLayoutTypes.list, [attachment], text, speak, input_hint)

def content_url ( url : str , content_type : str , name : str = None , text : str = None , speak : str = None , input_hint : Union [ InputHints , str ] = None ) : attachment = Attachment ( content_type = content_type , content_url = url ) if name : attachment . name = name return attachment_activity ( AttachmentLayoutTypes . list , [ attachment ] , text , speak , input_hint )

Returns a message that will display a single image or video to a user .


def create_trace(
        turn_activity: Activity,
        name: str,
        value: object = None,
        value_type: str = None,
        label: str = None,
    ) -> Activity:
        """Creates a trace activity based on this activity.

        :param turn_activity:
        :type turn_activity: Activity
        :param name: The value to assign to the trace activity's <see cref="Activity.name"/> property.
        :type name: str
        :param value: The value to assign to the trace activity's <see cref="Activity.value"/> property., defaults to None
        :param value: object, optional
        :param value_type: The value to assign to the trace activity's <see cref="Activity.value_type"/> property, defaults to None
        :param value_type: str, optional
        :param label: The value to assign to the trace activity's <see cref="Activity.label"/> property, defaults to None
        :param label: str, optional
        :return: The created trace activity.
        :rtype: Activity
        """

        from_property = (
            ChannelAccount(
                id=turn_activity.recipient.id, name=turn_activity.recipient.name
            )
            if turn_activity.recipient is not None
            else ChannelAccount()
        )
        if value_type is None and value is not None:
            value_type = type(value).__name__

        reply = Activity(
            type=ActivityTypes.trace,
            timestamp=datetime.utcnow(),
            from_property=from_property,
            recipient=ChannelAccount(
                id=turn_activity.from_property.id, name=turn_activity.from_property.name
            ),
            reply_to_id=turn_activity.id,
            service_url=turn_activity.service_url,
            channel_id=turn_activity.channel_id,
            conversation=ConversationAccount(
                is_group=turn_activity.conversation.is_group,
                id=turn_activity.conversation.id,
                name=turn_activity.conversation.name,
            ),
            name=name,
            label=label,
            value_type=value_type,
            value=value,
        )
        return reply

def create_trace ( turn_activity : Activity , name : str , value : object = None , value_type : str = None , label : str = None , ) -> Activity : from_property = ( ChannelAccount ( id = turn_activity . recipient . id , name = turn_activity . recipient . name ) if turn_activity . recipient is not None else ChannelAccount ( ) ) if value_type is None and value is not None : value_type = type ( value ) . __name__ reply = Activity ( type = ActivityTypes . trace , timestamp = datetime . utcnow ( ) , from_property = from_property , recipient = ChannelAccount ( id = turn_activity . from_property . id , name = turn_activity . from_property . name ) , reply_to_id = turn_activity . id , service_url = turn_activity . service_url , channel_id = turn_activity . channel_id , conversation = ConversationAccount ( is_group = turn_activity . conversation . is_group , id = turn_activity . conversation . id , name = turn_activity . conversation . name , ) , name = name , label = label , value_type = value_type , value = value , ) return reply

Creates a trace activity based on this activity .


def telemetry_client(self, value: BotTelemetryClient) -> None:
        """
        Sets the telemetry client for logging events.
        """
        if value is None:
            self._telemetry_client = NullTelemetryClient()
        else:
            self._telemetry_client = value

def telemetry_client ( self , value : BotTelemetryClient ) -> None : if value is None : self . _telemetry_client = NullTelemetryClient ( ) else : self . _telemetry_client = value

Sets the telemetry client for logging events .


async def read(self, keys: List[str]) -> dict:
        """Read storeitems from storage.

        :param keys:
        :return dict:
        """
        try:
            # check if the database and container exists and if not create
            if not self.__container_exists:
                self.__create_db_and_container()
            if len(keys) > 0:
                # create the parameters object
                parameters = [
                    {'name': f'@id{i}', 'value': f'{self.__sanitize_key(key)}'}
                    for i, key in enumerate(keys)
                    ]
                # get the names of the params
                parameter_sequence = ','.join(param.get('name')
                                              for param in parameters)
                # create the query
                query = {
                    "query":
                    f"SELECT c.id, c.realId, c.document, c._etag \
FROM c WHERE c.id in ({parameter_sequence})",
                    "parameters": parameters
                    }
                options = {'enableCrossPartitionQuery': True}
                # run the query and store the results as a list
                results = list(
                    self.client.QueryItems(
                        self.__container_link, query, options)
                    )
                # return a dict with a key and a StoreItem
                return {
                    r.get('realId'): self.__create_si(r) for r in results
                    }
            else:
                raise Exception('cosmosdb_storage.read(): \
provide at least one key')
        except TypeError as e:
            raise e


async def write(self, changes: Dict[str, StoreItem]):
        """Save storeitems to storage.

        :param changes:
        :return:
        """
        try:
            # check if the database and container exists and if not create
            if not self.__container_exists:
                self.__create_db_and_container()
                # iterate over the changes
            for (key, change) in changes.items():
                # store the e_tag
                e_tag = change.e_tag
                # create the new document
                doc = {'id': self.__sanitize_key(key),
                       'realId': key,
                       'document': self.__create_dict(change)
                       }
                # the e_tag will be * for new docs so do an insert
                if (e_tag == '*' or not e_tag):
                    self.client.UpsertItem(
                        database_or_Container_link=self.__container_link,
                        document=doc,
                        options={'disableAutomaticIdGeneration': True}
                        )
                # if we have an etag, do opt. concurrency replace
                elif(len(e_tag) > 0):
                    access_condition = {'type': 'IfMatch', 'condition': e_tag}
                    self.client.ReplaceItem(
                        document_link=self.__item_link(
                            self.__sanitize_key(key)),
                        new_document=doc,
                        options={'accessCondition': access_condition}
                        )
                # error when there is no e_tag
                else:
                    raise Exception('cosmosdb_storage.write(): etag missing')
        except Exception as e:
            raise e


async def delete(self, keys: List[str]):
        """Remove storeitems from storage.

        :param keys:
        :return:
        """
        try:
            # check if the database and container exists and if not create
            if not self.__container_exists:
                self.__create_db_and_container()
            # call the function for each key
            for k in keys:
                self.client.DeleteItem(
                    document_link=self.__item_link(self.__sanitize_key(k)))
                # print(res)
        except cosmos_errors.HTTPFailure as h:
            # print(h.status_code)
            if h.status_code != 404:
                raise h
        except TypeError as e:
            raise e


def __create_si(self, result) -> StoreItem:
        """Create a StoreItem from a result out of CosmosDB.

        :param result:
        :return StoreItem:
        """
        # get the document item from the result and turn into a dict
        doc = result.get('document')
        # readd the e_tag from Cosmos
        doc['e_tag'] = result.get('_etag')
        # create and return the StoreItem
        return StoreItem(**doc)

Create a StoreItem from a result out of CosmosDB .


def __create_dict(self, si: StoreItem) -> Dict:
        """Return the dict of a StoreItem.

        This eliminates non_magic attributes and the e_tag.

        :param si:
        :return dict:
        """
        # read the content
        non_magic_attr = ([attr for attr in dir(si)
                          if not attr.startswith('_') or attr.__eq__('e_tag')])
        # loop through attributes and write and return a dict
        return ({attr: getattr(si, attr)
                for attr in non_magic_attr})


def __sanitize_key(self, key) -> str:
        """Return the sanitized key.

        Replace characters that are not allowed in keys in Cosmos.

        :param key:
        :return str:
        """
        # forbidden characters
        bad_chars = ['\\', '?', '/', '#', '\t', '\n', '\r']
        # replace those with with '*' and the
        # Unicode code point of the character and return the new string
        return ''.join(
            map(
                lambda x: '*'+str(ord(x)) if x in bad_chars else x, key
                )
            )


def __create_db_and_container(self):
        """Call the get or create methods."""
        db_id = self.config.database
        container_name = self.config.container
        self.db = self.__get_or_create_database(self.client, db_id)
        self.container = self.__get_or_create_container(
            self.client, container_name
            )

def __create_db_and_container ( self ) : db_id = self . config . database container_name = self . config . container self . db = self . __get_or_create_database ( self . client , db_id ) self . container = self . __get_or_create_container ( self . client , container_name )


def __get_or_create_database(self, doc_client, id) -> str:
        """Return the database link.

        Check if the database exists or create the db.

        :param doc_client:
        :param id:
        :return str:
        """
        # query CosmosDB for a database with that name/id
        dbs = list(doc_client.QueryDatabases({
            "query": "SELECT * FROM r WHERE r.id=@id",
            "parameters": [
                {"name": "@id", "value": id}
            ]
        }))
        # if there are results, return the first (db names are unique)
        if len(dbs) > 0:
            return dbs[0]['id']
        else:
            # create the database if it didn't exist
            res = doc_client.CreateDatabase({'id': id})
            return res['id']


def __get_or_create_container(self, doc_client, container) -> str:
        """Return the container link.

        Check if the container exists or create the container.

        :param doc_client:
        :param container:
        :return str:
        """
        # query CosmosDB for a container in the database with that name
        containers = list(doc_client.QueryContainers(
            self.__database_link,
            {
                "query": "SELECT * FROM r WHERE r.id=@id",
                "parameters": [
                    {"name": "@id", "value": container}
                ]
            }
        ))
        # if there are results, return the first (container names are unique)
        if len(containers) > 0:
            return containers[0]['id']
        else:
            # Create a container if it didn't exist
            res = doc_client.CreateContainer(
                self.__database_link, {'id': container})
            return res['id']


def fill_qna_event(
        self,
        query_results: [QueryResult],
        turn_context: TurnContext,
        telemetry_properties: Dict[str,str] = None,
        telemetry_metrics: Dict[str,float] = None
    ) -> EventData:
        """
        Fills the event properties and metrics for the QnaMessage event for telemetry.

        :return: A tuple of event data properties and metrics that will be sent to the BotTelemetryClient.track_event() method for the QnAMessage event. The properties and metrics returned the standard properties logged with any properties passed from the get_answers() method.

        :rtype: EventData
        """

        properties: Dict[str,str] = dict()
        metrics: Dict[str, float] = dict()

        properties[QnATelemetryConstants.knowledge_base_id_property] = self._endpoint.knowledge_base_id

        text: str = turn_context.activity.text
        userName: str = turn_context.activity.from_property.name

        # Use the LogPersonalInformation flag to toggle logging PII data; text and username are common examples.
        if self.log_personal_information:
            if text:
                properties[QnATelemetryConstants.question_property] = text

            if userName:
                properties[QnATelemetryConstants.username_property] = userName

        # Fill in Qna Results (found or not).
        if len(query_results) > 0:
            query_result = query_results[0]

            result_properties = {
                QnATelemetryConstants.matched_question_property: json.dumps(query_result.questions),
                QnATelemetryConstants.question_id_property: str(query_result.id),
                QnATelemetryConstants.answer_property: query_result.answer,
                QnATelemetryConstants.score_metric: query_result.score,
                QnATelemetryConstants.article_found_property: 'true'
            }

            properties.update(result_properties)
        else:
            no_match_properties = {
                QnATelemetryConstants.matched_question_property : 'No Qna Question matched',
                QnATelemetryConstants.question_id_property : 'No Qna Question Id matched',
                QnATelemetryConstants.answer_property : 'No Qna Answer matched',
                QnATelemetryConstants.article_found_property : 'false'
            }

            properties.update(no_match_properties)

        # Additional Properties can override "stock" properties.
        if telemetry_properties:
            properties.update(telemetry_properties)

        # Additional Metrics can override "stock" metrics.
        if telemetry_metrics:
            metrics.update(telemetry_metrics)

        return EventData(properties=properties, metrics=metrics)

Fills the event properties and metrics for the QnaMessage event for telemetry .


def get_conversation_reference(activity: Activity) -> ConversationReference:
        """
        Returns the conversation reference for an activity. This can be saved as a plain old JSON
        object and then later used to message the user proactively.

        Usage Example:
        reference = TurnContext.get_conversation_reference(context.request)
        :param activity:
        :return:
        """
        return ConversationReference(activity_id=activity.id,
                                     user=copy(activity.from_property),
                                     bot=copy(activity.recipient),
                                     conversation=copy(activity.conversation),
                                     channel_id=activity.channel_id,
                                     service_url=activity.service_url)

def get_conversation_reference ( activity : Activity ) -> ConversationReference : return ConversationReference ( activity_id = activity . id , user = copy ( activity . from_property ) , bot = copy ( activity . recipient ) , conversation = copy ( activity . conversation ) , channel_id = activity . channel_id , service_url = activity . service_url )

Returns the conversation reference for an activity . This can be saved as a plain old JSON object and then later used to message the user proactively .


def get_step_name(self, index: int) -> str:
        """
        Give the waterfall step a unique name
        """
        step_name = self._steps[index].__qualname__

        if not step_name or ">" in step_name :
            step_name = f"Step{index + 1}of{len(self._steps)}"

        return step_name

def
get_step_name
(
self
,
index
:
int
)
->
str
:
step_name
=
self
.
_steps
[
index
]
.
__qualname__
if
not
step_name
or
">"
in
step_name
:
step_name
=
f"Step{index + 1}of{len(self._steps)}"
return
step_name


def supports_suggested_actions(channel_id: str, button_cnt: int = 100) -> bool:
        """Determine if a number of Suggested Actions are supported by a Channel.

        Args:
            channel_id (str): The Channel to check the if Suggested Actions are supported in.
            button_cnt (int, optional): Defaults to 100. The number of Suggested Actions to check for the Channel.

        Returns:
            bool: True if the Channel supports the button_cnt total Suggested Actions, False if the Channel does not support that number of Suggested Actions.
        """

        max_actions = {
            # https://developers.facebook.com/docs/messenger-platform/send-messages/quick-replies
            Channels.facebook: 10,
            Channels.skype: 10,
            # https://developers.line.biz/en/reference/messaging-api/#items-object
            Channels.line: 13,
            # https://dev.kik.com/#/docs/messaging#text-response-object
            Channels.kik: 20,
            Channels.telegram: 100,
            Channels.slack: 100,
            Channels.emulator: 100,
            Channels.direct_line: 100,
            Channels.webchat: 100,
        }
        return button_cnt <= max_actions[channel_id] if channel_id in max_actions else False

Determine if a number of Suggested Actions are supported by a Channel .


def supports_card_actions(channel_id: str, button_cnt: int = 100) -> bool:
        """Determine if a number of Card Actions are supported by a Channel.

        Args:
            channel_id (str): The Channel to check if the Card Actions are supported in.
            button_cnt (int, optional): Defaults to 100. The number of Card Actions to check for the Channel.

        Returns:
            bool: True if the Channel supports the button_cnt total Card Actions, False if the Channel does not support that number of Card Actions.
        """

        max_actions = {
            Channels.facebook: 3,
            Channels.skype: 3,
            Channels.ms_teams: 3,
            Channels.line: 99,
            Channels.slack: 100,
            Channels.emulator: 100,
            Channels.direct_line: 100,
            Channels.webchat: 100,
            Channels.cortana: 100,
        }
        return button_cnt <= max_actions[channel_id] if channel_id in max_actions else False

def supports_card_actions ( channel_id : str , button_cnt : int = 100 ) -> bool : max_actions = { Channels . facebook : 3 , Channels . skype : 3 , Channels . ms_teams : 3 , Channels . line : 99 , Channels . slack : 100 , Channels . emulator : 100 , Channels . direct_line : 100 , Channels . webchat : 100 , Channels . cortana : 100 , } return button_cnt <= max_actions [ channel_id ] if channel_id in max_actions else False

Determine if a number of Card Actions are supported by a Channel .


def get_channel_id(turn_context: TurnContext) -> str:
        """Get the Channel Id from the current Activity on the Turn Context.

        Args:
            turn_context (TurnContext): The Turn Context to retrieve the Activity's Channel Id from.

        Returns:
            str: The Channel Id from the Turn Context's Activity.
        """

        if turn_context.activity.channel_id is None:
            return ""
        else:
            return turn_context.activity.channel_id

def get_channel_id ( turn_context : TurnContext ) -> str : if turn_context . activity . channel_id is None : return "" else : return turn_context . activity . channel_id

Get the Channel Id from the current Activity on the Turn Context .


def is_token_from_emulator(auth_header: str) -> bool:
        """ Determines if a given Auth header is from the Bot Framework Emulator

        :param auth_header: Bearer Token, in the 'Bearer [Long String]' Format.
        :type auth_header: str

        :return: True, if the token was issued by the Emulator. Otherwise, false.
        """
        # The Auth Header generally looks like this:
        # "Bearer eyJ0e[...Big Long String...]XAiO"
        if not auth_header:
            # No token. Can't be an emulator token.
            return False

        parts = auth_header.split(' ')
        if len(parts) != 2:
            # Emulator tokens MUST have exactly 2 parts.
            # If we don't have 2 parts, it's not an emulator token
            return False

        auth_scheme = parts[0]
        bearer_token = parts[1]

        # We now have an array that should be:
        # [0] = "Bearer"
        # [1] = "[Big Long String]"
        if auth_scheme != 'Bearer':
            # The scheme from the emulator MUST be "Bearer"
            return False

        # Parse the Big Long String into an actual token.
        token = jwt.decode(bearer_token, verify=False)
        if not token:
            return False

        # Is there an Issuer?
        issuer = token['iss']
        if not issuer:
            # No Issuer, means it's not from the Emulator.
            return False

        # Is the token issues by a source we consider to be the emulator?
        issuer_list = EmulatorValidation.TO_BOT_FROM_EMULATOR_TOKEN_VALIDATION_PARAMETERS.issuer
        if issuer_list and not issuer in issuer_list:
            # Not a Valid Issuer. This is NOT a Bot Framework Emulator Token.
            return False

        # The Token is from the Bot Framework Emulator. Success!
        return True


def hero_card(card: HeroCard) -> Attachment:
        """
        Returns an attachment for a hero card. Will raise a TypeError if 'card' argument is not a HeroCard.

        Hero cards tend to have one dominant full width image and the cards text & buttons can
        usually be found below the image.
        :return:
        """
        if not isinstance(card, HeroCard):
            raise TypeError('CardFactory.hero_card(): `card` argument is not an instance of an HeroCard, '
                            'unable to prepare attachment.')

        return Attachment(content_type=CardFactory.content_types.hero_card,
                          content=card)

Returns an attachment for a hero card . Will raise a TypeError if card argument is not a HeroCard .


def params(self):
        """return instruction params"""
        # if params already defined don't attempt to get them from definition
        if self._definition and not self._params:
            self._params = []
            for sub_instr, _, _ in self._definition:
                self._params.extend(sub_instr.params)  # recursive call
            return self._params
        else:
            return self._params


def mirror(self):
        """For a composite instruction, reverse the order of sub-gates.

        This is done by recursively mirroring all sub-instructions.
        It does not invert any gate.

        Returns:
            Instruction: a fresh gate with sub-gates reversed
        """
        if not self._definition:
            return self.copy()

        reverse_inst = self.copy(name=self.name + '_mirror')
        reverse_inst.definition = []
        for inst, qargs, cargs in reversed(self._definition):
            reverse_inst._definition.append((inst.mirror(), qargs, cargs))
        return reverse_inst

def mirror ( self ) : if not self . _definition : return self . copy ( ) reverse_inst = self . copy ( name = self . name + '_mirror' ) reverse_inst . definition = [ ] for inst , qargs , cargs in reversed ( self . _definition ) : reverse_inst . _definition . append ( ( inst . mirror ( ) , qargs , cargs ) ) return reverse_inst

For a composite instruction reverse the order of sub - gates .


def inverse(self):
        """Invert this instruction.

        If the instruction is composite (i.e. has a definition),
        then its definition will be recursively inverted.

        Special instructions inheriting from Instruction can
        implement their own inverse (e.g. T and Tdg, Barrier, etc.)

        Returns:
            Instruction: a fresh instruction for the inverse

        Raises:
            QiskitError: if the instruction is not composite
                and an inverse has not been implemented for it.
        """
        if not self.definition:
            raise QiskitError("inverse() not implemented for %s." % self.name)
        inverse_gate = self.copy(name=self.name + '_dg')
        inverse_gate._definition = []
        for inst, qargs, cargs in reversed(self._definition):
            inverse_gate._definition.append((inst.inverse(), qargs, cargs))
        return inverse_gate

def
inverse
(
self
)
:
if
not
self
.
definition
:
raise
QiskitError
(
"inverse() not implemented for %s."
%
self
.
name
)
inverse_gate
=
self
.
copy
(
name
=
self
.
name
+
'_dg'
)
inverse_gate
.
_definition
=
[
]
for
inst
,
qargs
,
cargs
in
reversed
(
self
.
_definition
)
:
inverse_gate
.
_definition
.
append
(
(
inst
.
inverse
(
)
,
qargs
,
cargs
)
)
return
inverse_gate


def c_if(self, classical, val):
        """Add classical control on register classical and value val."""
        if not isinstance(classical, ClassicalRegister):
            raise QiskitError("c_if must be used with a classical register")
        if val < 0:
            raise QiskitError("control value should be non-negative")
        self.control = (classical, val)
        return self

Add classical control on register classical and value val .


def copy(self, name=None):
        """
        shallow copy of the instruction.

        Args:
          name (str): name to be given to the copied circuit,
            if None then the name stays the same

        Returns:
          Instruction: a shallow copy of the current instruction, with the name
            updated if it was provided
        """
        cpy = copy.copy(self)
        if name:
            cpy.name = name
        return cpy

def copy ( self , name = None ) : cpy = copy . copy ( self ) if name : cpy . name = name return cpy


def _qasmif(self, string):
        """Print an if statement if needed."""
        if self.control is None:
            return string
        return "if(%s==%d) " % (self.control[0].name, self.control[1]) + string

def
_qasmif
(
self
,
string
)
:
if
self
.
control
is
:
return
string
return
"if(%s==%d) "
%
(
self
.
control
[
0
]
.
name
,
self
.
control
[
1
]
)
+
string


def qasm(self):
        """Return a default OpenQASM string for the instruction.

        Derived instructions may override this to print in a
        different format (e.g. measure q[0] -> c[0];).
        """
        name_param = self.name
        if self.params:
            name_param = "%s(%s)" % (name_param, ",".join(
                [str(i) for i in self.params]))

        return self._qasmif(name_param)

def qasm ( self ) : name_param = self . name if self . params : name_param = "%s(%s)" % ( name_param , "," . join ( [ str ( i ) for i in self . params ] ) ) return self . _qasmif ( name_param )

Return a default OpenQASM string for the instruction .


def run(self, circuit):
        """Run all the passes on a QuantumCircuit

        Args:
            circuit (QuantumCircuit): circuit to transform via all the registered passes

        Returns:
            QuantumCircuit: Transformed circuit.
        """
        name = circuit.name
        dag = circuit_to_dag(circuit)
        del circuit
        for passset in self.working_list:
            for pass_ in passset:
                dag = self._do_pass(pass_, dag, passset.options)
        circuit = dag_to_circuit(dag)
        circuit.name = name
        return circuit

def run ( self , circuit ) : name = circuit . name dag = circuit_to_dag ( circuit ) del circuit for passset in self . working_list : for pass_ in passset : dag = self . _do_pass ( pass_ , dag , passset . options ) circuit = dag_to_circuit ( dag ) circuit . name = name return circuit


def _do_pass(self, pass_, dag, options):
        """Do a pass and its "requires".

        Args:
            pass_ (BasePass): Pass to do.
            dag (DAGCircuit): The dag on which the pass is ran.
            options (dict): PassManager options.
        Returns:
            DAGCircuit: The transformed dag in case of a transformation pass.
            The same input dag in case of an analysis pass.
        Raises:
            TranspilerError: If the pass is not a proper pass instance.
        """

        # First, do the requires of pass_
        if not options["ignore_requires"]:
            for required_pass in pass_.requires:
                dag = self._do_pass(required_pass, dag, options)

        # Run the pass itself, if not already run
        if pass_ not in self.valid_passes:
            if pass_.is_transformation_pass:
                pass_.property_set = self.fenced_property_set
                new_dag = pass_.run(dag)
                if not isinstance(new_dag, DAGCircuit):
                    raise TranspilerError("Transformation passes should return a transformed dag."
                                          "The pass %s is returning a %s" % (type(pass_).__name__,
                                                                             type(new_dag)))
                dag = new_dag
            elif pass_.is_analysis_pass:
                pass_.property_set = self.property_set
                pass_.run(FencedDAGCircuit(dag))
            else:
                raise TranspilerError("I dont know how to handle this type of pass")

            # update the valid_passes property
            self._update_valid_passes(pass_, options['ignore_preserves'])

        return dag


def passes(self):
        """
        Returns a list structure of the appended passes and its options.

        Returns (list): The appended passes.
        """
        ret = []
        for pass_ in self.working_list:
            ret.append(pass_.dump_passes())
        return ret

def passes ( self ) : ret = [ ] for pass_ in self . working_list : ret . append ( pass_ . dump_passes ( ) ) return ret

Returns a list structure of the appended passes and its options .


def dump_passes(self):
        """
        Fetches the passes added to this flow controller.

        Returns (dict): {'options': self.options, 'passes': [passes], 'type': type(self)}
        """
        ret = {'options': self.options, 'passes': [], 'type': type(self)}
        for pass_ in self._passes:
            if isinstance(pass_, FlowController):
                ret['passes'].append(pass_.dump_passes())
            else:
                ret['passes'].append(pass_)
        return ret

def dump_passes ( self ) : ret = { 'options' : self . options , 'passes' : [ ] , 'type' : type ( self ) } for pass_ in self . _passes : if isinstance ( pass_ , FlowController ) : ret [ 'passes' ] . append ( pass_ . dump_passes ( ) ) else : ret [ 'passes' ] . append ( pass_ ) return ret

Fetches the passes added to this flow controller .


def controller_factory(cls, passes, options, **partial_controller):
        """
        Constructs a flow controller based on the partially evaluated controller arguments.

        Args:
            passes (list[BasePass]): passes to add to the flow controller.
            options (dict): PassManager options.
            **partial_controller (dict): Partially evaluated controller arguments in the form
                `{name:partial}`

        Raises:
            TranspilerError: When partial_controller is not well-formed.

        Returns:
            FlowController: A FlowController instance.
        """
        if None in partial_controller.values():
            raise TranspilerError('The controller needs a condition.')

        if partial_controller:
            for registered_controller in cls.registered_controllers.keys():
                if registered_controller in partial_controller:
                    return cls.registered_controllers[registered_controller](passes, options,
                                                                             **partial_controller)
            raise TranspilerError("The controllers for %s are not registered" % partial_controller)
        else:
            return FlowControllerLinear(passes, options)

Constructs a flow controller based on the partially evaluated controller arguments .


def u_base(self, theta, phi, lam, q):
    """Apply U to q."""
    return self.append(UBase(theta, phi, lam), [q], [])

def u_base ( self , theta , phi , lam , q ) : return self . append ( UBase ( theta , phi , lam ) , [ q ] , [ ] )


def single_gate_params(gate, params=None):
    """Apply a single qubit gate to the qubit.

    Args:
        gate(str): the single qubit gate name
        params(list): the operation parameters op['params']
    Returns:
        tuple: a tuple of U gate parameters (theta, phi, lam)
    Raises:
        QiskitError: if the gate name is not valid
    """
    if gate in ('U', 'u3'):
        return params[0], params[1], params[2]
    elif gate == 'u2':
        return np.pi / 2, params[0], params[1]
    elif gate == 'u1':
        return 0, 0, params[0]
    elif gate == 'id':
        return 0, 0, 0
    raise QiskitError('Gate is not among the valid types: %s' % gate)

def
single_gate_params
(
gate
,
params
=
)
:
if
gate
in
(
'U'
,
'u3'
)
:
return
params
[
0
]
,
params
[
1
]
,
params
[
2
]
elif
gate
==
'u2'
:
return
np
.
pi
/
2
,
params
[
0
]
,
params
[
1
]
elif
gate
==
'u1'
:
return
0
,
0
,
params
[
0
]
elif
gate
==
'id'
:
return
0
,
0
,
0
raise
QiskitError
(
'Gate is not among the valid types: %s'
%
gate
)

Apply a single qubit gate to the qubit .


def single_gate_matrix(gate, params=None):
    """Get the matrix for a single qubit.

    Args:
        gate(str): the single qubit gate name
        params(list): the operation parameters op['params']
    Returns:
        array: A numpy array representing the matrix
    """

    # Converting sym to floats improves the performance of the simulator 10x.
    # This a is a probable a FIXME since it might show bugs in the simulator.
    (theta, phi, lam) = map(float, single_gate_params(gate, params))

    return np.array([[np.cos(theta / 2),
                      -np.exp(1j * lam) * np.sin(theta / 2)],
                     [np.exp(1j * phi) * np.sin(theta / 2),
                      np.exp(1j * phi + 1j * lam) * np.cos(theta / 2)]])


def einsum_matmul_index(gate_indices, number_of_qubits):
    """Return the index string for Numpy.eignsum matrix-matrix multiplication.

    The returned indices are to perform a matrix multiplication A.B where
    the matrix A is an M-qubit matrix, matrix B is an N-qubit matrix, and
    M <= N, and identity matrices are implied on the subsystems where A has no
    support on B.

    Args:
        gate_indices (list[int]): the indices of the right matrix subsystems
                                   to contract with the left matrix.
        number_of_qubits (int): the total number of qubits for the right matrix.

    Returns:
        str: An indices string for the Numpy.einsum function.
    """

    mat_l, mat_r, tens_lin, tens_lout = _einsum_matmul_index_helper(gate_indices,
                                                                    number_of_qubits)

    # Right indices for the N-qubit input and output tensor
    tens_r = ascii_uppercase[:number_of_qubits]

    # Combine indices into matrix multiplication string format
    # for numpy.einsum function
    return "{mat_l}{mat_r}, ".format(mat_l=mat_l, mat_r=mat_r) + \
           "{tens_lin}{tens_r}->{tens_lout}{tens_r}".format(tens_lin=tens_lin,
                                                            tens_lout=tens_lout,
                                                            tens_r=tens_r)

Return the index string for Numpy . eignsum matrix - matrix multiplication .


def einsum_vecmul_index(gate_indices, number_of_qubits):
    """Return the index string for Numpy.eignsum matrix-vector multiplication.

    The returned indices are to perform a matrix multiplication A.v where
    the matrix A is an M-qubit matrix, vector v is an N-qubit vector, and
    M <= N, and identity matrices are implied on the subsystems where A has no
    support on v.

    Args:
        gate_indices (list[int]): the indices of the right matrix subsystems
                                  to contract with the left matrix.
        number_of_qubits (int): the total number of qubits for the right matrix.

    Returns:
        str: An indices string for the Numpy.einsum function.
    """

    mat_l, mat_r, tens_lin, tens_lout = _einsum_matmul_index_helper(gate_indices,
                                                                    number_of_qubits)

    # Combine indices into matrix multiplication string format
    # for numpy.einsum function
    return "{mat_l}{mat_r}, ".format(mat_l=mat_l, mat_r=mat_r) + \
           "{tens_lin}->{tens_lout}".format(tens_lin=tens_lin,
                                            tens_lout=tens_lout)

Return the index string for Numpy . eignsum matrix - vector multiplication .


def _einsum_matmul_index_helper(gate_indices, number_of_qubits):
    """Return the index string for Numpy.eignsum matrix multiplication.

    The returned indices are to perform a matrix multiplication A.v where
    the matrix A is an M-qubit matrix, matrix v is an N-qubit vector, and
    M <= N, and identity matrices are implied on the subsystems where A has no
    support on v.

    Args:
        gate_indices (list[int]): the indices of the right matrix subsystems
                                   to contract with the left matrix.
        number_of_qubits (int): the total number of qubits for the right matrix.

    Returns:
        tuple: (mat_left, mat_right, tens_in, tens_out) of index strings for
        that may be combined into a Numpy.einsum function string.

    Raises:
        QiskitError: if the total number of qubits plus the number of
        contracted indices is greater than 26.
    """

    # Since we use ASCII alphabet for einsum index labels we are limited
    # to 26 total free left (lowercase) and 26 right (uppercase) indexes.
    # The rank of the contracted tensor reduces this as we need to use that
    # many characters for the contracted indices
    if len(gate_indices) + number_of_qubits > 26:
        raise QiskitError("Total number of free indexes limited to 26")

    # Indicies for N-qubit input tensor
    tens_in = ascii_lowercase[:number_of_qubits]

    # Indices for the N-qubit output tensor
    tens_out = list(tens_in)

    # Left and right indices for the M-qubit multiplying tensor
    mat_left = ""
    mat_right = ""

    # Update left indices for mat and output
    for pos, idx in enumerate(reversed(gate_indices)):
        mat_left += ascii_lowercase[-1 - pos]
        mat_right += tens_in[-1 - idx]
        tens_out[-1 - idx] = ascii_lowercase[-1 - pos]
    tens_out = "".join(tens_out)

    # Combine indices into matrix multiplication string format
    # for numpy.einsum function
    return mat_left, mat_right, tens_in, tens_out

Return the index string for Numpy . eignsum matrix multiplication .


def circuit_to_dag(circuit):
    """Build a ``DAGCircuit`` object from a ``QuantumCircuit``.

    Args:
        circuit (QuantumCircuit): the input circuit.

    Return:
        DAGCircuit: the DAG representing the input circuit.
    """
    dagcircuit = DAGCircuit()
    dagcircuit.name = circuit.name
    for register in circuit.qregs:
        dagcircuit.add_qreg(register)
    for register in circuit.cregs:
        dagcircuit.add_creg(register)

    for instruction, qargs, cargs in circuit.data:
        # Get arguments for classical control (if any)
        if instruction.control is None:
            control = None
        else:
            control = (instruction.control[0], instruction.control[1])

        dagcircuit.apply_operation_back(instruction.copy(),
                                        qargs, cargs, control)

    return dagcircuit

Build a DAGCircuit object from a QuantumCircuit .


def exp_fit_fun(x, a, tau, c):
    """Function used to fit the exponential decay."""
    # pylint: disable=invalid-name
    return a * np.exp(-x / tau) + c

def
exp_fit_fun
(
x
,
a
,
tau
,
c
)
:
# pylint: disable=invalid-name
return
a
*
np
.
exp
(
x
/
tau
)
+
c

Function used to fit the exponential decay .


def osc_fit_fun(x, a, tau, f, phi, c):
    """Function used to fit the decay cosine."""
    # pylint: disable=invalid-name
    return a * np.exp(-x / tau) * np.cos(2 * np.pi * f * x + phi) + c

def
osc_fit_fun
(
x
,
a
,
tau
,
f
,
phi
,
c
)
:
# pylint: disable=invalid-name
return
a
*
np
.
exp
(
x
/
tau
)
*
np
.
cos
(
2
*
np
.
pi
*
f
*
x
+
phi
)
+
c


def plot_coherence(xdata, ydata, std_error, fit, fit_function, xunit, exp_str,
                   qubit_label):
    """Plot coherence data.

    Args:
        xdata
        ydata
        std_error
        fit
        fit_function
        xunit
        exp_str
        qubit_label
    Raises:
        ImportError: If matplotlib is not installed.
    """
    if not HAS_MATPLOTLIB:
        raise ImportError('The function plot_coherence needs matplotlib. '
                          'Run "pip install matplotlib" before.')
    plt.errorbar(xdata, ydata, std_error, marker='.',
                 markersize=9, c='b', linestyle='')
    plt.plot(xdata, fit_function(xdata, *fit), c='r', linestyle='--',
             label=(exp_str + '= %s %s' % (str(round(fit[1])), xunit)))
    plt.xticks(fontsize=14, rotation=70)
    plt.yticks(fontsize=14)
    plt.xlabel('time [%s]' % (xunit), fontsize=16)
    plt.ylabel('P(1)', fontsize=16)
    plt.title(exp_str + ' measurement of Q$_{%s}$' % (str(qubit_label)), fontsize=18)
    plt.legend(fontsize=12)
    plt.grid(True)
    plt.show()


def shape_rb_data(raw_rb):
    """Take the raw rb data and convert it into averages and std dev

    Args:
        raw_rb (numpy.array): m x n x l list where m is the number of seeds, n
            is the number of Clifford sequences and l is the number of qubits

    Return:
        numpy_array: 2 x n x l list where index 0 is the mean over seeds, 1 is
            the std dev overseeds
    """
    rb_data = []
    rb_data.append(np.mean(raw_rb, 0))
    rb_data.append(np.std(raw_rb, 0))

    return rb_data

def shape_rb_data ( raw_rb ) : rb_data = [ ] rb_data . append ( np . mean ( raw_rb , 0 ) ) rb_data . append ( np . std ( raw_rb , 0 ) ) return rb_data


def plot_rb_data(xdata, ydatas, yavg, yerr, fit, survival_prob, ax=None,
                 show_plt=True):
    """Plot randomized benchmarking data.

    Args:
        xdata (list): list of subsequence lengths
        ydatas (list): list of lists of survival probabilities for each
            sequence
        yavg (list): mean of the survival probabilities at each sequence
            length
        yerr (list): error of the survival
        fit (list): fit parameters
        survival_prob (callable): function that computes survival probability
        ax (Axes or None): plot axis (if passed in)
        show_plt (bool): display the plot.

    Raises:
        ImportError: If matplotlib is not installed.
    """
    # pylint: disable=invalid-name

    if not HAS_MATPLOTLIB:
        raise ImportError('The function plot_rb_data needs matplotlib. '
                          'Run "pip install matplotlib" before.')
    if ax is None:
        plt.figure()
        ax = plt.gca()

    # Plot the result for each sequence
    for ydata in ydatas:
        ax.plot(xdata, ydata, color='gray', linestyle='none', marker='x')
    # Plot the mean with error bars
    ax.errorbar(xdata, yavg, yerr=yerr, color='r', linestyle='--', linewidth=3)

    # Plot the fit
    ax.plot(xdata, survival_prob(xdata, *fit), color='blue', linestyle='-', linewidth=2)
    ax.tick_params(labelsize=14)
    # ax.tick_params(axis='x',labelrotation=70)

    ax.set_xlabel('Clifford Length', fontsize=16)
    ax.set_ylabel('Z', fontsize=16)
    ax.grid(True)

    if show_plt:
        plt.show()


def _split_runs_on_parameters(runs):
    """Finds runs containing parameterized gates and splits them into sequential
    runs excluding the parameterized gates.
    """

    def _is_dagnode_parameterized(node):
        return any(isinstance(param, Parameter) for param in node.op.params)

    out = []
    for run in runs:
        groups = groupby(run, _is_dagnode_parameterized)

        for group_is_parameterized, gates in groups:
            if not group_is_parameterized:
                out.append(list(gates))

    return out

def _split_runs_on_parameters ( runs ) : def _is_dagnode_parameterized ( node ) : return any ( isinstance ( param , Parameter ) for param in node . op . params ) out = [ ] for run in runs : groups = groupby ( run , _is_dagnode_parameterized ) for group_is_parameterized , gates in groups : if not group_is_parameterized : out . append ( list ( gates ) ) return out

Finds runs containing parameterized gates and splits them into sequential runs excluding the parameterized gates .


def compose_u3(theta1, phi1, lambda1, theta2, phi2, lambda2):
        """Return a triple theta, phi, lambda for the product.

        u3(theta, phi, lambda)
           = u3(theta1, phi1, lambda1).u3(theta2, phi2, lambda2)
           = Rz(phi1).Ry(theta1).Rz(lambda1+phi2).Ry(theta2).Rz(lambda2)
           = Rz(phi1).Rz(phi').Ry(theta').Rz(lambda').Rz(lambda2)
           = u3(theta', phi1 + phi', lambda2 + lambda')

        Return theta, phi, lambda.
        """
        # Careful with the factor of two in yzy_to_zyz
        thetap, phip, lambdap = Optimize1qGates.yzy_to_zyz((lambda1 + phi2), theta1, theta2)
        (theta, phi, lamb) = (thetap, phi1 + phip, lambda2 + lambdap)

        return (theta, phi, lamb)

Return a triple theta phi lambda for the product .


def yzy_to_zyz(xi, theta1, theta2, eps=1e-9):  # pylint: disable=invalid-name
        """Express a Y.Z.Y single qubit gate as a Z.Y.Z gate.

        Solve the equation

        .. math::

        Ry(theta1).Rz(xi).Ry(theta2) = Rz(phi).Ry(theta).Rz(lambda)

        for theta, phi, and lambda.

        Return a solution theta, phi, and lambda.
        """
        quaternion_yzy = quaternion_from_euler([theta1, xi, theta2], 'yzy')
        euler = quaternion_yzy.to_zyz()
        quaternion_zyz = quaternion_from_euler(euler, 'zyz')
        # output order different than rotation order
        out_angles = (euler[1], euler[0], euler[2])
        abs_inner = abs(quaternion_zyz.data.dot(quaternion_yzy.data))
        if not np.allclose(abs_inner, 1, eps):
            raise TranspilerError('YZY and ZYZ angles do not give same rotation matrix.')
        out_angles = tuple(0 if np.abs(angle) < _CHOP_THRESHOLD else angle
                           for angle in out_angles)
        return out_angles

Express a Y . Z . Y single qubit gate as a Z . Y . Z gate .


def _validate_input_state(quantum_state):
    """Validates the input to state visualization functions.

    Args:
        quantum_state (ndarray): Input state / density matrix.
    Returns:
        rho: A 2d numpy array for the density matrix.
    Raises:
        VisualizationError: Invalid input.
    """
    rho = np.asarray(quantum_state)
    if rho.ndim == 1:
        rho = np.outer(rho, np.conj(rho))
    # Check the shape of the input is a square matrix
    shape = np.shape(rho)
    if len(shape) != 2 or shape[0] != shape[1]:
        raise VisualizationError("Input is not a valid quantum state.")
    # Check state is an n-qubit state
    num = int(np.log2(rho.shape[0]))
    if 2 ** num != rho.shape[0]:
        raise VisualizationError("Input is not a multi-qubit quantum state.")
    return rho

Validates the input to state visualization functions .


def _trim(image):
    """Trim a PIL image and remove white space."""
    background = PIL.Image.new(image.mode, image.size, image.getpixel((0, 0)))
    diff = PIL.ImageChops.difference(image, background)
    diff = PIL.ImageChops.add(diff, diff, 2.0, -100)
    bbox = diff.getbbox()
    if bbox:
        image = image.crop(bbox)
    return image

def _trim ( image ) : background = PIL . Image . new ( image . mode , image . size , image . getpixel ( ( 0 , 0 ) ) ) diff = PIL . ImageChops . difference ( image , background ) diff = PIL . ImageChops . add ( diff , diff , 2.0 , - 100 ) bbox = diff . getbbox ( ) if bbox : image = image . crop ( bbox ) return image

Trim a PIL image and remove white space .


def _get_gate_span(qregs, instruction):
    """Get the list of qubits drawing this gate would cover"""

    min_index = len(qregs)
    max_index = 0
    for qreg in instruction.qargs:
        index = qregs.index(qreg)

        if index < min_index:
            min_index = index
        if index > max_index:
            max_index = index

    if instruction.cargs:
        return qregs[min_index:]

    return qregs[min_index:max_index + 1]

def _get_gate_span ( qregs , instruction ) : min_index = len ( qregs ) max_index = 0 for qreg in instruction . qargs : index = qregs . index ( qreg ) if index < min_index : min_index = index if index > max_index : max_index = index if instruction . cargs : return qregs [ min_index : ] return qregs [ min_index : max_index + 1 ]


def circuit_to_instruction(circuit):
    """Build an ``Instruction`` object from a ``QuantumCircuit``.

    The instruction is anonymous (not tied to a named quantum register),
    and so can be inserted into another circuit. The instruction will
    have the same string name as the circuit.

    Args:
        circuit (QuantumCircuit): the input circuit.

    Return:
        Instruction: an instruction equivalent to the action of the
            input circuit. Upon decomposition, this instruction will
            yield the components comprising the original circuit.
    """
    instruction = Instruction(name=circuit.name,
                              num_qubits=sum([qreg.size for qreg in circuit.qregs]),
                              num_clbits=sum([creg.size for creg in circuit.cregs]),
                              params=[])
    instruction.control = None

    def find_bit_position(bit):
        """find the index of a given bit (Register, int) within
        a flat ordered list of bits of the circuit
        """
        if isinstance(bit[0], QuantumRegister):
            ordered_regs = circuit.qregs
        else:
            ordered_regs = circuit.cregs
        reg_index = ordered_regs.index(bit[0])
        return sum([reg.size for reg in ordered_regs[:reg_index]]) + bit[1]

    definition = circuit.data.copy()

    if instruction.num_qubits > 0:
        q = QuantumRegister(instruction.num_qubits, 'q')
    if instruction.num_clbits > 0:
        c = ClassicalRegister(instruction.num_clbits, 'c')

    definition = list(map(lambda x:
                          (x[0],
                           list(map(lambda y: (q, find_bit_position(y)), x[1])),
                           list(map(lambda y: (c, find_bit_position(y)), x[2]))), definition))
    instruction.definition = definition

    return instruction

Build an Instruction object from a QuantumCircuit .


def run(self, dag):
        """
        Pick a convenient layout depending on the best matching
        qubit connectivity, and set the property `layout`.

        Args:
            dag (DAGCircuit): DAG to find layout for.

        Raises:
            TranspilerError: if dag wider than self.coupling_map
        """
        num_dag_qubits = sum([qreg.size for qreg in dag.qregs.values()])
        if num_dag_qubits > self.coupling_map.size():
            raise TranspilerError('Number of qubits greater than device.')
        best_sub = self._best_subset(num_dag_qubits)
        layout = Layout()
        map_iter = 0
        for qreg in dag.qregs.values():
            for i in range(qreg.size):
                layout[(qreg, i)] = int(best_sub[map_iter])
                map_iter += 1
        self.property_set['layout'] = layout

def
run
(
self
,
dag
)
:
num_dag_qubits
=
sum
(
[
qreg
.
size
for
qreg
in
dag
.
qregs
.
values
(
)
]
)
if
num_dag_qubits
>
self
.
coupling_map
.
size
(
)
:
raise
TranspilerError
(
'Number of qubits greater than device.'
)
best_sub
=
self
.
_best_subset
(
num_dag_qubits
)
layout
=
Layout
(
)
map_iter
=
0
for
qreg
in
dag
.
qregs
.
values
(
)
:
for
i
in
range
(
qreg
.
size
)
:
layout
[
(
qreg
,
i
)
]
=
int
(
best_sub
[
map_iter
]
)
map_iter
+=
1
self
.
property_set
[
'layout'
]
=
layout

Pick a convenient layout depending on the best matching qubit connectivity and set the property layout .


def _best_subset(self, n_qubits):
        """Computes the qubit mapping with the best connectivity.

        Args:
            n_qubits (int): Number of subset qubits to consider.

        Returns:
            ndarray: Array of qubits to use for best connectivity mapping.
        """
        if n_qubits == 1:
            return np.array([0])

        device_qubits = self.coupling_map.size()

        cmap = np.asarray(self.coupling_map.get_edges())
        data = np.ones_like(cmap[:, 0])
        sp_cmap = sp.coo_matrix((data, (cmap[:, 0], cmap[:, 1])),
                                shape=(device_qubits, device_qubits)).tocsr()
        best = 0
        best_map = None
        # do bfs with each node as starting point
        for k in range(sp_cmap.shape[0]):
            bfs = cs.breadth_first_order(sp_cmap, i_start=k, directed=False,
                                         return_predecessors=False)

            connection_count = 0
            sub_graph = []
            for i in range(n_qubits):
                node_idx = bfs[i]
                for j in range(sp_cmap.indptr[node_idx],
                               sp_cmap.indptr[node_idx + 1]):
                    node = sp_cmap.indices[j]
                    for counter in range(n_qubits):
                        if node == bfs[counter]:
                            connection_count += 1
                            sub_graph.append([node_idx, node])
                            break

            if connection_count > best:
                best = connection_count
                best_map = bfs[0:n_qubits]
                # Return a best mapping that has reduced bandwidth
                mapping = {}
                for edge in range(best_map.shape[0]):
                    mapping[best_map[edge]] = edge
                new_cmap = [[mapping[c[0]], mapping[c[1]]] for c in sub_graph]
                rows = [edge[0] for edge in new_cmap]
                cols = [edge[1] for edge in new_cmap]
                data = [1]*len(rows)
                sp_sub_graph = sp.coo_matrix((data, (rows, cols)),
                                             shape=(n_qubits, n_qubits)).tocsr()
                perm = cs.reverse_cuthill_mckee(sp_sub_graph)
                best_map = best_map[perm]
        return best_map

Computes the qubit mapping with the best connectivity .


def barrier(self, *qargs):
    """Apply barrier to circuit.
    If qargs is None, applies to all the qbits.
    Args is a list of QuantumRegister or single qubits.
    For QuantumRegister, applies barrier to all the qubits in that register."""
    qubits = []

    qargs = _convert_to_bits(qargs, [qbit for qreg in self.qregs for qbit in qreg])

    if not qargs:  # None
        for qreg in self.qregs:
            for j in range(qreg.size):
                qubits.append((qreg, j))

    for qarg in qargs:
        if isinstance(qarg, (QuantumRegister, list)):
            if isinstance(qarg, QuantumRegister):
                qubits.extend([(qarg, j) for j in range(qarg.size)])
            else:
                qubits.extend(qarg)
        else:
            qubits.append(qarg)

    return self.append(Barrier(len(qubits)), qubits, [])

def
barrier
(
self
,
*
qargs
)
:
qubits
=
[
]
qargs
=
_convert_to_bits
(
qargs
,
[
qbit
for
qreg
in
self
.
qregs
for
qbit
in
qreg
]
)
if
not
qargs
:
# None
for
qreg
in
self
.
qregs
:
for
j
in
range
(
qreg
.
size
)
:
qubits
.
append
(
(
qreg
,
j
)
)
for
qarg
in
qargs
:
if
isinstance
(
qarg
,
(
QuantumRegister
,
list
)
)
:
if
isinstance
(
qarg
,
QuantumRegister
)
:
qubits
.
extend
(
[
(
qarg
,
j
)
for
j
in
range
(
qarg
.
size
)
]
)
else
:
qubits
.
extend
(
qarg
)
else
:
qubits
.
append
(
qarg
)
return
self
.
append
(
Barrier
(
len
(
qubits
)
)
,
qubits
,
[
]
)

Apply barrier to circuit . If qargs is None applies to all the qbits . Args is a list of QuantumRegister or single qubits . For QuantumRegister applies barrier to all the qubits in that register .


def average_data(counts, observable):
    """Compute the mean value of an diagonal observable.

    Takes in a diagonal observable in dictionary, list or matrix format and then
    calculates the sum_i value(i) P(i) where value(i) is the value of the
    observable for state i.

    Args:
        counts (dict): a dict of outcomes from an experiment
        observable (dict or matrix or list): The observable to be averaged over.
        As an example, ZZ on qubits can be given as:
        * dict: {"00": 1, "11": 1, "01": -1, "10": -1}
        * matrix: [[1, 0, 0, 0], [0, -1, 0, 0, ], [0, 0, -1, 0], [0, 0, 0, 1]]
        * matrix diagonal (list): [1, -1, -1, 1]

    Returns:
        Double: Average of the observable
    """
    if not isinstance(observable, dict):
        observable = make_dict_observable(observable)
    temp = 0
    tot = sum(counts.values())
    for key in counts:
        if key in observable:
            temp += counts[key] * observable[key] / tot
    return temp

def average_data ( counts , observable ) : if not isinstance ( observable , dict ) : observable = make_dict_observable ( observable ) temp = 0 tot = sum ( counts . values ( ) ) for key in counts : if key in observable : temp += counts [ key ] * observable [ key ] / tot return temp

Compute the mean value of an diagonal observable .


def _process_bit_id(self, node):
        """Process an Id or IndexedId node as a bit or register type.

        Return a list of tuples (Register,index).
        """
        # pylint: disable=inconsistent-return-statements
        reg = None
        if node.name in self.dag.qregs:
            reg = self.dag.qregs[node.name]
        elif node.name in self.dag.cregs:
            reg = self.dag.cregs[node.name]
        else:
            raise QiskitError("expected qreg or creg name:",
                              "line=%s" % node.line,
                              "file=%s" % node.file)

        if node.type == "indexed_id":
            # An indexed bit or qubit
            return [(reg, node.index)]
        elif node.type == "id":
            # A qubit or qreg or creg
            if not self.bit_stack[-1]:
                # Global scope
                return [(reg, j) for j in range(reg.size)]
            else:
                # local scope
                if node.name in self.bit_stack[-1]:
                    return [self.bit_stack[-1][node.name]]
                raise QiskitError("expected local bit name:",
                                  "line=%s" % node.line,
                                  "file=%s" % node.file)
        return None

Process an Id or IndexedId node as a bit or register type .


def _process_custom_unitary(self, node):
        """Process a custom unitary node."""
        name = node.name
        if node.arguments is not None:
            args = self._process_node(node.arguments)
        else:
            args = []
        bits = [self._process_bit_id(node_element)
                for node_element in node.bitlist.children]
        if name in self.gates:
            gargs = self.gates[name]["args"]
            gbits = self.gates[name]["bits"]
            # Loop over register arguments, if any.
            maxidx = max(map(len, bits))
            for idx in range(maxidx):
                self.arg_stack.append({gargs[j]: args[j]
                                       for j in range(len(gargs))})
                # Only index into register arguments.
                element = [idx*x for x in
                           [len(bits[j]) > 1 for j in range(len(bits))]]
                self.bit_stack.append({gbits[j]: bits[j][element[j]]
                                       for j in range(len(gbits))})
                self._create_dag_op(name,
                                    [self.arg_stack[-1][s].sym() for s in gargs],
                                    [self.bit_stack[-1][s] for s in gbits])
                self.arg_stack.pop()
                self.bit_stack.pop()
        else:
            raise QiskitError("internal error undefined gate:",
                              "line=%s" % node.line, "file=%s" % node.file)

def
_process_custom_unitary
(
self
,
node
)
:
name
=
node
.
name
if
node
.
arguments
is
not
:
args
=
self
.
_process_node
(
node
.
arguments
)
else
:
args
=
[
]
bits
=
[
self
.
_process_bit_id
(
node_element
)
for
node_element
in
node
.
bitlist
.
children
]
if
name
in
self
.
gates
:
gargs
=
self
.
gates
[
name
]
[
"args"
]
gbits
=
self
.
gates
[
name
]
[
"bits"
]
# Loop over register arguments, if any.
maxidx
=
max
(
map
(
len
,
bits
)
)
for
idx
in
range
(
maxidx
)
:
self
.
arg_stack
.
append
(
{
gargs
[
j
]
:
args
[
j
]
for
j
in
range
(
len
(
gargs
)
)
}
)
# Only index into register arguments.
element
=
[
idx
*
x
for
x
in
[
len
(
bits
[
j
]
)
>
1
for
j
in
range
(
len
(
bits
)
)
]
]
self
.
bit_stack
.
append
(
{
gbits
[
j
]
:
bits
[
j
]
[
element
[
j
]
]
for
j
in
range
(
len
(
gbits
)
)
}
)
self
.
_create_dag_op
(
name
,
[
self
.
arg_stack
[
1
]
[
s
]
.
sym
(
)
for
s
in
gargs
]
,
[
self
.
bit_stack
[
1
]
[
s
]
for
s
in
gbits
]
)
self
.
arg_stack
.
pop
(
)
self
.
bit_stack
.
pop
(
)
else
:
raise
QiskitError
(
"internal error undefined gate:"
,
"line=%s"
%
node
.
line
,
"file=%s"
%
node
.
file
)


def _process_gate(self, node, opaque=False):
        """Process a gate node.

        If opaque is True, process the node as an opaque gate node.
        """
        self.gates[node.name] = {}
        de_gate = self.gates[node.name]
        de_gate["print"] = True  # default
        de_gate["opaque"] = opaque
        de_gate["n_args"] = node.n_args()
        de_gate["n_bits"] = node.n_bits()
        if node.n_args() > 0:
            de_gate["args"] = [element.name for element in node.arguments.children]
        else:
            de_gate["args"] = []
        de_gate["bits"] = [c.name for c in node.bitlist.children]
        if opaque:
            de_gate["body"] = None
        else:
            de_gate["body"] = node.body

def
_process_gate
(
self
,
node
,
opaque
=
False
)
:
self
.
gates
[
node
.
name
]
=
{
}
de_gate
=
self
.
gates
[
node
.
name
]
de_gate
[
"print"
]
=
True
# default
de_gate
[
"opaque"
]
=
opaque
de_gate
[
"n_args"
]
=
node
.
n_args
(
)
de_gate
[
"n_bits"
]
=
node
.
n_bits
(
)
if
node
.
n_args
(
)
>
0
:
de_gate
[
"args"
]
=
[
element
.
name
for
element
in
node
.
arguments
.
children
]
else
:
de_gate
[
"args"
]
=
[
]
de_gate
[
"bits"
]
=
[
c
.
name
for
c
in
node
.
bitlist
.
children
]
if
opaque
:
de_gate
[
"body"
]
=
else
:
de_gate
[
"body"
]
=
node
.
body


def _process_cnot(self, node):
        """Process a CNOT gate node."""
        id0 = self._process_bit_id(node.children[0])
        id1 = self._process_bit_id(node.children[1])
        if not(len(id0) == len(id1) or len(id0) == 1 or len(id1) == 1):
            raise QiskitError("internal error: qreg size mismatch",
                              "line=%s" % node.line, "file=%s" % node.file)
        maxidx = max([len(id0), len(id1)])
        for idx in range(maxidx):
            if len(id0) > 1 and len(id1) > 1:
                self.dag.apply_operation_back(CXBase(), [id0[idx], id1[idx]], [], self.condition)
            elif len(id0) > 1:
                self.dag.apply_operation_back(CXBase(), [id0[idx], id1[0]], [], self.condition)
            else:
                self.dag.apply_operation_back(CXBase(), [id0[0], id1[idx]], [], self.condition)

def
_process_cnot
(
self
,
node
)
:
id0
=
self
.
_process_bit_id
(
node
.
children
[
0
]
)
id1
=
self
.
_process_bit_id
(
node
.
children
[
1
]
)
if
not
(
len
(
id0
)
==
len
(
id1
)
or
len
(
id0
)
==
1
or
len
(
id1
)
==
1
)
:
raise
QiskitError
(
"internal error: qreg size mismatch"
,
"line=%s"
%
node
.
line
,
"file=%s"
%
node
.
file
)
maxidx
=
max
(
[
len
(
id0
)
,
len
(
id1
)
]
)
for
idx
in
range
(
maxidx
)
:
if
len
(
id0
)
>
1
and
len
(
id1
)
>
1
:
self
.
dag
.
apply_operation_back
(
CXBase
(
)
,
[
id0
[
idx
]
,
id1
[
idx
]
]
,
[
]
,
self
.
condition
)
elif
len
(
id0
)
>
1
:
self
.
dag
.
apply_operation_back
(
CXBase
(
)
,
[
id0
[
idx
]
,
id1
[
0
]
]
,
[
]
,
self
.
condition
)
else
:
self
.
dag
.
apply_operation_back
(
CXBase
(
)
,
[
id0
[
0
]
,
id1
[
idx
]
]
,
[
]
,
self
.
condition
)


def _process_measure(self, node):
        """Process a measurement node."""
        id0 = self._process_bit_id(node.children[0])
        id1 = self._process_bit_id(node.children[1])
        if len(id0) != len(id1):
            raise QiskitError("internal error: reg size mismatch",
                              "line=%s" % node.line, "file=%s" % node.file)
        for idx, idy in zip(id0, id1):
            self.dag.apply_operation_back(Measure(), [idx], [idy], self.condition)

def
_process_measure
(
self
,
node
)
:
id0
=
self
.
_process_bit_id
(
node
.
children
[
0
]
)
id1
=
self
.
_process_bit_id
(
node
.
children
[
1
]
)
if
len
(
id0
)
!=
len
(
id1
)
:
raise
QiskitError
(
"internal error: reg size mismatch"
,
"line=%s"
%
node
.
line
,
"file=%s"
%
node
.
file
)
for
idx
,
idy
in
zip
(
id0
,
id1
)
:
self
.
dag
.
apply_operation_back
(
Measure
(
)
,
[
idx
]
,
[
idy
]
,
self
.
condition
)


def _process_if(self, node):
        """Process an if node."""
        creg_name = node.children[0].name
        creg = self.dag.cregs[creg_name]
        cval = node.children[1].value
        self.condition = (creg, cval)
        self._process_node(node.children[2])
        self.condition = None

def _process_if ( self , node ) : creg_name = node . children [ 0 ] . name creg = self . dag . cregs [ creg_name ] cval = node . children [ 1 ] . value self . condition = ( creg , cval ) self . _process_node ( node . children [ 2 ] ) self . condition = None


def _create_dag_op(self, name, params, qargs):
        """
        Create a DAG node out of a parsed AST op node.

        Args:
            name (str): operation name to apply to the dag.
            params (list): op parameters
            qargs (list(QuantumRegister, int)): qubits to attach to

        Raises:
            QiskitError: if encountering a non-basis opaque gate
        """
        if name == "u0":
            op_class = U0Gate
        elif name == "u1":
            op_class = U1Gate
        elif name == "u2":
            op_class = U2Gate
        elif name == "u3":
            op_class = U3Gate
        elif name == "x":
            op_class = XGate
        elif name == "y":
            op_class = YGate
        elif name == "z":
            op_class = ZGate
        elif name == "t":
            op_class = TGate
        elif name == "tdg":
            op_class = TdgGate
        elif name == "s":
            op_class = SGate
        elif name == "sdg":
            op_class = SdgGate
        elif name == "swap":
            op_class = SwapGate
        elif name == "rx":
            op_class = RXGate
        elif name == "ry":
            op_class = RYGate
        elif name == "rz":
            op_class = RZGate
        elif name == "rzz":
            op_class = RZZGate
        elif name == "id":
            op_class = IdGate
        elif name == "h":
            op_class = HGate
        elif name == "cx":
            op_class = CnotGate
        elif name == "cy":
            op_class = CyGate
        elif name == "cz":
            op_class = CzGate
        elif name == "ch":
            op_class = CHGate
        elif name == "crz":
            op_class = CrzGate
        elif name == "cu1":
            op_class = Cu1Gate
        elif name == "cu3":
            op_class = Cu3Gate
        elif name == "ccx":
            op_class = ToffoliGate
        elif name == "cswap":
            op_class = FredkinGate
        else:
            raise QiskitError("unknown operation for ast node name %s" % name)

        op = op_class(*params)

        self.dag.apply_operation_back(op, qargs, [], condition=self.condition)

Create a DAG node out of a parsed AST op node .


def ch_duration(self, *channels: List[Channel]) -> int:
        """Return duration of supplied channels.

        Args:
            *channels: Supplied channels
        """
        return self.timeslots.ch_duration(*channels)

def ch_duration ( self , * channels : List [ Channel ] ) -> int : return self . timeslots . ch_duration ( * channels )


def ch_start_time(self, *channels: List[Channel]) -> int:
        """Return minimum start time for supplied channels.

        Args:
            *channels: Supplied channels
        """
        return self.timeslots.ch_start_time(*channels)

def ch_start_time ( self , * channels : List [ Channel ] ) -> int : return self . timeslots . ch_start_time ( * channels )

Return minimum start time for supplied channels .


def ch_stop_time(self, *channels: List[Channel]) -> int:
        """Return maximum start time for supplied channels.

        Args:
            *channels: Supplied channels
        """
        return self.timeslots.ch_stop_time(*channels)

def ch_stop_time ( self , * channels : List [ Channel ] ) -> int : return self . timeslots . ch_stop_time ( * channels )

Return maximum start time for supplied channels .


def _instructions(self, time: int = 0) -> Iterable[Tuple[int, 'Instruction']]:
        """Iterable for flattening Schedule tree.

        Args:
            time: Shifted time due to parent

        Yields:
            Tuple[int, ScheduleComponent]: Tuple containing time `ScheduleComponent` starts
                at and the flattened `ScheduleComponent`.
        """
        for insert_time, child_sched in self.children:
            yield from child_sched._instructions(time + insert_time)

def _instructions ( self , time : int = 0 ) -> Iterable [ Tuple [ int , 'Instruction' ] ] : for insert_time , child_sched in self . children : yield from child_sched . _instructions ( time + insert_time )


def check_type(self, value, attr, data):
        """Validates a value against the correct type of the field.

        It calls ``_expected_types`` to get a list of valid types.

        Subclasses can do one of the following:

            1. They can override the ``valid_types`` property with a tuple with
            the expected types for this field.

            2. They can override the ``_expected_types`` method to return a
            tuple of expected types for the field.

            3. They can change ``check_type`` completely to customize
            validation.

        This method or the overrides must return the ``value`` parameter
        untouched.
        """
        expected_types = self._expected_types()
        if not isinstance(value, expected_types):
            raise self._not_expected_type(
                value, expected_types, fields=[self], field_names=attr, data=data)
        return value

def check_type ( self , value , attr , data ) : expected_types = self . _expected_types ( ) if not isinstance ( value , expected_types ) : raise self . _not_expected_type ( value , expected_types , fields = [ self ] , field_names = attr , data = data ) return value

Validates a value against the correct type of the field .


def dump_additional_data(self, valid_data, many, original_data):
        """Include unknown fields after dumping.

        Unknown fields are added with no processing at all.

        Args:
            valid_data (dict or list): data collected and returned by ``dump()``.
            many (bool): if True, data and original_data are a list.
            original_data (object or list): object passed to ``dump()`` in the
                first place.

        Returns:
            dict: the same ``valid_data`` extended with the unknown attributes.

        Inspired by https://github.com/marshmallow-code/marshmallow/pull/595.
        """
        if many:
            for i, _ in enumerate(valid_data):
                additional_keys = set(original_data[i].__dict__) - set(valid_data[i])
                for key in additional_keys:
                    valid_data[i][key] = getattr(original_data[i], key)
        else:
            additional_keys = set(original_data.__dict__) - set(valid_data)
            for key in additional_keys:
                valid_data[key] = getattr(original_data, key)

        return valid_data

def dump_additional_data ( self , valid_data , many , original_data ) : if many : for i , _ in enumerate ( valid_data ) : additional_keys = set ( original_data [ i ] . __dict__ ) - set ( valid_data [ i ] ) for key in additional_keys : valid_data [ i ] [ key ] = getattr ( original_data [ i ] , key ) else : additional_keys = set ( original_data . __dict__ ) - set ( valid_data ) for key in additional_keys : valid_data [ key ] = getattr ( original_data , key ) return valid_data


def load_additional_data(self, valid_data, many, original_data):
        """Include unknown fields after load.

        Unknown fields are added with no processing at all.

        Args:
            valid_data (dict or list): validated data returned by ``load()``.
            many (bool): if True, data and original_data are a list.
            original_data (dict or list): data passed to ``load()`` in the
                first place.

        Returns:
            dict: the same ``valid_data`` extended with the unknown attributes.

        Inspired by https://github.com/marshmallow-code/marshmallow/pull/595.
        """
        if many:
            for i, _ in enumerate(valid_data):
                additional_keys = set(original_data[i]) - set(valid_data[i])
                for key in additional_keys:
                    valid_data[i][key] = original_data[i][key]
        else:
            additional_keys = set(original_data) - set(valid_data)
            for key in additional_keys:
                valid_data[key] = original_data[key]

        return valid_data

def load_additional_data ( self , valid_data , many , original_data ) : if many : for i , _ in enumerate ( valid_data ) : additional_keys = set ( original_data [ i ] ) - set ( valid_data [ i ] ) for key in additional_keys : valid_data [ i ] [ key ] = original_data [ i ] [ key ] else : additional_keys = set ( original_data ) - set ( valid_data ) for key in additional_keys : valid_data [ key ] = original_data [ key ] return valid_data


def _create_validation_schema(schema_cls):
        """Create a patched Schema for validating models.

        Model validation is not part of Marshmallow. Schemas have a ``validate``
        method but this delegates execution on ``load`` and discards the result.
        Similarly, ``load`` will call ``_deserialize`` on every field in the
        schema.

        This function patches the ``_deserialize`` instance method of each
        field to make it call a custom defined method ``check_type``
        provided by Qiskit in the different fields at
        ``qiskit.validation.fields``.

        Returns:
            BaseSchema: a copy of the original Schema, overriding the
                ``_deserialize()`` call of its fields.
        """
        validation_schema = schema_cls()
        for _, field in validation_schema.fields.items():
            if isinstance(field, ModelTypeValidator):
                validate_function = field.__class__.check_type
                field._deserialize = MethodType(validate_function, field)

        return validation_schema

def _create_validation_schema ( schema_cls ) : validation_schema = schema_cls ( ) for _ , field in validation_schema . fields . items ( ) : if isinstance ( field , ModelTypeValidator ) : validate_function = field . __class__ . check_type field . _deserialize = MethodType ( validate_function , field ) return validation_schema

Create a patched Schema for validating models .


def _validate(instance):
        """Validate the internal representation of the instance."""
        try:
            _ = instance.schema.validate(instance.to_dict())
        except ValidationError as ex:
            raise ModelValidationError(
                ex.messages, ex.field_names, ex.fields, ex.data, **ex.kwargs)

def _validate ( instance ) : try : _ = instance . schema . validate ( instance . to_dict ( ) ) except ValidationError as ex : raise ModelValidationError ( ex . messages , ex . field_names , ex . fields , ex . data , * * ex . kwargs )

Validate the internal representation of the instance .


def _validate_after_init(init_method):
        """Add validation after instantiation."""

        @wraps(init_method)
        def _decorated(self, **kwargs):
            try:
                _ = self.shallow_schema.validate(kwargs)
            except ValidationError as ex:
                raise ModelValidationError(
                    ex.messages, ex.field_names, ex.fields, ex.data, **ex.kwargs) from None

            init_method(self, **kwargs)

        return _decorated

def _validate_after_init ( init_method ) : @ wraps ( init_method ) def _decorated ( self , * * kwargs ) : try : _ = self . shallow_schema . validate ( kwargs ) except ValidationError as ex : raise ModelValidationError ( ex . messages , ex . field_names , ex . fields , ex . data , * * ex . kwargs ) from None init_method ( self , * * kwargs ) return _decorated


def to_dict(self):
        """Serialize the model into a Python dict of simple types.

        Note that this method requires that the model is bound with
        ``@bind_schema``.
        """
        try:
            data, _ = self.schema.dump(self)
        except ValidationError as ex:
            raise ModelValidationError(
                ex.messages, ex.field_names, ex.fields, ex.data, **ex.kwargs) from None

        return data

def to_dict ( self ) : try : data , _ = self . schema . dump ( self ) except ValidationError as ex : raise ModelValidationError ( ex . messages , ex . field_names , ex . fields , ex . data , * * ex . kwargs ) from None return data

Serialize the model into a Python dict of simple types .


def from_dict(cls, dict_):
        """Deserialize a dict of simple types into an instance of this class.

        Note that this method requires that the model is bound with
        ``@bind_schema``.
        """
        try:
            data, _ = cls.schema.load(dict_)
        except ValidationError as ex:
            raise ModelValidationError(
                ex.messages, ex.field_names, ex.fields, ex.data, **ex.kwargs) from None

        return data

def from_dict ( cls , dict_ ) : try : data , _ = cls . schema . load ( dict_ ) except ValidationError as ex : raise ModelValidationError ( ex . messages , ex . field_names , ex . fields , ex . data , * * ex . kwargs ) from None return data

Deserialize a dict of simple types into an instance of this class .


def qft(circ, q, n):
    """n-qubit QFT on q in circ."""
    for j in range(n):
        for k in range(j):
            circ.cu1(math.pi / float(2**(j - k)), q[j], q[k])
        circ.h(q[j])

def qft ( circ , q , n ) : for j in range ( n ) : for k in range ( j ) : circ . cu1 ( math . pi / float ( 2 ** ( j - k ) ) , q [ j ] , q [ k ] ) circ . h ( q [ j ] )