Skip to content

Commit

Permalink
fix: Use the IR's inputs if it has them for OQ3 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws authored Jul 8, 2024
1 parent 4127749 commit 83850ed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/BraketSimulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ end
shots::Int = 0;
kwargs...,
)
inputs = get(kwargs, :inputs, circuit_ir.inputs)
inputs = isnothing(inputs) ? Dict{String, Float64}() : Dict{String, Any}(k=>v for (k,v) in inputs)
ir_inputs = isnothing(circuit_ir.inputs) || isempty(circuit_ir.inputs) ? Dict{String, Float64}() : circuit_ir.inputs
inputs = get(kwargs, :inputs, ir_inputs)
inputs = isnothing(inputs) || isempty(inputs) ? ir_inputs : Dict{String, Any}(k=>v for (k,v) in inputs)
circuit = Circuit(circuit_ir.source, inputs)
measure_ixs = splice!(circuit.instructions, findall(ix->ix.operator isa Measure, circuit.instructions))
measure_targets = (convert(Vector{Int}, measure.target) for measure in measure_ixs)
Expand Down
47 changes: 47 additions & 0 deletions test/test_sv_simulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,51 @@ LARGE_TESTS = get(ENV, "BRAKET_SV_LARGE_TESTS", false)
"zz",
]
end
@testset "inputs handling" begin
sv_adder_qasm = """
OPENQASM 3;
input uint[4] a_in;
input uint[4] b_in;
gate majority a, b, c {
cnot c, b;
cnot c, a;
ccnot a, b, c;
}
gate unmaj a, b, c {
ccnot a, b, c;
cnot c, a;
cnot a, b;
}
qubit cin;
qubit[4] a;
qubit[4] b;
qubit cout;
// set input states
for int[8] i in [0: 3] {
if(bool(a_in[i])) x a[i];
if(bool(b_in[i])) x b[i];
}
// add a to b, storing result in b
majority cin, b[3], a[3];
for int[8] i in [3: -1: 1] { majority a[i], b[i - 1], a[i - 1]; }
cnot a[0], cout;
for int[8] i in [1: 3] { unmaj a[i], b[i - 1], a[i - 1]; }
unmaj cin, b[3], a[3];
// todo: subtle bug when trying to get a result type for both at once
#pragma braket result probability cout, b
#pragma braket result probability cout
#pragma braket result probability b
"""
simulator = StateVectorSimulator(6, 0)
oq3_program = Braket.OpenQasmProgram(Braket.header_dict[Braket.OpenQasmProgram], sv_adder_qasm, Dict("a_in"=>3, "b_in"=>7))
# should NOT throw a missing input error
simulate(simulator, oq3_program, 0; inputs=Dict{String, Float64}())
end
end

0 comments on commit 83850ed

Please sign in to comment.