Beispiel #1
0
    def select_credentials(self, url):
        if url is not None:
            credentials = Credentials('', url)
            if credentials.unique_id() in self._credentials:
                self._selected_credentials = self._credentials[
                    credentials.unique_id()]

        return self._selected_credentials
Beispiel #2
0
 def remove_credentials(self, url):
     if url is not None:
         credentials = Credentials('', url)
         if credentials.unique_id() in self._credentials:
             del self._credentials[credentials.unique_id()]
             self._credentials_changed = True
         if self._selected_credentials is not None and self._selected_credentials.unique_id(
         ) == credentials.unique_id():
             self._selected_credentials = None
             credentials = list(self._credentials.values())
             if len(credentials) > 0:
                 self._selected_credentials = credentials[0]
Beispiel #3
0
    def set_credentials(self, token, url, proxy_urls=None):
        if url is not None and token is not None:
            proxies = {} if proxy_urls is None else {'urls': proxy_urls}
            credentials = Credentials(token, url, proxies=proxies)
            self._credentials[credentials.unique_id()] = credentials
            self._credentials_changed = True
            return credentials

        return None
Beispiel #4
0
def _get_credentials(test_object, test_options):
    """
    Finds the credentials for a specific test and options.

    Args:
        test_object (QiskitTestCase): The test object asking for credentials
        test_options (dict): Options after QISKIT_TESTS was parsed by get_test_options.

    Returns:
        Credentials: set of credentials

    Raises:
        Exception: When the credential could not be set and they are needed for that set of options
    """

    dummy_credentials = Credentials(
        'dummyapiusersloginWithTokenid01',
        'https://quantumexperience.ng.bluemix.net/api')

    if test_options['mock_online']:
        return dummy_credentials

    if os.getenv('USE_ALTERNATE_ENV_CREDENTIALS', False):
        # Special case: instead of using the standard credentials mechanism,
        # load them from different environment variables. This assumes they
        # will always be in place, as is used by the Travis setup.
        return Credentials(os.getenv('IBMQ_TOKEN'), os.getenv('IBMQ_URL'))
    else:
        # Attempt to read the standard credentials.
        discovered_credentials = discover_credentials()

        if discovered_credentials:
            # Decide which credentials to use for testing.
            if len(discovered_credentials) > 1:
                try:
                    # Attempt to use QE credentials.
                    return discovered_credentials[
                        dummy_credentials.unique_id()]
                except KeyError:
                    pass

            # Use the first available credentials.
            return list(discovered_credentials.values())[0]

    # No user credentials were found.
    if test_options['rec']:
        raise Exception(
            'Could not locate valid credentials. You need them for recording '
            'tests against the remote API.')

    test_object.log.warning(
        "No user credentials were detected. Running with mocked data.")
    test_options['mock_online'] = True
    return dummy_credentials
Beispiel #5
0
    def register_and_get_operational_backends():
        # update registration info using internal methods because:
        # at this point I don't want to save to or removecredentials from disk
        # I want to update url, proxies etc without removing token and
        # re-adding in 2 methods

        ibmq_backends = []
        try:
            credentials = None
            preferences = Preferences()
            url = preferences.get_url()
            token = preferences.get_token()
            if url is not None and url != '' and token is not None and token != '':
                credentials = Credentials(token,
                                          url,
                                          proxies=preferences.get_proxies({}))
            if credentials is not None:
                qiskit.IBMQ._accounts[
                    credentials.unique_id()] = IBMQSingleProvider(
                        credentials, qiskit.IBMQ)
                logger.debug("Registered with Qiskit successfully.")
                ibmq_backends = [
                    x.name()
                    for x in qiskit.IBMQ.backends(url=url, token=token)
                ]
        except Exception as e:
            logger.debug("Failed to register with Qiskit: {}".format(str(e)))

        backends = set()
        aer_backends = [x.name() for x in qiskit.Aer.backends()]
        for aer_backend in aer_backends:
            backend = None
            for group_name, names in qiskit.Aer.grouped_backend_names().items(
            ):
                if aer_backend in names:
                    backend = group_name
                    break
            if backend is None:
                backend = aer_backend

            supported = True
            for unsupported_backend in QuantumAlgorithm.UNSUPPORTED_BACKENDS:
                if backend.startswith(unsupported_backend):
                    supported = False
                    break

            if supported:
                backends.add(backend)

        return list(backends) + ibmq_backends
Beispiel #6
0
 def get_credentials_with_same_key(self, url):
     if url is not None:
         credentials = Credentials('', url)
         return self._credentials.get(credentials.unique_id())
     return False