Skip to content

Commit 2493c69

Browse files
committed
polish deps
1 parent a12586e commit 2493c69

File tree

5 files changed

+154
-81
lines changed

5 files changed

+154
-81
lines changed

Project.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@ AWS = "fbe9abb3-538b-5e4e-ba9e-bc94f4f92ebc"
88
Configurations = "5218b696-f38b-4ac9-8b61-a12ec717816d"
99
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
1010
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
11-
Expronicon = "6b7a57c9-7cc1-4fdf-b7f5-e857abae3636"
12-
ExproniconLite = "55351af7-c7e9-48d6-89ff-24e801d99491"
1311
GarishPrint = "b0ab02a7-8576-43f7-aa76-eaa7c3897c54"
1412
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
1513
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1614
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
1715
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1816

1917
[compat]
20-
AWS = "1.39"
18+
AWS = "1"
2119
Configurations = "0.16"
20+
Crayons = "4"
21+
GarishPrint = "0.2"
22+
HTTP = "0.9"
23+
JSON = "0.21"
2224
julia = "1"
2325

2426
[extras]
27+
BrokenRecord = "bdd55f5b-6e67-4da1-a080-6086e55655a0"
2528
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2629

2730
[targets]
28-
test = ["Test"]
31+
test = ["Test", "BrokenRecord"]

src/AWSBraket.jl

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,57 @@ function parse_device_info(d)
2525
return from_dict_validate(Schema.DeviceInfo, d)::Schema.DeviceInfo
2626
end
2727

28+
const DOC_AWS_CONFIG = """
29+
# AWS Service Configuration
30+
31+
You can specify custom configuration via `aws_config` keyword argument,
32+
to select another regions or use a different account, default
33+
is `AWS.global_aws_config()`, e.g you can choose a different region in
34+
this config
35+
36+
```julia
37+
using AWS
38+
config = AWSConfig(;region="us-west-1")
39+
```
40+
41+
See the [AWS Julia Interface](https://github.com/JuliaCloud/AWS.jl) documentation
42+
for more advanced usage.
43+
"""
44+
45+
"""
46+
get_device(arn::String; aws_config=AWS.global_aws_config())
47+
48+
Retrieves the devices available in Amazon Braket.
49+
50+
# Arguments
51+
52+
- `arn`: The ARN of the device to retrieve.
53+
54+
$(DOC_AWS_CONFIG)
55+
"""
2856
function get_device(arn::String; aws_config=AWS.global_aws_config())
2957
# NOTE: the AWS dev docs
3058
d = Braket.get_device(HTTP.escapeuri(arn); aws_config)
3159
return parse_device_info(d)
3260
end
3361

62+
"""
63+
search_devices(filters=[]; max_results::Maybe{Int}=nothing, next_token::Maybe{String}=nothing, aws_config=AWS.global_aws_config())
64+
65+
Searches for devices using the specified filters.
66+
67+
# Arguments
68+
69+
- `filters`: The filter values to use to search for a device.
70+
71+
# Keyword Arguments (Optional)
72+
73+
- `max_results`: The maximum number of results to return in the response.
74+
- `next_token`: A token used for pagination of results returned in the response. Use the token returned from
75+
the previous request continue results where the previous request ended.
76+
77+
$(DOC_AWS_CONFIG)
78+
"""
3479
function search_devices(filters=[]; max_results::Maybe{Int}=nothing, next_token::Maybe{String}=nothing, aws_config=AWS.global_aws_config())
3580
params = Dict{String, Any}()
3681
max_results === nothing || (params["maxResults"] = max_results)
@@ -40,6 +85,11 @@ function search_devices(filters=[]; max_results::Maybe{Int}=nothing, next_token:
4085
return map(parse_device_info, d["devices"]), get(d, "nextToken", nothing)
4186
end
4287

88+
"""
89+
make_device_parameters(program::Schema.Program, arn::String, disable_qubit_rewiring::Bool)
90+
91+
Create device parameters from given `program`, device `arn` and `disable_qubit_rewiring` option.
92+
"""
4393
function make_device_parameters(program::Schema.Program, arn::String, disable_qubit_rewiring::Bool)
4494
paradigm_parameters = Schema.GateModelParameters(
4595
qubitCount=Schema.count_qubits(program),
@@ -63,6 +113,29 @@ function make_device_parameters(program::Schema.Program, arn::String, disable_qu
63113
return device_parameters
64114
end
65115

116+
"""
117+
create_quantum_task(;kw...)
118+
119+
Create a quantum task in braket service.
120+
121+
# Required Keyword Arguments
122+
123+
- `program::Schema.Program`: the program one wants to execute.
124+
- `device_arn::String`: device arn.
125+
- `bucket::String`: S3 bucket to store the results in.
126+
- `folder::String`: S3 bucket folder.
127+
128+
# Optional Keyword Arguments
129+
130+
- `disable_qubit_rewiring::Bool`: disable qubit rewiring in braket service, default is `false`.
131+
- `device_parameters`: device parameters, such as [`Schema.IonqDeviceParameters`](@ref),
132+
[`Schema.RigettiDeviceParameters`](@ref), default is inferred from previous arguments.
133+
- `nshots`: number of shots, default is `100`.
134+
- `client_token`: a `UUID` for the client token, will generate one by default.
135+
- `tags::Dict{String, String}`: a list of tags you would to attach to this task.
136+
137+
$(DOC_AWS_CONFIG)
138+
"""
66139
function create_quantum_task(;
67140
program::Schema.Program,
68141
device_arn::String,
@@ -73,6 +146,7 @@ function create_quantum_task(;
73146
nshots::Int = 100,
74147
client_token::UUID = uuid1(),
75148
tags::Dict{String, String} = Dict{String, String}(),
149+
aws_config=AWS.global_aws_config(),
76150
)
77151

78152
response = Braket.create_quantum_task(
@@ -85,17 +159,39 @@ function create_quantum_task(;
85159
Dict(
86160
"deviceParameters" => JSON.json(to_dict(device_parameters; include_defaults=true, exclude_nothing=true)),
87161
"tags" => tags,
88-
),
162+
);
163+
aws_config
89164
)
90165
return response["quantumTaskArn"], response["status"]
91166
end
92167

93-
function get_quantum_task(task_arn::String)
94-
from_dict(Schema.BraketTaskInfo, Braket.get_quantum_task(HTTP.escapeuri(task_arn)))
168+
"""
169+
get_quantum_task(task_arn::String)
170+
171+
Get the quantum task from `task_arn`.
172+
173+
$(DOC_AWS_CONFIG)
174+
"""
175+
function get_quantum_task(task_arn::String; aws_config=AWS.global_aws_config())
176+
from_dict(
177+
Schema.BraketTaskInfo,
178+
Braket.get_quantum_task(HTTP.escapeuri(task_arn); aws_config)
179+
)
95180
end
96181

97-
function cancel_quantum_task(client_token::String, task_arn::String)
98-
Braket.cancel_quantum_task(client_token, HTTP.escapeuri(task_arn))
182+
"""
183+
cancel_quantum_task(client_token::String, task_arn::String)
184+
185+
Cancel quantum task given by `client_token` and its `task_arn`.
186+
187+
$(DOC_AWS_CONFIG)
188+
"""
189+
function cancel_quantum_task(client_token::String, task_arn::String; aws_config=AWS.global_aws_config())
190+
Braket.cancel_quantum_task(
191+
client_token,
192+
HTTP.escapeuri(task_arn);
193+
aws_config
194+
)
99195
end
100196

101197
using Crayons.Box

src/menu.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ mutable struct DeviceMenu <: TerminalMenus.AbstractMenu
77
selected::Int
88
end
99

10+
"""
11+
DeviceMenu([devices, regions]; pagesize::Int=5)
12+
13+
Create a DeviceMenu to display available devices interactively in the Julia REPL.
14+
15+
# Arguments
16+
17+
- `devices::Vector{Schema.DeviceInfo}`: optional, a list of devices.
18+
- `regions::Vector{String}`: optional, a list of regions corresponding to the device list.
19+
"""
1020
function DeviceMenu(; pagesize::Int=5)
1121
all_devices = Schema.DeviceInfo[]
1222
regions = String[]

test/runtests.jl

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,6 @@
1-
using GarishPrint
2-
using AWSBraket
31
using Test
42

5-
using JSON
6-
using HTTP
7-
using Configurations
8-
using AWS
9-
using AWS: @service
10-
11-
@service Braket
12-
13-
14-
info = AWSBraket.get_device("arn:aws:braket:::device/quantum-simulator/amazon/sv1")
15-
pprint(info)
16-
17-
dev, _ = AWSBraket.search_devices(;aws_config=AWS.global_aws_config(region="us-west-1"))
18-
pprint(dev[1])
19-
20-
dev[1].deviceArn
21-
dev[2].deviceArn
22-
dev[3].deviceArn
23-
dev[4].deviceArn
24-
25-
26-
Braket.search_quantum_tasks([])
27-
Braket.create_quantum_task
28-
29-
using AWSBraket.Schema
30-
31-
dev = Braket.search_devices([]; aws_config=AWS.global_aws_config(region="us-east-1"))["devices"]
32-
map(dev) do d
33-
from_dict(Schema.DeviceInfo, d)
34-
end
35-
36-
from_dict(Schema.DeviceInfo, dev[1])
37-
38-
AWS.global_aws_config(region="us-east-1")
3+
# https://github.com/JuliaTesting/BrokenRecord.jl/issues/20
4+
# @testset "task" begin
5+
# include("task.jl")
6+
# end

test/task.jl

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
using BrokenRecord
12
using AWSBraket
23
using AWSBraket.Schema
34
using Configurations
45
using JSON
6+
using HTTP
7+
using Test
8+
using AWS
9+
using AWS: @service
10+
@service Braket
11+
@service S3
12+
13+
BrokenRecord.configure!(path=joinpath(pkgdir(AWSBraket), "test", "records"), ignore_headers=["Authorization"])
514

615
bell = Schema.Program(;
716
instructions=[
@@ -10,25 +19,33 @@ bell = Schema.Program(;
1019
],
1120
)
1221

13-
task, status = AWSBraket.create_quantum_task(;
14-
program=bell,
15-
device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1",
16-
bucket="amazon-braket-8865d8c99645",
17-
folder="braket",
18-
)
22+
task, status = BrokenRecord.playback("create_quantum_task.json") do
23+
AWSBraket.create_quantum_task(;
24+
program=bell,
25+
device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1",
26+
bucket="amazon-braket-8865d8c99645",
27+
folder="braket",
28+
)
29+
end
1930

20-
info = AWSBraket.get_quantum_task(task)
21-
# AWSBraket.cancel_quantum_task(info.id, task)
31+
info = BrokenRecord.playback("get_quantum_task.json") do
32+
AWSBraket.get_quantum_task(task)
33+
end
2234

23-
using HTTP
24-
using AWS: @service
25-
@service Braket
26-
@service S3
2735

28-
# S3.list_objects(info.outputS3Bucket)["Contents"]
29-
content = S3.get_object(info.outputS3Bucket, info.outputS3Directory * "/results.json")
30-
JSON.parse(String(content))
36+
@test_throws AWS.AWSExceptions.AWSException BrokenRecord.playback("cancel_quantum_task.json") do
37+
AWSBraket.cancel_quantum_task(info.id, task)
38+
end
39+
40+
content = BrokenRecord.playback("get_result_object.json") do
41+
S3.get_object(info.outputS3Bucket, info.outputS3Directory * "/results.json")
42+
end
43+
44+
result = JSON.parse(String(content))
45+
result["measurements"]
3146

47+
48+
# NOTE: the results type is broken currently
3249
# bell = Schema.Program(;
3350
# instructions=[
3451
# Schema.H(;target=0),
@@ -42,24 +59,3 @@ JSON.parse(String(content))
4259
# )
4360
# ]
4461
# )
45-
46-
# deviceParameters = Schema.GateModelSimulatorDeviceParameters(
47-
# paradigmParameters=Schema.GateModelParameters(
48-
# qubitCount=2, disableQubitRewiring=false,
49-
# )
50-
# )
51-
52-
# JSON.json(to_dict(bell; include_defaults=true, exclude_nothing=true), 2)|>print
53-
54-
# # direct call
55-
# task = Braket.create_quantum_task(
56-
# JSON.json(to_dict(bell; include_defaults=true, exclude_nothing=true)),
57-
# string(uuid1()),
58-
# "arn:aws:braket:::device/quantum-simulator/amazon/sv1",
59-
# "amazon-braket-8865d8c99645",
60-
# "braket",
61-
# 100,
62-
# Dict(
63-
# "deviceParameters" => JSON.json(to_dict(deviceParameters; include_defaults=true)),
64-
# ),
65-
# )

0 commit comments

Comments
 (0)