Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handling of optional arguments #13

Merged
merged 12 commits into from
May 23, 2021
3 changes: 2 additions & 1 deletion src/YaoBlocksQobj.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module YaoBlocksQobj

export create_qobj
export convert_to_qobj

using IBMQClient
using IBMQClient.Schema
using YaoBlocks

Expand Down
52 changes: 21 additions & 31 deletions src/qobj.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
using Random

IBMQClient.@option struct ExpOptions
id::String
header::IBMQClient.Maybe{Dict} = nothing
nshots::Int = 1024
exp_header::IBMQClient.Maybe{Vector} = nothing
exp_config::IBMQClient.Maybe{Vector} = nothing
end

"""
create_qobj(qc, id, header, nshots, exp_header, exp_config)
convert_to_qobj(qc, id, header, nshots, exp_header, exp_config)

Creates a `Qobj` based on the IBMQClient schema.

Expand All @@ -20,10 +28,13 @@ using Random
- `exp_config` (optional): An Array of Configuration structure for user settings that can be different in each
experiment. These will override the configuration settings of the whole job.
"""
function create_qobj(qc::Array{<:AbstractBlock{N}}; id::String = randstring(), header = nothing, nshots::Int = 1024, exp_header = nothing, exp_config = nothing) where N
experiments = create_experiment(qc, exp_header, exp_config)
config = ExpConfig(shots = nshots, memory_slots = length(experiments))
Qobj(;qobj_id = id, type = "QASM", schema_version = v"1", header, experiments = experiments, config = config)

convert_to_qobj(qc::Vector{<:AbstractBlock}; kw...) = convert_to_qobj(qc, ExpOptions(;kw...))

function convert_to_qobj(qc::Vector{<:AbstractBlock{N}}, options::ExpOptions) where N
experiments = create_experiment(qc, options.exp_header, options.exp_config)
config = ExpConfig(shots = options.nshots, memory_slots = length(experiments)) #from the schema
Qobj(;qobj_id = options.id, type = "QASM", schema_version = v"1", options.header, experiments, config)
end

"""
Expand All @@ -41,33 +52,12 @@ end
- `exp_config` (optional): An Array of Configuration structure for user settings that can be different in each
experiment. These will override the configuration settings of the whole job.
"""
function create_experiment(qc::Array{<:AbstractBlock{N}}, exp_header = nothing, exp_config = nothing) where N
experiments = Experiment[]
head = false
config = false
if exp_header !== nothing
head = true
elseif exp_config !== nothing
config = true
end

for i in 1:length(qc)
if head && config
exp = create_experiment!(qc[i], exp_header[i], exp_config[i])
elseif head && !config
exp = create_experiment!(qc[i], exp_header[i], exp_config)
elseif !head && config
exp = create_experiment!(qc[i], exp_header, exp_config[i])
else
exp = create_experiment!(qc[i], exp_header, exp_config)
end

push!(experiments, exp)
end
return experiments
end
create_experiment(qc::Vector{<:AbstractBlock{N}}, exp_header::Nothing, exp_config::Nothing) where N = collect(create_experiment!(qc[i], nothing, nothing) for i in 1:length(qc))
create_experiment(qc::Vector{<:AbstractBlock{N}}, exp_header::Array, exp_config::Array) where N = collect(create_experiment!(qc[i], exp_header[i], exp_config[i]) for i in 1:length(qc))
create_experiment(qc::Vector{<:AbstractBlock{N}}, exp_header::Nothing, exp_config::Array) where N = collect(create_experiment!(qc[i], nothing, exp_config[i]) for i in 1:length(qc))
create_experiment(qc::Vector{<:AbstractBlock{N}}, exp_header::Array, exp_config::Nothing) where N = collect(create_experiment!(qc[i], exp_header[i], nothing) for i in 1:length(qc))

function create_experiment!(qc::AbstractBlock{N}, exp_header = nothing, exp_config = nothing) where N
function create_experiment!(qc::AbstractBlock{N}, exp_header, exp_config) where N
exp_inst = generate_inst(qc)
experiment = Experiment(;header = exp_header, config = exp_config, instructions = exp_inst)
return experiment
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include("qobjtoqbir.jl")
exp_header = [Dict("description"=>"1"), Dict("description"=>"2")]

circuits = [qc, qc1]
q = create_qobj(circuits, id = "test", header = header, exp_header = exp_header)
q = convert_to_qobj(circuits, id = "test", header = header, exp_header = exp_header)

experiments = q.experiments
for i in 1:length(experiments)
Expand Down