Skip to content

Commit fa1648a

Browse files
authored
Update Azure Quantum Jobs readme, tests and samples to use Quantum Computing sample (Azure#37892)
1 parent f178f4a commit fa1648a

File tree

10 files changed

+114
-132
lines changed

10 files changed

+114
-132
lines changed

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

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Azure Quantum Jobs client library for .NET
22

3-
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:
3+
Azure Quantum is a Microsoft Azure service that you can use to run quantum computing programs 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:
44
- Create, enumerate, and cancel quantum jobs
55
- Enumerate provider status and quotas
66

@@ -69,7 +69,7 @@ We guarantee that all client instance methods are thread-safe and independent of
6969
Create an instance of the QuantumJobClient by passing in these parameters:
7070
- [Subscription][subscriptions] - looks like XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX and can be found in your list of subscriptions on azure
7171
- [Resource Group][resource-groups] - a container that holds related resources for an Azure solution
72-
- [Workspace][workspaces] - a collection of assets associated with running quantum or optimization applications
72+
- [Workspace][workspaces] - a collection of assets associated with running quantum
7373
- [Location][location] - choose the best data center by geographical region
7474
- [StorageContainerName][blob-storage] - your blob storage
7575
- [Credential][credentials] - used to authenticate
@@ -103,76 +103,67 @@ var containerUri = (quantumJobClient.GetStorageSasUri(
103103
new BlobDetails(storageContainerName))).Value.SasUri;
104104
```
105105

106+
### Compile your quantum program into QIR
107+
108+
This step can be done in multiple ways and it is not in scope for this sample.
109+
110+
[Quantum Intermediate Representation (QIR)](https://github.com/qir-alliance/qir-spec) is a [QIR Alliance](https://www.qir-alliance.org/) specification to represent quantum programs within the [LLVM](https://llvm.org/) Intermediate Representation (IR).
111+
112+
A few methods to compile or generate a quantum program into QIR:
113+
- [Q# compiler](https://github.com/microsoft/qsharp-compiler/): Can be used to [compile Q# Code into QIR](https://github.com/microsoft/qsharp-compiler/tree/main/src/QsCompiler/QirGeneration).
114+
- [PyQIR](https://github.com/qir-alliance/pyqir): PyQIR is a set of APIs for generating, parsing, and evaluating Quantum Intermediate Representation (QIR).
115+
- [IQ#](https://github.com/microsoft/iqsharp): Can be used to compile a Q# program into QIR with the [%qir](https://learn.microsoft.com/qsharp/api/iqsharp-magic/qir) magic command.
116+
117+
In this sample, we assume you already have a file with the QIR bitcode and you know the method name that you want to execute (entry point).
118+
119+
We will use the QIR bitcode sample (./samples/BellState.bc), compiled a Q# code (./samples/BellState.qs) targeting the `quantinuum.sim.h1-1e` target, with `AdaptiveExecution` target capability.
120+
106121
### Upload Input Data
107122

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.
110-
This contains the parameters to be used with [Quantum Inspired Optimizations](https://docs.microsoft.com/azure/quantum/optimization-overview-introduction)
123+
Using the SAS URI, upload the QIR bitcode input data to the blob client.
111124

112-
```C# Snippet:Azure_Quantum_Jobs_UploadInputData
113-
string problemFilePath = "./problem.json";
125+
```C# Snippet:Azure_Quantum_Jobs_UploadQIRBitCode
126+
string qirFilePath = "./BellState.bc";
114127

115128
// Get input data blob Uri with SAS key
116-
string blobName = Path.GetFileName(problemFilePath);
129+
string blobName = Path.GetFileName(qirFilePath);
117130
var inputDataUri = (quantumJobClient.GetStorageSasUri(
118131
new BlobDetails(storageContainerName)
119132
{
120133
BlobName = blobName,
121134
})).Value.SasUri;
122135

123-
using (var problemStreamToUpload = new MemoryStream())
136+
// Upload QIR bitcode to blob storage
137+
var blobClient = new BlobClient(new Uri(inputDataUri));
138+
var blobHeaders = new BlobHttpHeaders
124139
{
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);
140+
ContentType = "qir.v1"
141+
};
142+
var blobUploadOptions = new BlobUploadOptions { HttpHeaders = blobHeaders };
143+
using (FileStream qirFileStream = File.OpenRead(qirFilePath))
144+
{
145+
blobClient.Upload(qirFileStream, options: blobUploadOptions);
160146
}
161147
```
162148

163149
### Create The Job
164150

165-
Now that you've uploaded your problem definition to Azure Storage, you can use `CreateJob` to define an Azure Quantum job.
151+
Now that you've uploaded your QIR program bitcode to Azure Storage, you can use `CreateJob` to define an Azure Quantum job.
166152

167153
```C# Snippet:Azure_Quantum_Jobs_CreateJob
168154
// Submit job
169155
var jobId = $"job-{Guid.NewGuid():N}";
170156
var jobName = $"jobName-{Guid.NewGuid():N}";
171-
var inputDataFormat = "microsoft.qio.v2";
172-
var outputDataFormat = "microsoft.qio-results.v2";
173-
var providerId = "microsoft";
174-
var target = "microsoft.paralleltempering-parameterfree.cpu";
175-
var inputParams = new Dictionary<string, object>() { { "params", new Dictionary<string, object>() } };
157+
var inputDataFormat = "qir.v1";
158+
var outputDataFormat = "microsoft.quantum-results.v1";
159+
var providerId = "quantinuum";
160+
var target = "quantinuum.sim.h1-1e";
161+
var inputParams = new Dictionary<string, object>()
162+
{
163+
{ "entryPoint", "ENTRYPOINT__BellState" },
164+
{ "arguments", new string[] { } },
165+
{ "targetCapability", "AdaptiveExecution" },
166+
};
176167
var createJobDetails = new JobDetails(containerUri, inputDataFormat, providerId, target)
177168
{
178169
Id = jobId,

sdk/quantum/Azure.Quantum.Jobs/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/quantum/Azure.Quantum.Jobs",
5-
"Tag": "net/quantum/Azure.Quantum.Jobs_e7eba546ea"
5+
"Tag": "net/quantum/Azure.Quantum.Jobs_61646d685c"
66
}

sdk/quantum/Azure.Quantum.Jobs/samples/Azure.Quantum.Jobs.Samples.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<None Update="problem.json">
23-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
<None Update="BellState.bc">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2424
</None>
2525
</ItemGroup>
2626

2.25 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace QSharpBellState {
2+
open Microsoft.Quantum.Intrinsic;
3+
4+
operation BellState() : (Result,Result) {
5+
use q0 = Qubit();
6+
use q1 = Qubit();
7+
H(q0);
8+
CNOT(q0, q1);
9+
return (M(q0), M(q1));
10+
}
11+
}

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

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System.IO.Compression;
88
using System.Linq;
99
using System.Threading;
10+
using Azure.Core;
1011
using Azure.Identity;
1112
using Azure.Quantum.Jobs.Models;
1213
using Azure.Storage.Blobs;
1314
using Azure.Storage.Blobs.Models;
15+
using static System.Net.Mime.MediaTypeNames;
1416

1517
namespace Azure.Quantum.Jobs.Samples
1618
{
@@ -64,54 +66,27 @@ public static void Main(string[] args)
6466

6567
Console.WriteLine($@"Uploading data into a blob...");
6668

67-
#region Snippet:Azure_Quantum_Jobs_UploadInputData
68-
string problemFilePath = "./problem.json";
69+
#region Snippet:Azure_Quantum_Jobs_UploadQIRBitCode
70+
string qirFilePath = "./BellState.bc";
6971

7072
// Get input data blob Uri with SAS key
71-
string blobName = Path.GetFileName(problemFilePath);
73+
string blobName = Path.GetFileName(qirFilePath);
7274
var inputDataUri = (quantumJobClient.GetStorageSasUri(
7375
new BlobDetails(storageContainerName)
7476
{
7577
BlobName = blobName,
7678
})).Value.SasUri;
7779

78-
using (var problemStreamToUpload = new MemoryStream())
80+
// Upload QIR bitcode to blob storage
81+
var blobClient = new BlobClient(new Uri(inputDataUri));
82+
var blobHeaders = new BlobHttpHeaders
7983
{
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);
84+
ContentType = "qir.v1"
85+
};
86+
var blobUploadOptions = new BlobUploadOptions { HttpHeaders = blobHeaders };
87+
using (FileStream qirFileStream = File.OpenRead(qirFilePath))
88+
{
89+
blobClient.Upload(qirFileStream, options: blobUploadOptions);
11590
}
11691
#endregion
11792

@@ -125,11 +100,16 @@ public static void Main(string[] args)
125100
// Submit job
126101
var jobId = $"job-{Guid.NewGuid():N}";
127102
var jobName = $"jobName-{Guid.NewGuid():N}";
128-
var inputDataFormat = "microsoft.qio.v2";
129-
var outputDataFormat = "microsoft.qio-results.v2";
130-
var providerId = "microsoft";
131-
var target = "microsoft.paralleltempering-parameterfree.cpu";
132-
var inputParams = new Dictionary<string, object>() { { "params", new Dictionary<string, object>() } };
103+
var inputDataFormat = "qir.v1";
104+
var outputDataFormat = "microsoft.quantum-results.v1";
105+
var providerId = "quantinuum";
106+
var target = "quantinuum.sim.h1-1e";
107+
var inputParams = new Dictionary<string, object>()
108+
{
109+
{ "entryPoint", "ENTRYPOINT__BellState" },
110+
{ "arguments", new string[] { } },
111+
{ "targetCapability", "AdaptiveExecution" },
112+
};
133113
var createJobDetails = new JobDetails(containerUri, inputDataFormat, providerId, target)
134114
{
135115
Id = jobId,

sdk/quantum/Azure.Quantum.Jobs/tests/Azure.Quantum.Jobs.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21-
<None Update="problem.json">
22-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
<None Update="BellState.bc">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2323
</None>
2424
</ItemGroup>
2525
</Project>
2.25 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace QSharpBellState {
2+
open Microsoft.Quantum.Intrinsic;
3+
4+
operation BellState() : (Result,Result) {
5+
use q0 = Qubit();
6+
use q1 = Qubit();
7+
H(q0);
8+
CNOT(q0, q1);
9+
return (M(q0), M(q1));
10+
}
11+
}

sdk/quantum/Azure.Quantum.Jobs/tests/QuantumJobClientLiveTests.cs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -96,55 +96,44 @@ public async Task JobLifecycleTest()
9696
}
9797

9898
// Get input data blob Uri with SAS key
99-
string blobName = $"input-{TestEnvironment.GetRandomId("BlobName")}.json";
99+
string blobName = $"input-{TestEnvironment.GetRandomId("BlobName")}.bc";
100100
var inputDataUri = (await client.GetStorageSasUriAsync(
101101
new BlobDetails("testcontainer")
102102
{
103103
BlobName = blobName,
104104
})).Value.SasUri;
105105

106-
// Upload input data to blob (if not in Playback mode)
106+
// Upload QIR bitcode to blob storage (if not in Playback mode)
107107
if (Mode != RecordedTestMode.Playback)
108108
{
109-
var problemFilePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "problem.json");
109+
var qirFilePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "BellState.bc");
110110

111-
using (var problemStreamToUpload = new MemoryStream())
111+
// Upload QIR bitcode to blob storage
112+
var blobClient = new BlobClient(new Uri(inputDataUri));
113+
var blobHeaders = new BlobHttpHeaders
112114
{
113-
using (FileStream problemFileStream = File.OpenRead(problemFilePath))
114-
{
115-
using (var gzip = new GZipStream(problemStreamToUpload, CompressionMode.Compress, leaveOpen: true))
116-
{
117-
byte[] buffer = new byte[8192];
118-
int count;
119-
while ((count = problemFileStream.Read(buffer, 0, buffer.Length)) > 0)
120-
{
121-
gzip.Write(buffer, 0, count);
122-
}
123-
}
124-
}
125-
126-
problemStreamToUpload.Position = 0;
127-
128-
// Upload input data to blob
129-
var blobClient = new BlobClient(new Uri(inputDataUri));
130-
var blobHeaders = new BlobHttpHeaders
131-
{
132-
ContentType = "application/json",
133-
ContentEncoding = "gzip"
134-
};
135-
var blobUploadOptions = new BlobUploadOptions { HttpHeaders = blobHeaders };
136-
blobClient.Upload(problemStreamToUpload, options: blobUploadOptions);
115+
ContentType = "qir.v1"
116+
};
117+
var blobUploadOptions = new BlobUploadOptions { HttpHeaders = blobHeaders };
118+
using (FileStream qirFileStream = File.OpenRead(qirFilePath))
119+
{
120+
await blobClient.UploadAsync(qirFileStream, options: blobUploadOptions);
137121
}
138122
}
139123

140124
// Submit job
141125
var jobId = $"job-{TestEnvironment.GetRandomId("JobId")}";
142126
var jobName = $"jobName-{TestEnvironment.GetRandomId("JobName")}";
143-
var inputDataFormat = "microsoft.qio.v2";
144-
var outputDataFormat = "microsoft.qio-results.v2";
145-
var providerId = "microsoft";
146-
var target = "microsoft.paralleltempering-parameterfree.cpu";
147-
var inputParams = new Dictionary<string, object>() { { "params", new Dictionary<string, object>() } };
127+
var inputDataFormat = "qir.v1";
128+
var outputDataFormat = "microsoft.quantum-results.v1";
129+
var providerId = "quantinuum";
130+
var target = "quantinuum.sim.h1-1e";
131+
var inputParams = new Dictionary<string, object>()
132+
{
133+
{ "entryPoint", "ENTRYPOINT__BellState" },
134+
{ "arguments", new string[] { } },
135+
{ "targetCapability", "AdaptiveExecution" },
136+
};
148137
var createJobDetails = new JobDetails(containerUri, inputDataFormat, providerId, target)
149138
{
150139
Id = jobId,

0 commit comments

Comments
 (0)