Skip to content

Commit

Permalink
Merge pull request #5 from kshyatt-aws/ksh/printing
Browse files Browse the repository at this point in the history
fix: Fix and more tests for proper printing of sized types
  • Loading branch information
kshyatt-aws authored Oct 28, 2024
2 parents 9d27e63 + b42e279 commit faf8911
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
15 changes: 15 additions & 0 deletions src/Quasar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,23 @@ end
Base.length(s::SizedBitVector) = s.size
Base.size(s::SizedBitVector) = (s.size,)
Base.show(io::IO, s::SizedBitVector) = print(io, "SizedBitVector{$(s.size.args[end])}")
Base.iterate(s::SizedBitVector) = nothing
Base.iterate(s::SizedBitVector, ::Nothing) = nothing
struct SizedInt <: Integer
size::QasmExpression
SizedInt(size::QasmExpression) = new(size)
SizedInt(sint::SizedInt) = new(sint.size)
end
Base.iterate(s::SizedInt) = nothing
Base.iterate(s::SizedInt, ::Nothing) = nothing
Base.show(io::IO, s::SizedInt) = print(io, "SizedInt{$(s.size.args[end])}")
struct SizedUInt <: Unsigned
size::QasmExpression
SizedUInt(size::QasmExpression) = new(size)
SizedUInt(suint::SizedUInt) = new(suint.size)
end
Base.iterate(s::SizedUInt) = nothing
Base.iterate(s::SizedUInt, ::Nothing) = nothing
Base.show(io::IO, s::SizedUInt) = print(io, "SizedUInt{$(s.size.args[end])}")
struct SizedFloat <: AbstractFloat
size::QasmExpression
Expand All @@ -332,12 +338,16 @@ struct SizedAngle <: AbstractFloat
SizedAngle(size::QasmExpression) = new(size)
SizedAngle(sangle::SizedAngle) = new(sangle.size)
end
Base.iterate(s::SizedAngle) = nothing
Base.iterate(s::SizedAngle, ::Nothing) = nothing
Base.show(io::IO, s::SizedAngle) = print(io, "SizedAngle{$(s.size.args[end])}")
struct SizedComplex <: Number
size::QasmExpression
SizedComplex(size::QasmExpression) = new(size)
SizedComplex(scomplex::SizedComplex) = new(scomplex.size)
end
Base.iterate(s::SizedComplex) = nothing
Base.iterate(s::SizedComplex, ::Nothing) = nothing
Base.show(io::IO, s::SizedComplex) = print(io, "SizedComplex{$(s.size.args[end])}")

struct SizedArray{T,N} <: AbstractArray{T, N}
Expand All @@ -352,10 +362,15 @@ function SizedArray(eltype::QasmExpression, size::QasmExpression)
end
return SizedArray(eltype.args[1], arr_size)
end
Base.iterate(s::SizedArray) = nothing
Base.iterate(s::SizedArray, ::Nothing) = nothing
Base.show(io::IO, s::SizedArray{T, N}) where {T, N} = print(io, "SizedArray{$(sprint(show, s.type)), $N}")
Base.size(a::SizedArray{T, N}, dim::Int=0) where {T, N} = a.size[dim+1]

const SizedNumber = Union{SizedComplex, SizedAngle, SizedFloat, SizedInt, SizedUInt}
if v"1.9" <= VERSION < v"1.11"
Base.Iterators.iterlength(s::Union{SizedNumber, SizedBitVector, SizedArray}) = -1
end

function parse_classical_type(tokens, stack, start, qasm)
is_sized = length(tokens) > 1 && tokens[2][end] == lbracket
Expand Down
39 changes: 25 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,20 @@ Quasar.builtin_gates[] = complex_builtin_gates
@test visitor.classical_defs["b"].val == to_value
end
end
@testset "Numbers $qasm_str" for (qasm_str, var_name, output_val) in (("float[32] a = 1.24e-3;", "a", 1.24e-3),
("complex[float] b = 1-0.23im;", "b", 1-0.23im),
("const bit c = \"0\";", "c", falses(1)),
("bool d = false;", "d", false),
("complex[float] e = -0.23+2im;", "e", -0.23+2im),
("uint f = 0x123456789abcdef;", "f", 0x123456789abcdef),
("int g = 0o010;", "g", 0o010),
("float[64] h = 2*π;", "h", 2π),
("float[64] i = τ/2;", "i", Float64(π)),
("complex[float] j = 0.23im;", "j", 0.23im),
("complex[float] k = -0.23im;", "k", -0.23im),
("complex[float] l = 2im;", "l", 2*im),
("complex[float] m = -0.23 + -2.0im;", "m", -0.23-2.0*im),
)
@testset "Numbers $qasm_str" for (qasm_str, var_name, output_val, type_name) in (("float[32] a = 1.24e-3;", "a", 1.24e-3, "SizedFloat{32}"),
("complex[float] b = 1-0.23im;", "b", 1-0.23im, "SizedComplex{-1}"),
("bit c = \"0\";", "c", falses(1), "SizedBitVector{-1}"),
("bool d = false;", "d", false, "Bool"),
("complex[float] e = -0.23+2im;", "e", -0.23+2im, "SizedComplex{-1}"),
("uint f = 0x123456789abcdef;", "f", 0x123456789abcdef, "SizedUInt{-1}"),
("int g = 0o010;", "g", 0o010, "SizedInt{-1}"),
("float[64] h = 2*π;", "h", 2π, "SizedFloat{64}"),
("float[64] i = τ/2;", "i", Float64(π), "SizedFloat{64}"),
("complex[float] j = 0.23im;", "j", 0.23im, "SizedComplex{-1}"),
("complex[float] k = -0.23im;", "k", -0.23im, "SizedComplex{-1}"),
("complex[float] l = 2im;", "l", 2*im, "SizedComplex{-1}"),
("complex[float] m = -0.23 + -2.0im;", "m", -0.23-2.0*im, "SizedComplex{-1}"),
)


qasm = """
Expand All @@ -356,6 +356,17 @@ Quasar.builtin_gates[] = complex_builtin_gates
visitor = QasmProgramVisitor()
visitor(parsed)
@test visitor.classical_defs[var_name].val == output_val
@test startswith(sprint(show, parsed), """
QasmExpression :program
└─ QasmExpression :classical_declaration
├─ QasmExpression :classical_type
│ └─ $type_name
└─ QasmExpression :classical_assignment
└─ QasmExpression :binary_op
├─ :(=)
├─ QasmExpression :identifier
│ └─ "$var_name"
""")
end
@testset "Qubit identifiers" begin
qasm = """
Expand Down

0 comments on commit faf8911

Please sign in to comment.