Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/codegen/qobj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ function codegen_ifnot(::TargetQobjQASM, ci::CodeInfo, st::CodeGenQobjState)
error("classical post processing measurement result in Qobj is not supported")
condition = st.register_map[creg.id]
else
error("invalid value for measurement result: $creg, possible fix: run :julia optimization pass before codegen")
error(
"invalid value for measurement result: $creg, possible fix: run :julia optimization pass before codegen",
)
end

length(condition) == 1 || error("Qobj only allows single bit condition for if statement")
Expand Down
29 changes: 17 additions & 12 deletions src/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ Base.iterate(p::Core.Compiler.Pair, st) = Core.Compiler.iterate(p, st)

Base.getindex(m::Core.Compiler.MethodLookupResult, idx::Int) = Core.Compiler.getindex(m, idx)

function Core.Compiler.optimize(interp::YaoInterpreter, opt::OptimizationState, params::OptimizationParams, @nospecialize(result))
function Core.Compiler.optimize(
interp::YaoInterpreter,
opt::OptimizationState,
params::OptimizationParams,
@nospecialize(result)
)
nargs = Int(opt.nargs) - 1
@timeit "optimizer" ir = Core.Compiler.run_passes(opt.src, nargs, opt)

# make sure all const are inlined
# Julia itself may not inline all
# the const values we want, e.g gates
Expand Down Expand Up @@ -204,31 +209,31 @@ function inline_const!(ir::IRCode)
break
end
end

if allconst &&
isa(f, Core.IntrinsicFunction) &&
is_pure_intrinsic_infer(f) &&
intrinsic_nothrow(f, atypes[2:end])

fargs = anymap(x::Const -> x.val, atypes[2:end])
val = f(fargs...)
ir.stmts[i][:inst] = quoted(val)
ir.stmts[i][:type] = Const(val)
elseif allconst && isa(f, Core.Builtin) &&
(f === Core.tuple || f === Core.getfield)
elseif allconst && isa(f, Core.Builtin) && (f === Core.tuple || f === Core.getfield)
fargs = anymap(x::Const -> x.val, atypes[2:end])
val = f(fargs...)
ir.stmts[i][:inst] = quoted(val)
ir.stmts[i][:type] = Const(val)
end
elseif stmt.head === :new
exargs = stmt.args[2:end]
allconst = all(arg->is_arg_allconst(ir, arg), exargs)
allconst = all(arg -> is_arg_allconst(ir, arg), exargs)
t = stmt.args[1]
if allconst && isconcretetype(t) && !t.mutable
args = anymap(arg->unwrap_arg(ir, arg), exargs)
val = ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), t, args, length(args))

args = anymap(arg -> unwrap_arg(ir, arg), exargs)
val =
ccall(:jl_new_structv, Any, (Any, Ptr{Cvoid}, UInt32), t, args, length(args))

ir.stmts[i][:inst] = quoted(val)
ir.stmts[i][:type] = Const(val)
end
Expand All @@ -245,11 +250,11 @@ function elim_map_check!(ir::IRCode)

if stmt.head === :invoke && stmt.args[2] === GlobalRef(YaoLocations, :map_check)
exargs = stmt.args[3:end]
allconst = all(arg->is_arg_allconst(ir, arg), exargs)
allconst = all(arg -> is_arg_allconst(ir, arg), exargs)


if allconst
args = anymap(arg->unwrap_arg(ir, arg), exargs)
args = anymap(arg -> unwrap_arg(ir, arg), exargs)
val = map_check_nothrow(args[1], args[2])

ir.stmts[i][:inst] = quoted(val)
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ function execute(stub::typeof(Semantic.barrier), r::TraceTape, loc::Locations)
end

function execute(stub::typeof(Semantic.measure), r::TraceTape, locs::Locations)
error("one should not trace programs contain @measure, since we cannot purify this hybrid program")
error(
"one should not trace programs contain @measure, since we cannot purify this hybrid program",
)
end

function trace_m(ex)
Expand Down
2 changes: 1 addition & 1 deletion test/compiler/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ end
3 => post()
end
3 => rx(1.0)
return (c=c, )
return (c = c,)
end

@testset "branches & const inline" begin
Expand Down
10 changes: 9 additions & 1 deletion test/compiler/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ end
@testset "gate_count" begin
@test gate_count(demo_circ_simp()) == Dict(
:ctrl => IdDict(Z => 1, X => 4),
:gate => IdDict(T => 2, H => 8, Rx(π / 4) => 1, X => 2, S => 4, shift(7π/4) => 1, shift(3π/2)=>1),
:gate => IdDict(
T => 2,
H => 8,
Rx(π / 4) => 1,
X => 2,
S => 4,
shift(7π / 4) => 1,
shift(3π / 2) => 1,
),
)
end

Expand Down