Skip to content

Commit

Permalink
fix: More coverage/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws committed Sep 25, 2024
1 parent 33aee72 commit 890698b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
13 changes: 7 additions & 6 deletions src/Quasar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,8 @@ struct Qubit
size::Int
end

const CircuitInstruction = @NamedTuple begin type::String; arguments::Vector{Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}}; targets::Vector{Int}; controls::Vector{Pair{Int, Int}}; exponent::Float64 end
const InstructionArgument = Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}
const CircuitInstruction = @NamedTuple begin type::String; arguments::Vector{InstructionArgument}; targets::Vector{Int}; controls::Vector{Pair{Int, Int}}; exponent::Float64 end
const CircuitResult = @NamedTuple begin type::Symbol; operator::Vector{Union{String, Matrix{ComplexF64}}}; targets::Vector{Int}; states::Vector{String}; end

abstract type AbstractGateDefinition end
Expand Down Expand Up @@ -1282,7 +1283,7 @@ function remap(ix, target_mapper::Dict{Int, Int})
return (type=ix.type, arguments=ix.arguments, targets=mapped_targets, controls=mapped_controls, exponent=ix.exponent)
end
function bind_arguments!(ix::CircuitInstruction, argument_values::Dict{Symbol, <:Real})
new_arguments = Union{Symbol, Dates.Period,Real,Matrix{ComplexF64}}[get(argument_values, arg, arg) for arg in ix.arguments]
new_arguments = InstructionArgument[get(argument_values, arg, arg) for arg in ix.arguments]
return (type=ix.type, arguments=new_arguments, targets=ix.targets, controls=ix.controls, exponent=ix.exponent)
end

Expand Down Expand Up @@ -1420,21 +1421,21 @@ function (v::AbstractVisitor)(program_expr::QasmExpression)
elseif head(program_expr) == :reset
targets = program_expr.args[1]::QasmExpression
target_qubits = evaluate_qubits(v, targets)
ixs = [(type="reset", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[t], controls=Pair{Int, Int}[], exponent=1.0) for t in target_qubits]
ixs = [(type="reset", arguments=InstructionArgument[], targets=[t], controls=Pair{Int, Int}[], exponent=1.0) for t in target_qubits]
push!(v, ixs)
return v
elseif head(program_expr) == :barrier
targets = program_expr.args[1]::QasmExpression
target_qubits = evaluate_qubits(v, targets)
ixs = [(type="barrier", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[t], controls=Pair{Int, Int}[], exponent=1.0) for t in target_qubits]
ixs = [(type="barrier", arguments=InstructionArgument[], targets=[t], controls=Pair{Int, Int}[], exponent=1.0) for t in target_qubits]
push!(v, ixs)
return v
elseif head(program_expr) == :delay
duration_expr = program_expr.args[1].args[1]::QasmExpression
targets = program_expr.args[2].args[1]::QasmExpression
target_qubits = evaluate_qubits(v, targets)
duration = v(duration_expr)
ixs = [(type="delay", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[duration], targets=[t], controls=Pair{Int, Int}[], exponent=1.0) for t in target_qubits]
ixs = [(type="delay", arguments=InstructionArgument[duration], targets=[t], controls=Pair{Int, Int}[], exponent=1.0) for t in target_qubits]
push!(v, ixs)
return v
elseif head(program_expr) == :stretch
Expand Down Expand Up @@ -1750,7 +1751,7 @@ function (v::AbstractVisitor)(program_expr::QasmExpression)
return ()
elseif head(program_expr) == :measure
qubits_to_measure = evaluate_qubits(v, program_expr.args[1])
push!(v, CircuitInstruction[(type="measure", arguments=Union{Symbol, Dates.Period,Real,Matrix{ComplexF64}}[], targets=[q], controls=Pair{Int,Int}[], exponent=1.0) for q in qubits_to_measure])
push!(v, CircuitInstruction[(type="measure", arguments=InstructionArgument[], targets=[q], controls=Pair{Int,Int}[], exponent=1.0) for q in qubits_to_measure])
return false
elseif head(program_expr) == :hw_qubit
return tryparse(Int, name(program_expr))
Expand Down
76 changes: 38 additions & 38 deletions src/builtin_gates.jl
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
# OpenQASM 3 Braket Standard Gates
builtin_gates() = Dict{String, BuiltinGateDefinition}(
# identity gate
"i"=>BuiltinGateDefinition("i", String[], ["a"], (type="i", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"i"=>BuiltinGateDefinition("i", String[], ["a"], (type="i", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# phase gate
"phaseshift"=>BuiltinGateDefinition("phaseshift", ["λ"], ["a"], (type="phaseshift", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"phaseshift"=>BuiltinGateDefinition("phaseshift", ["λ"], ["a"], (type="phaseshift", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# pauli X gate
"x"=>BuiltinGateDefinition("x", String[], ["a"], (type="x", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"x"=>BuiltinGateDefinition("x", String[], ["a"], (type="x", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# pauli Y gate
"y"=>BuiltinGateDefinition("y", String[], ["a"], (type="y", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"y"=>BuiltinGateDefinition("y", String[], ["a"], (type="y", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# pauli Z gate
"z"=>BuiltinGateDefinition("z", String[], ["a"], (type="z", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"z"=>BuiltinGateDefinition("z", String[], ["a"], (type="z", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# Hadamard gate
"h"=>BuiltinGateDefinition("h", String[], ["a"], (type="h", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"h"=>BuiltinGateDefinition("h", String[], ["a"], (type="h", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# S gate
"s"=>BuiltinGateDefinition("s", String[], ["a"], (type="s", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"s"=>BuiltinGateDefinition("s", String[], ["a"], (type="s", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# Si gate
"si"=>BuiltinGateDefinition("si", String[], ["a"], (type="si", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"si"=>BuiltinGateDefinition("si", String[], ["a"], (type="si", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# T gate
"t"=>BuiltinGateDefinition("t", String[], ["a"], (type="t", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"t"=>BuiltinGateDefinition("t", String[], ["a"], (type="t", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# Ti gate
"ti"=>BuiltinGateDefinition("ti", String[], ["a"], (type="ti", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"ti"=>BuiltinGateDefinition("ti", String[], ["a"], (type="ti", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# V gate
"v"=>BuiltinGateDefinition("v", String[], ["a"], (type="v", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"v"=>BuiltinGateDefinition("v", String[], ["a"], (type="v", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# Vi gate
"vi"=>BuiltinGateDefinition("vi", String[], ["a"], (type="vi", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"vi"=>BuiltinGateDefinition("vi", String[], ["a"], (type="vi", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# RotX gate
"rx"=>BuiltinGateDefinition("rx", ["θ"], ["a"], (type="rx", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"rx"=>BuiltinGateDefinition("rx", ["θ"], ["a"], (type="rx", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# RotY gate
"ry"=>BuiltinGateDefinition("ry", ["θ"], ["a"], (type="ry", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"ry"=>BuiltinGateDefinition("ry", ["θ"], ["a"], (type="ry", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# RotZ gate
"rz"=>BuiltinGateDefinition("rz", ["θ"], ["a"], (type="rz", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"rz"=>BuiltinGateDefinition("rz", ["θ"], ["a"], (type="rz", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# CNot gate
"cnot"=>BuiltinGateDefinition("cnot", String[], ["a", "b"], (type="cnot", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cnot"=>BuiltinGateDefinition("cnot", String[], ["a", "b"], (type="cnot", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# CY gate
"cy"=>BuiltinGateDefinition("cy", String[], ["a", "b"], (type="cy", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cy"=>BuiltinGateDefinition("cy", String[], ["a", "b"], (type="cy", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# CZ gate
"cz"=>BuiltinGateDefinition("cz", String[], ["a", "b"], (type="cz", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cz"=>BuiltinGateDefinition("cz", String[], ["a", "b"], (type="cz", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# CV gate
"cv"=>BuiltinGateDefinition("cv", String[], ["a", "b"], (type="cv", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cv"=>BuiltinGateDefinition("cv", String[], ["a", "b"], (type="cv", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# controlled-phase
"cphaseshift"=>BuiltinGateDefinition("cphaseshift", ["λ"], ["a", "b"], (type="cphaseshift", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cphaseshift"=>BuiltinGateDefinition("cphaseshift", ["λ"], ["a", "b"], (type="cphaseshift", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# controlled-phase-00
"cphaseshift00"=>BuiltinGateDefinition("cphaseshift00", ["λ"], ["a", "b"], (type="cphaseshift00", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cphaseshift00"=>BuiltinGateDefinition("cphaseshift00", ["λ"], ["a", "b"], (type="cphaseshift00", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# controlled-phase-01
"cphaseshift01"=>BuiltinGateDefinition("cphaseshift01", ["λ"], ["a", "b"], (type="cphaseshift01", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cphaseshift01"=>BuiltinGateDefinition("cphaseshift01", ["λ"], ["a", "b"], (type="cphaseshift01", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# controlled-phase-10
"cphaseshift10"=>BuiltinGateDefinition("cphaseshift10", ["λ"], ["a", "b"], (type="cphaseshift10", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"cphaseshift10"=>BuiltinGateDefinition("cphaseshift10", ["λ"], ["a", "b"], (type="cphaseshift10", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# Swap gate
"swap"=>BuiltinGateDefinition("swap", String[], ["a", "b"], (type="swap", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"swap"=>BuiltinGateDefinition("swap", String[], ["a", "b"], (type="swap", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# ISwap gate
"iswap"=>BuiltinGateDefinition("iswap", String[], ["a", "b"], (type="iswap", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"iswap"=>BuiltinGateDefinition("iswap", String[], ["a", "b"], (type="iswap", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# ISwap gate
"pswap"=>BuiltinGateDefinition("pswap", ["θ"], ["a", "b"], (type="pswap", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"pswap"=>BuiltinGateDefinition("pswap", ["θ"], ["a", "b"], (type="pswap", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# controlled-swap gate
"cswap"=>BuiltinGateDefinition("cswap", String[], ["a", "b", "c"], (type="cswap", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1, 2], controls=Pair{Int,Int}[], exponent=1.0)),
"cswap"=>BuiltinGateDefinition("cswap", String[], ["a", "b", "c"], (type="cswap", arguments=InstructionArgument[], targets=[0, 1, 2], controls=Pair{Int,Int}[], exponent=1.0)),
# ccnot/Toffoli gate
"ccnot"=>BuiltinGateDefinition("ccnot", String[], ["a", "b", "c"], (type="ccnot", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1, 2], controls=Pair{Int,Int}[], exponent=1.0)),
"ccnot"=>BuiltinGateDefinition("ccnot", String[], ["a", "b", "c"], (type="ccnot", arguments=InstructionArgument[], targets=[0, 1, 2], controls=Pair{Int,Int}[], exponent=1.0)),
# XX gate
"xx"=>BuiltinGateDefinition("xx", ["θ"], ["a", "b"], (type="xx", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"xx"=>BuiltinGateDefinition("xx", ["θ"], ["a", "b"], (type="xx", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# XY gate
"xy"=>BuiltinGateDefinition("xy", ["θ"], ["a", "b"], (type="xy", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"xy"=>BuiltinGateDefinition("xy", ["θ"], ["a", "b"], (type="xy", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# YY gate
"yy"=>BuiltinGateDefinition("yy", ["θ"], ["a", "b"], (type="yy", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"yy"=>BuiltinGateDefinition("yy", ["θ"], ["a", "b"], (type="yy", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# ZZ gate
"zz"=>BuiltinGateDefinition("zz", ["θ"], ["a", "b"], (type="zz", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"zz"=>BuiltinGateDefinition("zz", ["θ"], ["a", "b"], (type="zz", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# ECR gate
"ecr"=>BuiltinGateDefinition("ecr", String[], ["a", "b"], (type="ecr", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"ecr"=>BuiltinGateDefinition("ecr", String[], ["a", "b"], (type="ecr", arguments=InstructionArgument[], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# MS gate
"ms"=>BuiltinGateDefinition("ms", ["ϕ", "θ", "λ"], ["a", "b"], (type="ms", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[, , ], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
"ms"=>BuiltinGateDefinition("ms", ["ϕ", "θ", "λ"], ["a", "b"], (type="ms", arguments=InstructionArgument[, , ], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0)),
# GPi gate
"gpi"=>BuiltinGateDefinition("gpi", ["θ"], ["a"], (type="gpi", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"gpi"=>BuiltinGateDefinition("gpi", ["θ"], ["a"], (type="gpi", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# GPi2 gate
"gpi2"=>BuiltinGateDefinition("gpi2", ["θ"], ["a"], (type="gpi2", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"gpi2"=>BuiltinGateDefinition("gpi2", ["θ"], ["a"], (type="gpi2", arguments=InstructionArgument[], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# PRx gate
"prx"=>BuiltinGateDefinition("prx", ["θ", "ϕ"], ["a"], (type="prx", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[, ], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"prx"=>BuiltinGateDefinition("prx", ["θ", "ϕ"], ["a"], (type="prx", arguments=InstructionArgument[, ], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
# 3-angle U gate
"U"=>BuiltinGateDefinition("U", ["θ", "ϕ", "λ"], ["a"], (type="u", arguments=Union{Symbol, Dates.Period, Real, Matrix{ComplexF64}}[, , ], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
"U"=>BuiltinGateDefinition("U", ["θ", "ϕ", "λ"], ["a"], (type="u", arguments=InstructionArgument[, , ], targets=[0], controls=Pair{Int,Int}[], exponent=1.0)),
)
Loading

0 comments on commit 890698b

Please sign in to comment.