Skip to content

Commit c50254b

Browse files
authored
[quantum] Migrate quantum projects to use snippets extraction (#33198)
### Packages impacted by this PR - @azure/quantum-jobs ### Issues associated with this PR - #32416 ### Describe the problem that is addressed by this PR Updates all projects under `quantum` to use snippets extraction. ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary)
1 parent e37e425 commit c50254b

8 files changed

+469
-110
lines changed

sdk/quantum/quantum-jobs/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
<!-- dev-tool snippets ignore -->
2+
13
# Release History
24

35
## 1.0.0-beta.2 (Unreleased)
46

57
### Breaking Changes
68

79
- Migrated to the Core v2 HTTP pipeline. As a result of this migration:
10+
811
- The response types no longer contain the raw response `_response`. To access the raw response, an `onResponse` callback has to be passed in the request options bag, for example:
912

1013
```ts

sdk/quantum/quantum-jobs/README.md

+210-90
Original file line numberDiff line numberDiff line change
@@ -63,44 +63,68 @@ Create an instance of the QuantumJobClient by passing in these parameters:
6363
- [Storage Container Name][blob-storage] - your blob storage
6464
- [Credential][credentials] - used to authenticate
6565

66-
```Javascript Snippet
67-
const credential = new DefaultAzureCredential();
68-
69-
// Create a QuantumJobClient
70-
const subscriptionId = "your_subscription_id";
71-
const resourceGroupName = "your_resource_group_name";
72-
const workspaceName = "your_quantum_workspace_name";
73-
const storageContainerName = "mycontainer";
74-
const location = "westus"; //"your_location";
75-
const endpoint = "https://" + location + ".quantum.azure.com";
76-
77-
const quantumJobClient = new QuantumJobClient(
78-
credential,
79-
subscriptionId,
80-
resourceGroupName,
81-
workspaceName,
82-
{
83-
endpoint: endpoint,
84-
credentialScopes: "https://quantum.microsoft.com/.default"
85-
}
86-
);
66+
```ts snippet:ReadmeSampleCreateClient_TokenCredential
67+
import { DefaultAzureCredential } from "@azure/identity";
68+
import { QuantumJobClient } from "@azure/quantum-jobs";
69+
70+
const credential = new DefaultAzureCredential();
71+
72+
// Create a QuantumJobClient
73+
const subscriptionId = "your_subscription_id";
74+
const resourceGroupName = "your_resource_group_name";
75+
const workspaceName = "your_quantum_workspace_name";
76+
const location = "westus";
77+
const endpoint = `https://${location}.quantum.azure.com`;
78+
const quantumJobClient = new QuantumJobClient(
79+
credential,
80+
subscriptionId,
81+
resourceGroupName,
82+
workspaceName,
83+
{
84+
endpoint: endpoint,
85+
credentialScopes: "https://quantum.microsoft.com/.default",
86+
},
87+
);
8788
```
8889

8990
### Get Container SAS URI
9091

9192
Create a storage container to put your data.
9293

93-
```Javascript Snippet
94-
// Get container Uri with SAS key
95-
const containerUri = (
96-
await quantumJobClient.storage.sasUri({
97-
containerName: storageContainerName
98-
})
99-
).sasUri;
100-
101-
// Create container if not exists
102-
const containerClient = new ContainerClient(containerUri);
103-
await containerClient.createIfNotExists();
94+
```ts snippet:ReadmeSampleCreateContainer
95+
import { DefaultAzureCredential } from "@azure/identity";
96+
import { QuantumJobClient } from "@azure/quantum-jobs";
97+
import { ContainerClient } from "@azure/storage-blob";
98+
99+
const credential = new DefaultAzureCredential();
100+
const subscriptionId = "your_subscription_id";
101+
const resourceGroupName = "your_resource_group_name";
102+
const workspaceName = "your_quantum_workspace_name";
103+
const storageContainerName = "containername";
104+
const location = "westus";
105+
const endpoint = `https://${location}.quantum.azure.com`;
106+
107+
const quantumJobClient = new QuantumJobClient(
108+
credential,
109+
subscriptionId,
110+
resourceGroupName,
111+
workspaceName,
112+
{
113+
endpoint: endpoint,
114+
credentialScopes: "https://quantum.microsoft.com/.default",
115+
},
116+
);
117+
118+
// Get container Uri with SAS key
119+
const containerUri = (
120+
await quantumJobClient.storage.sasUri({
121+
containerName: storageContainerName,
122+
})
123+
).sasUri;
124+
125+
// Create container if not exists
126+
const containerClient = new ContainerClient(containerUri);
127+
await containerClient.createIfNotExists();
104128
```
105129

106130
### Compile your quantum program into QIR
@@ -123,82 +147,166 @@ We will use the QIR bitcode sample (`BellState.bc` in the samples folder), compi
123147

124148
Using the SAS URI, upload the QIR bitcode input data to the blob client.
125149

126-
```Javascript Snippet
127-
// Get input data blob Uri with SAS key
128-
const blobName = "myjobinput.bc";
129-
const inputDataUri = (
130-
await quantumJobClient.storage.sasUri({
131-
containerName: storageContainerName,
132-
blobName: blobName
133-
})
134-
).sasUri;
135-
136-
// Upload input data to blob
137-
const blobClient = new BlockBlobClient(inputDataUri);
138-
const problemFilename = "BellState.bc";
139-
const fileContent = fs.readFileSync(problemFilename, "utf8");
140-
const blobOptions = {
141-
blobHTTPHeaders: {
142-
blobContentType: "qir.v1",
143-
},
144-
};
145-
await blobClient.upload(fileContent, Buffer.byteLength(fileContent), blobOptions);
150+
```ts snippet:ReadmeSampleUploadInputData
151+
import { DefaultAzureCredential } from "@azure/identity";
152+
import { QuantumJobClient } from "@azure/quantum-jobs";
153+
import { BlockBlobClient } from "@azure/storage-blob";
154+
import { readFileSync } from "node:fs";
155+
156+
const credential = new DefaultAzureCredential();
157+
const subscriptionId = "your_subscription_id";
158+
const resourceGroupName = "your_resource_group_name";
159+
const workspaceName = "your_quantum_workspace_name";
160+
const storageContainerName = "containername";
161+
const location = "westus";
162+
const endpoint = `https://${location}.quantum.azure.com`;
163+
164+
const quantumJobClient = new QuantumJobClient(
165+
credential,
166+
subscriptionId,
167+
resourceGroupName,
168+
workspaceName,
169+
{
170+
endpoint: endpoint,
171+
credentialScopes: "https://quantum.microsoft.com/.default",
172+
},
173+
);
174+
175+
// Get input data blob Uri with SAS key
176+
const blobName = "myjobinput.bc";
177+
const inputDataUri = (
178+
await quantumJobClient.storage.sasUri({
179+
containerName: storageContainerName,
180+
blobName: blobName,
181+
})
182+
).sasUri;
183+
184+
// Upload input data to blob
185+
const blobClient = new BlockBlobClient(inputDataUri);
186+
const problemFilename = "BellState.bc";
187+
const fileContent = readFileSync(problemFilename, "utf8");
188+
const blobOptions = {
189+
blobHTTPHeaders: {
190+
blobContentType: "qir.v1",
191+
},
192+
};
193+
await blobClient.upload(fileContent, Buffer.byteLength(fileContent), blobOptions);
146194
```
147195

148196
### Create The Job
149197

150198
Now that you've uploaded your problem definition to Azure Storage, you can use `jobs.create` to define an Azure Quantum job.
151199

152-
```Javascript Snippet
153-
const randomId = `${Math.floor(Math.random() * 10000 + 1)}`;
154-
155-
// Submit job
156-
const jobId = `job-${randomId}`;
157-
const jobName = `jobName-${randomId}`;
158-
const inputDataFormat = "qir.v1";
159-
const outputDataFormat = "microsoft.quantum-results.v1";
160-
const providerId = "quantinuum";
161-
const target = "quantinuum.sim.h1-1e";
162-
const inputParams = {
163-
"entryPoint": "ENTRYPOINT__BellState",
164-
"arguments": [],
165-
"targetCapability": "AdaptiveExecution",
166-
};
167-
const createJobDetails = {
168-
containerUri: containerUri,
169-
inputDataFormat: inputDataFormat,
170-
providerId: providerId,
171-
target: target,
172-
id: jobId,
173-
inputDataUri: inputDataUri,
174-
name: jobName,
175-
outputDataFormat: outputDataFormat,
176-
inputParams: inputParams
177-
};
178-
const createdJob = await quantumJobClient.jobs.create(jobId, createJobDetails);
200+
```ts snippet:ReadmeSampleCreateJob
201+
import { DefaultAzureCredential } from "@azure/identity";
202+
import { QuantumJobClient } from "@azure/quantum-jobs";
203+
204+
const credential = new DefaultAzureCredential();
205+
const subscriptionId = "your_subscription_id";
206+
const resourceGroupName = "your_resource_group_name";
207+
const workspaceName = "your_quantum_workspace_name";
208+
const location = "westus";
209+
const endpoint = `https://${location}.quantum.azure.com`;
210+
211+
const quantumJobClient = new QuantumJobClient(
212+
credential,
213+
subscriptionId,
214+
resourceGroupName,
215+
workspaceName,
216+
{
217+
endpoint: endpoint,
218+
credentialScopes: "https://quantum.microsoft.com/.default",
219+
},
220+
);
221+
222+
const randomId = `${Math.floor(Math.random() * 10000 + 1)}`;
223+
224+
// Submit job
225+
const jobId = `job-${randomId}`;
226+
const jobName = `jobName-${randomId}`;
227+
const inputDataFormat = "qir.v1";
228+
const outputDataFormat = "microsoft.quantum-results.v1";
229+
const providerId = "quantinuum";
230+
const target = "quantinuum.sim.h1-1e";
231+
const inputParams = {
232+
entryPoint: "ENTRYPOINT__BellState",
233+
arguments: [],
234+
targetCapability: "AdaptiveExecution",
235+
};
236+
const createJobDetails = {
237+
containerUri: "https://<container-uri>",
238+
inputDataFormat: inputDataFormat,
239+
providerId: providerId,
240+
target: target,
241+
id: jobId,
242+
inputDataUri: "https://<input-data-url>",
243+
name: jobName,
244+
outputDataFormat: outputDataFormat,
245+
inputParams: inputParams,
246+
};
247+
const createdJob = await quantumJobClient.jobs.create(jobId, createJobDetails);
179248
```
180249

181250
### Get Job
182251

183252
`GetJob` retrieves a specific job by its id.
184253

185-
```Javascript Snippet
186-
// Get the job that we've just created based on its jobId
187-
const myJob = await quantumJobClient.jobs.get(jobId);
254+
```ts snippet:ReadmeSampleGetJob
255+
import { DefaultAzureCredential } from "@azure/identity";
256+
import { QuantumJobClient } from "@azure/quantum-jobs";
257+
258+
const credential = new DefaultAzureCredential();
259+
const subscriptionId = "your_subscription_id";
260+
const resourceGroupName = "your_resource_group_name";
261+
const workspaceName = "your_quantum_workspace_name";
262+
const location = "westus";
263+
const endpoint = `https://${location}.quantum.azure.com`;
264+
265+
const quantumJobClient = new QuantumJobClient(
266+
credential,
267+
subscriptionId,
268+
resourceGroupName,
269+
workspaceName,
270+
{
271+
endpoint: endpoint,
272+
credentialScopes: "https://quantum.microsoft.com/.default",
273+
},
274+
);
275+
276+
// Get the job that we've just created based on its jobId
277+
const myJob = await quantumJobClient.jobs.get("job-1234");
188278
```
189279

190280
### Get Jobs
191281

192282
To enumerate all the jobs in the workspace, use the `jobs.list` method.
193283

194-
```Javascript Snippet
195-
let jobListResult = await quantumJobClient.jobs.list();
196-
let listOfJobs = await jobListResult.next();
197-
while (!listOfJobs.done) {
198-
let job = listOfJobs.value;
199-
console.log(` ${job.name}`);
200-
listOfJobs = await jobListResult.next();
201-
}
284+
```ts snippet:ReadmeSampleListJobs
285+
import { DefaultAzureCredential } from "@azure/identity";
286+
import { QuantumJobClient } from "@azure/quantum-jobs";
287+
288+
const credential = new DefaultAzureCredential();
289+
const subscriptionId = "your_subscription_id";
290+
const resourceGroupName = "your_resource_group_name";
291+
const workspaceName = "your_quantum_workspace_name";
292+
const location = "westus";
293+
const endpoint = `https://${location}.quantum.azure.com`;
294+
295+
const quantumJobClient = new QuantumJobClient(
296+
credential,
297+
subscriptionId,
298+
resourceGroupName,
299+
workspaceName,
300+
{
301+
endpoint: endpoint,
302+
credentialScopes: "https://quantum.microsoft.com/.default",
303+
},
304+
);
305+
306+
const jobListResult = quantumJobClient.jobs.list();
307+
for await (const job of jobListResult) {
308+
console.log(`Job Id: ${job.id} and Job Name: ${job.name}`);
309+
}
202310
```
203311

204312
## Next steps
@@ -224,6 +332,18 @@ additional questions or comments.
224332

225333
All Quantum Jobs service operations will throw a RequestFailedException on failure with helpful ErrorCodes. Many of these errors are recoverable.
226334

335+
### Logging
336+
337+
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
338+
339+
```ts snippet:SetLogLevel
340+
import { setLogLevel } from "@azure/logger";
341+
342+
setLogLevel("info");
343+
```
344+
345+
For more detailed instructions on how to enable logs, you can look at the [@azure/logger package docs](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger).
346+
227347
<!-- LINKS -->
228348

229349
[source]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/quantum/quantum-jobs/src

0 commit comments

Comments
 (0)