Azure Quantum Jobs client library for JavaScript - version 1.0.0-beta.1
This package contains an isomorphic SDK for QuantumJobClient.
Azure Quantum is a Microsoft Azure service that you can use to run quantum computing programs or solve optimization problems in the cloud. Using the Azure Quantum tools and SDKs, you can create quantum programs and run them against different quantum simulators and machines. You can use the @azure/quantum-jobs
client library to:
Create, enumerate, and cancel quantum jobs
Enumerate provider status and quotas
Source code | API reference documentation | Product documentation | Samples
Getting started
This section includes everything a developer needs to install and create their first client connection very quickly.
Install the package
Install the Azure Quantum Jobs client library for Javascript with npm
:
npm install @azure/quantum-jobs
Prerequisites
- Node.js version 8.x.x or higher
- Azure subscription
- Azure Quantum Workspace
Authenticate the client
To authenticate with the service, you can use DefaultAzureCredential from the @azure/identity
library. This will try different authentication mechanisms based on the environment (e.g. Environment Variables, ManagedIdentity, CachedTokens) and finally, it will fallback to InteractiveBrowserCredential.
The client also allows the user to override the above behavior by passing their own implementations of the TokenCredential.
TokenCredential
is the default Authentication mechanism used by Azure SDKs.
Key concepts
QuantumJobClient
is the root class to be used to authenticate, and create, enumerate, and cancel jobs.
JobDetails
contains all the properties of a job.
ProviderStatus
contains status information for a provider.
QuantumJobQuota
contains quota properties.
Examples
Create the client
Create an instance of the QuantumJobClient by passing in these parameters:
- Subscription Id - looks like XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX and can be found in your list of subscriptions on azure
- Resource Group Name - a container that holds related resources for an Azure solution
- Workspace Name - a collection of assets associated with running quantum or optimization applications
- Location - choose the best data center by geographical region
- Storage Container Name - your blob storage
- Credential - used to authenticate
const credential = new DefaultAzureCredential();
// Create a QuantumJobClient
const subscriptionId = "your_subscription_id";
const resourceGroupName = "your_resource_group_name";
const workspaceName = "your_quantum_workspace_name";
const storageContainerName = "mycontainer";
const location = "westus"; //"your_location";
const endpoint = "https://" + location + ".quantum.azure.com";
const quantumJobClient = new QuantumJobClient(
credential,
subscriptionId,
resourceGroupName,
workspaceName,
{
endpoint: endpoint,
credentialScopes: "https://quantum.microsoft.com/.default"
}
);
Get Container SAS URI
Create a storage container to put your data.
// Get container Uri with SAS key
const containerUri = (
await quantumJobClient.storage.sasUri({
containerName: storageContainerName
})
).sasUri;
// Create container if not exists
const containerClient = new ContainerClient(containerUri);
await containerClient.createIfNotExists();
Upload Input Data
Using the SAS URI, upload the json input data to the blob client. This contains the parameters to be used with Quantum Inspired Optimizations
// Get input data blob Uri with SAS key
const blobName = "myjobinput.json";
const inputDataUri = (
await quantumJobClient.storage.sasUri({
containerName: storageContainerName,
blobName: blobName
})
).sasUri;
// Upload input data to blob
const blobClient = new BlockBlobClient(inputDataUri);
const problemFilename = "problem.json";
const fileContent = fs.readFileSync(problemFilename, "utf8");
await blobClient.upload(fileContent, Buffer.byteLength(fileContent));
Create The Job
Now that you've uploaded your problem definition to Azure Storage, you can use jobs.create
to define an Azure Quantum job.
const randomId = `${Math.floor(Math.random() * 10000 + 1)}`;
// Submit job
const jobId = `job-${randomId}`;
const jobName = `jobName-${randomId}`;
const inputDataFormat = "microsoft.qio.v2";
const outputDataFormat = "microsoft.qio-results.v2";
const providerId = "microsoft";
const target = "microsoft.paralleltempering-parameterfree.cpu";
const createJobDetails = {
containerUri: containerUri,
inputDataFormat: inputDataFormat,
providerId: providerId,
target: target,
id: jobId,
inputDataUri: inputDataUri,
name: jobName,
outputDataFormat: outputDataFormat
};
const createdJob = await quantumJobClient.jobs.create(jobId, createJobDetails);
Get Job
GetJob
retrieves a specific job by its id.
// Get the job that we've just created based on its jobId
const myJob = await quantumJobClient.jobs.get(jobId);
Get Jobs
To enumerate all the jobs in the workspace, use the jobs.list
method.
let jobListResult = await quantumJobClient.jobs.list();
let listOfJobs = await jobListResult.next();
while (!listOfJobs.done) {
let job = listOfJobs.value;
console.log(` ${job.name}`);
listOfJobs = await jobListResult.next();
}
Next steps
- Visit our Product documentation to learn more about Azure Quantum.
Contributing
See the CONTRIBUTING.md for details on building, testing, and contributing to this library.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Troubleshooting
All Quantum Jobs service operations will throw a RequestFailedException on failure with helpful ErrorCodes. Many of these errors are recoverable.
Azure SDK for JavaScript