Skip to content

Commit ec686d8

Browse files
vxfieldmasenol
andauthored
Update Quantum.Job sample and readme to properly submit a job using compressed/gzip input data (#21836)
* Fixes to properly submit a job * Generic connection parameter values * Updating initialization of InputParams * Update readme with new code snippets * Update code snippets in the readme Co-authored-by: Mahmut Burak Senol <masenol@microsoft.com>
1 parent 2af3137 commit ec686d8

File tree

2 files changed

+120
-22
lines changed

2 files changed

+120
-22
lines changed

sdk/quantum/Azure.Quantum.Jobs/README.md

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var resourceGroupName = "your_resource_group_name";
8181
var workspaceName = "your_quantum_workspace_name";
8282
var location = "your_location";
8383
var storageContainerName = "your_container_name";
84+
8485
var credential = new DefaultAzureCredential(true);
8586

8687
var quantumJobClient =
@@ -104,22 +105,59 @@ var containerUri = (quantumJobClient.GetStorageSasUri(
104105

105106
### Upload Input Data
106107

107-
Using the SAS URI, upload the json input data to the blob client.
108+
Using the SAS URI, upload the compressed json input data to the blob client.
109+
Note that we need to compress the json input data before uploading it to the blob storage.
108110
This contains the parameters to be used with [Quantum Inspired Optimizations](https://docs.microsoft.com/azure/quantum/optimization-overview-introduction)
109111

110112
```C# Snippet:Azure_Quantum_Jobs_UploadInputData
113+
string problemFilePath = "./problem.json";
114+
111115
// Get input data blob Uri with SAS key
112-
string blobName = $"myjobinput.json";
116+
string blobName = Path.GetFileName(problemFilePath);
113117
var inputDataUri = (quantumJobClient.GetStorageSasUri(
114118
new BlobDetails(storageContainerName)
115119
{
116120
BlobName = blobName,
117121
})).Value.SasUri;
118122

119-
// Upload input data to blob
120-
var blobClient = new BlobClient(new Uri(inputDataUri));
121-
var problemFilename = "problem.json";
122-
blobClient.Upload(problemFilename, overwrite: true);
123+
using (var problemStreamToUpload = new MemoryStream())
124+
{
125+
using (FileStream problemFileStream = File.OpenRead(problemFilePath))
126+
{
127+
// Check if problem file is a gzip file.
128+
// If it is, just read its contents.
129+
// If not, read and compress the content.
130+
var fileExtension = Path.GetExtension(problemFilePath).ToLower();
131+
if (fileExtension == ".gz" ||
132+
fileExtension == ".gzip")
133+
{
134+
problemFileStream.CopyTo(problemStreamToUpload);
135+
}
136+
else
137+
{
138+
using (var gzip = new GZipStream(problemStreamToUpload, CompressionMode.Compress, leaveOpen: true))
139+
{
140+
byte[] buffer = new byte[8192];
141+
int count;
142+
while ((count = problemFileStream.Read(buffer, 0, buffer.Length)) > 0)
143+
{
144+
gzip.Write(buffer, 0, count);
145+
}
146+
}
147+
}
148+
}
149+
problemStreamToUpload.Position = 0;
150+
151+
// Upload input data to blob
152+
var blobClient = new BlobClient(new Uri(inputDataUri));
153+
var blobHeaders = new BlobHttpHeaders
154+
{
155+
ContentType = "application/json",
156+
ContentEncoding = "gzip"
157+
};
158+
var blobUploadOptions = new BlobUploadOptions { HttpHeaders = blobHeaders };
159+
blobClient.Upload(problemStreamToUpload, options: blobUploadOptions);
160+
}
123161
```
124162

125163
### Create The Job
@@ -134,14 +172,17 @@ var inputDataFormat = "microsoft.qio.v2";
134172
var outputDataFormat = "microsoft.qio-results.v2";
135173
var providerId = "microsoft";
136174
var target = "microsoft.paralleltempering-parameterfree.cpu";
175+
var inputParams = new Dictionary<string, object>() { { "params", new Dictionary<string, object>() } };
137176
var createJobDetails = new JobDetails(containerUri, inputDataFormat, providerId, target)
138177
{
139178
Id = jobId,
140179
InputDataUri = inputDataUri,
141180
Name = jobName,
181+
InputParams = inputParams,
142182
OutputDataFormat = outputDataFormat
143183
};
144-
JobDetails createdJob = (quantumJobClient.CreateJob(jobId, createJobDetails)).Value;
184+
185+
JobDetails myJob = (quantumJobClient.CreateJob(jobId, createJobDetails)).Value;
145186
```
146187

147188
### Get Job
@@ -150,7 +191,7 @@ JobDetails createdJob = (quantumJobClient.CreateJob(jobId, createJobDetails)).Va
150191

151192
```C# Snippet:Azure_Quantum_Jobs_GetJob
152193
// Get the job that we've just created based on its jobId
153-
JobDetails myJob = (quantumJobClient.GetJob(jobId)).Value;
194+
myJob = (quantumJobClient.GetJob(jobId)).Value;
154195
```
155196

156197
### Get Jobs
@@ -160,11 +201,10 @@ To enumerate all the jobs in the workspace, use the `GetJobs` method.
160201
```C# Snippet:Azure_Quantum_Jobs_GetJobs
161202
foreach (JobDetails job in quantumJobClient.GetJobs())
162203
{
163-
Console.WriteLine($"{job.Name}");
204+
Console.WriteLine($"{job.Name}");
164205
}
165206
```
166207

167-
168208
## Troubleshooting
169209

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

sdk/quantum/Azure.Quantum.Jobs/samples/Program.cs

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.IO.Compression;
58
using System.Linq;
9+
using System.Threading;
610
using Azure.Identity;
711
using Azure.Quantum.Jobs.Models;
812
using Azure.Storage.Blobs;
13+
using Azure.Storage.Blobs.Models;
914

1015
namespace Azure.Quantum.Jobs.Samples
1116
{
@@ -20,6 +25,7 @@ public static void Main(string[] args)
2025
var workspaceName = "your_quantum_workspace_name";
2126
var location = "your_location";
2227
var storageContainerName = "your_container_name";
28+
2329
var credential = new DefaultAzureCredential(true);
2430

2531
var quantumJobClient =
@@ -59,18 +65,54 @@ public static void Main(string[] args)
5965
Console.WriteLine($@"Uploading data into a blob...");
6066

6167
#region Snippet:Azure_Quantum_Jobs_UploadInputData
68+
string problemFilePath = "./problem.json";
69+
6270
// Get input data blob Uri with SAS key
63-
string blobName = $"myjobinput.json";
71+
string blobName = Path.GetFileName(problemFilePath);
6472
var inputDataUri = (quantumJobClient.GetStorageSasUri(
6573
new BlobDetails(storageContainerName)
6674
{
6775
BlobName = blobName,
6876
})).Value.SasUri;
6977

70-
// Upload input data to blob
71-
var blobClient = new BlobClient(new Uri(inputDataUri));
72-
var problemFilename = "problem.json";
73-
blobClient.Upload(problemFilename, overwrite: true);
78+
using (var problemStreamToUpload = new MemoryStream())
79+
{
80+
using (FileStream problemFileStream = File.OpenRead(problemFilePath))
81+
{
82+
// Check if problem file is a gzip file.
83+
// If it is, just read its contents.
84+
// If not, read and compress the content.
85+
var fileExtension = Path.GetExtension(problemFilePath).ToLower();
86+
if (fileExtension == ".gz" ||
87+
fileExtension == ".gzip")
88+
{
89+
problemFileStream.CopyTo(problemStreamToUpload);
90+
}
91+
else
92+
{
93+
using (var gzip = new GZipStream(problemStreamToUpload, CompressionMode.Compress, leaveOpen: true))
94+
{
95+
byte[] buffer = new byte[8192];
96+
int count;
97+
while ((count = problemFileStream.Read(buffer, 0, buffer.Length)) > 0)
98+
{
99+
gzip.Write(buffer, 0, count);
100+
}
101+
}
102+
}
103+
}
104+
problemStreamToUpload.Position = 0;
105+
106+
// Upload input data to blob
107+
var blobClient = new BlobClient(new Uri(inputDataUri));
108+
var blobHeaders = new BlobHttpHeaders
109+
{
110+
ContentType = "application/json",
111+
ContentEncoding = "gzip"
112+
};
113+
var blobUploadOptions = new BlobUploadOptions { HttpHeaders = blobHeaders };
114+
blobClient.Upload(problemStreamToUpload, options: blobUploadOptions);
115+
}
74116
#endregion
75117

76118
Console.WriteLine($@"Input data Uri with SAS key:
@@ -87,28 +129,44 @@ public static void Main(string[] args)
87129
var outputDataFormat = "microsoft.qio-results.v2";
88130
var providerId = "microsoft";
89131
var target = "microsoft.paralleltempering-parameterfree.cpu";
132+
var inputParams = new Dictionary<string, object>() { { "params", new Dictionary<string, object>() } };
90133
var createJobDetails = new JobDetails(containerUri, inputDataFormat, providerId, target)
91134
{
92135
Id = jobId,
93136
InputDataUri = inputDataUri,
94137
Name = jobName,
138+
InputParams = inputParams,
95139
OutputDataFormat = outputDataFormat
96140
};
97-
JobDetails createdJob = (quantumJobClient.CreateJob(jobId, createJobDetails)).Value;
141+
142+
JobDetails myJob = (quantumJobClient.CreateJob(jobId, createJobDetails)).Value;
98143
#endregion
99144

100145
Console.WriteLine($@"Job created:
101-
Id: {createdJob.Id}
102-
Name: {createdJob.Name}
103-
CreationTime: {createdJob.CreationTime}
104-
Status: {createdJob.Status}
146+
Id: {myJob.Id}
147+
Name: {myJob.Name}
148+
CreationTime: {myJob.CreationTime}
149+
Status: {myJob.Status}
105150
");
106151

152+
Console.WriteLine($@"Awaiting job to complete...");
153+
while (myJob.Status == JobStatus.Waiting ||
154+
myJob.Status == JobStatus.Executing)
155+
{
156+
Thread.Sleep(TimeSpan.FromSeconds(5));
157+
myJob = (quantumJobClient.GetJob(jobId)).Value;
158+
Console.WriteLine($@"Job status: {myJob.Status}");
159+
}
160+
if (myJob.Status == JobStatus.Failed)
161+
{
162+
Console.WriteLine($@"Job has failed with error: {myJob.ErrorData.Message}");
163+
}
164+
107165
Console.WriteLine($@"Getting Quantum job...");
108166

109167
#region Snippet:Azure_Quantum_Jobs_GetJob
110168
// Get the job that we've just created based on its jobId
111-
JobDetails myJob = (quantumJobClient.GetJob(jobId)).Value;
169+
myJob = (quantumJobClient.GetJob(jobId)).Value;
112170
#endregion
113171

114172
Console.WriteLine($@"Job obtained:
@@ -128,7 +186,7 @@ public static void Main(string[] args)
128186
#region Snippet:Azure_Quantum_Jobs_GetJobs
129187
foreach (JobDetails job in quantumJobClient.GetJobs())
130188
{
131-
Console.WriteLine($"{job.Name}");
189+
Console.WriteLine($"{job.Name}");
132190
}
133191
#endregion
134192

0 commit comments

Comments
 (0)