2
$\begingroup$

As far as I know, IBM Quantum provides two types of APIs: one for Qiskit, and the other for third-party uses (API for developers).

In this question, there are several arguments about using the API for developers, there are even useful links to some sources.

But now IBM has changed the interface of its platform, and I can't figure out where to get the API token for developers (while the API token for Qiskit is in the most prominent place).

I would also like to see an example of using the API in action in Java. Example of a program that sends a schema to IBM Quantum and gets the result.

In total, I have two questions:

  1. Where is the API token for developers?
  2. Example of using the API in Java.
$\endgroup$
1
$\begingroup$

For your first question,

Where is the API token for developers?

The API token can be copied from your account details page:

IBM Quantum API Token

For the second question,

Example of using the API in Java.

IBM Quantum API is a RESTful API. It can be consumed the same way as any RESTful API using a REST client. And when it comes to Java, there are many options to choose from to build a REST client. This answer from StackOverflow contains some good options. However, if you are building your solution using a Java Framework, most probably you will find it supports consuming REST APIs. It should be your first option.

Anyway, the following is a simple example using Spring's RestTemplate. The code does not follow the best practices (e.g., no error handling). It is for demonstration only:

RestTemplate restTemplate = new RestTemplate();

String baseUrl = "https://api.quantum-computing.ibm.com/v2";

// Login
URI loginUrl = UriComponentsBuilder.fromUriString(baseUrl)
    .path("/users/loginWithToken")
    .buildAndExpand()
    .toUri();

LoginPayload payload = new LoginPayload();
payload.setApiToken("*********");
HttpEntity<LoginPayload> request = new HttpEntity<>(payload);
JsonNode loginResponse = restTemplate.postForObject(loginUrl, request, JsonNode.class);
String accessToken = loginResponse.get("id").asText();

// Get Providers Information
URI providersInfoUrl = UriComponentsBuilder.fromUriString(baseUrl)
    .path("/Network")
    .queryParam("access_token", accessToken)
    .buildAndExpand()
    .toUri();

// Parse the response to get list of devices
JsonNode response = restTemplate.getForObject(providersInfoUrl, JsonNode.class);
for(JsonNode node : response) {
    if(node.get("name").asText().equals("ibm-q")) {
        JsonNode devices = node.get("groups").get("open").get("projects").get("main").get("devices");
        for(JsonNode device : devices) {
            System.out.println(device.get("name"));
        }
    }
}

Finally, if you plan to support checking the job status in your solution then you will need to support WebSocket also.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.