Esempio n. 1
0
    def __init__(
            self,
            base_uri: str,
            authentication: Optional[coreapi.auth.AuthBase] = None,
            project_name: Optional[str] = None,
            coreapi_client_class: Type[coreapi.Client] = coreapi.Client
    ) -> None:
        """ Python interface to the Quantum Inspire API (Application Programmer Interface).

        The Quantum Inspire API supplies an interface for executing cQASM programs and can be used to access the
        different entities in the Quantum Inspire database needed for running the programs.
        The entities for which an interface is provided are:
            Backend types: Depending on the user account more qubits can be used and simulated on faster hardware.
            Projects: Executing programs is done from a project. Projects keep track of the other entities.
            Jobs: A job contains all the information and the parameters needed for executing the program.
            Assets: A container for a cQASM program. Is part of a job.
            Results: After the job is finished, the results are gathered in the result-entity.

        QuantumInspireAPI is a convenient interface (or wrapper) to the low level API and hides details for
        requesting data (via get) and performing operations on the different entities (via actions).

        For more documentation see the knowledge base on: https://www.quantum-inspire.com/kbase/low-level-api/
        The REST API can be found on: https://api.quantum-inspire.com/
        The Core API schema is published on: https://api.quantum-inspire.com/schema/

        Args:
            base_uri: The base uri of the Quantum Inspire API-location where the schema can be found (in path
                      'schema/').
            authentication: The authentication, can be one of the following coreapi authentications:
                            BasicAuthentication(email, password), HTTP authentication with valid email/password.
                            TokenAuthentication(token, scheme="token"), token authentication with a valid API-token.
                            When authentication is None, a token is read from the default resource.
            project_name: The project used for executing the jobs.
            coreapi_client_class: Coreapi client to interact with the API through a schema.
                                  Default set to coreapi.Client.

        Note: When no project name is given, a temporary project is created for the job and deleted after the job
              has finished. When a project name is given, a project is created if it does not exist, but re-used
              if a project with that name already exists. In either case, the project will not be deleted when a
              project name is supplied here.

        Raises:
            ApiError: An ApiError exception is raised when no authentication is given and the token could not be
                      loaded or the schema could not be loaded.
        """
        if authentication is None:
            token = load_token()
            if token is not None:
                authentication = TokenAuthentication(token, scheme="token")
            else:
                raise ApiError('No credentials have been provided')
        self.__client = coreapi_client_class(auth=authentication)
        self.project_name = project_name
        self.base_uri = base_uri
        try:
            self._load_schema()
        except (CoreAPIException, TypeError) as ex:
            raise ApiError('Could not connect to {}'.format(base_uri)) from ex
Esempio n. 2
0
def get_token_authentication(token: str) -> TokenAuthentication:
    """ Set up token authentication for Quantum Inspire to be used in the API.

    Args
        token: the Quantum Inspire  token to set in TokenAuthentication.

    Returns:
        The token authentication for Quantum Inspire.
    """
    return TokenAuthentication(token, scheme="token")
Esempio n. 3
0
 def test_get_api(self):
     with mock.patch(
             'quantuminspire.qiskit.quantum_inspire_provider.QuantumInspireAPI'
     ) as api:
         quantum_inpire_provider = QuantumInspireProvider()
         token = 'This_is_a_nice_looking_token'
         authentication = TokenAuthentication(token, scheme="token")
         project_name = 'This_is_my_first_project_name'
         quantum_inpire_provider.set_authentication(
             authentication, project_name=project_name)
         actual_api = quantum_inpire_provider.get_api()
         self.assertIsNotNone(actual_api)
Esempio n. 4
0
def get_token_authentication(token: Optional[str] = None) -> TokenAuthentication:
    """ Set up token authentication for Quantum Inspire to be used in the API.

    Args:
        token: the Quantum Inspire token to set in TokenAuthentication. When no token is given,

    Returns:
        The token authentication for Quantum Inspire.
    """
    if not token:
        token = load_account()
    return TokenAuthentication(token, scheme="token")
Esempio n. 5
0
def get_token_authentication(
        token: Optional[str] = None) -> TokenAuthentication:
    """Set up token authentication for Quantum Inspire to be used in the API.

    :param token: the Quantum Inspire token to set in TokenAuthentication. When no token is given,
        the token returned from :meth:`~.load_account` is used.

    :return:
        The token authentication for Quantum Inspire.
    """
    if not token:
        token = load_account()
    return TokenAuthentication(token, scheme="token")
Esempio n. 6
0
 def test_set_projectname(self):
     with mock.patch(
             'quantuminspire.qiskit.quantum_inspire_provider.QuantumInspireAPI'
     ) as api:
         quantum_inpire_provider = QuantumInspireProvider()
         token = 'This_is_a_nice_looking_token'
         authentication = TokenAuthentication(token, scheme="token")
         project_name = 'This_is_my_first_project_name'
         quantum_inpire_provider.set_authentication(
             authentication, project_name=project_name)
         api.assert_called_with(QI_URL, authentication, project_name)
         project_name = 'This_is_my_second_project_name'
         quantum_inpire_provider.set_project_name(project_name)
         self.assertEqual(api.return_value.project_name, project_name)
Esempio n. 7
0
 def test_set_authentication(self):
     with mock.patch(
             'quantuminspire.qiskit.quantum_inspire_provider.QuantumInspireAPI'
     ) as api:
         quantum_inpire_provider = QuantumInspireProvider()
         with self.assertRaises(QiskitBackendError):
             quantum_inpire_provider.backends(name='quantum-inspire')
         token = 'This_is_a_nice_looking_token'
         authentication = TokenAuthentication(token, scheme="token")
         quantum_inpire_provider.set_authentication(authentication)
         api.assert_called_with(QI_URL, authentication, None)
         authentication = BasicAuthentication('email', 'password')
         quantum_inpire_provider.set_authentication(authentication)
         api.assert_called_with(QI_URL, authentication, None)
Esempio n. 8
0
import requests
from coreapi import Client
from coreapi.auth import TokenAuthentication

# """ http post http://localhost:9000/timeside/api-token-auth/ username=admin password=admin """
url = 'http://localhost:9000/timeside/api-token-auth/'
auth={'username':'******', 'password':'******'}
r = requests.post(url, data=auth)
token=r.json()['token']

#coreapi client with the right token
auth = TokenAuthentication(
    scheme='Token',
    token=token
)
client = Client(auth=auth)

#testing several request to the TimeSide core API
schema = client.get('http://localhost:9000/timeside/api/schema/')

keys = ['api', 'items', 'create']
params = {'title':'fooTest'}
client.action(schema,keys,params)

keys = ['api', 'items', 'list']
data = client.action(schema,keys)
for item in data:
    print(item['title'] + '   ' + item['uuid'])