Skip to content

Commit

Permalink
Handle block comments properly (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws authored Nov 18, 2024
1 parent 377d161 commit 358a567
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Quasar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const qasm_tokens = [
:hex_integer => re"(0x|0X) ([0-9a-fA-F] _?)* [0-9a-fA-F]",
:hardware_qubit => re"$ [0-9]+",
:line_comment => re"//",
:block_comment => re"/\* .*? \*/",
:block_comment => re"/\*.*?\*/",
:char => '\'' * (re"[ -&(-~]" | ('\\' * re"[ -~]")) * '\'',
:string_token => '"' * rep(re"[ !#-~]" | re"\\\\\"") * '"' | '\'' * rep(re"[ -&(-~]" | ('\\' * re"[ -~]")) * '\'',
:newline => re"\r?\n",
Expand Down Expand Up @@ -1054,7 +1054,7 @@ end
function parse_qasm(qasm::String, root=QasmExpression(:program))::QasmExpression
raw_tokens = tokenize(Token, qasm)
clean_tokens = filter(triplet->triplet[3] (spaces, block_comment), collect(raw_tokens))
# add a final newline in case one is missing
# add a final newline in case one is missing
clean_tokens[end][end] == newline || push!(clean_tokens, (-1, Int32(-1), newline))
stack = parse_qasm(clean_tokens, qasm, root)
stack_exprs = convert(Vector{QasmExpression}, collect(Iterators.reverse(stack)))::Vector{QasmExpression}
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,24 @@ Quasar.builtin_gates[] = complex_builtin_gates
@test ix == c_ix
end
end
@testset "Block comment" begin
qasm = """
qubit[2] qs;
int[8] two = 2;
gphase(π);
/*** Apply inverse of gphase here ***/
inv @ gphase(π / 2);
negctrl @ ctrl @ gphase(2 * π) qs[0], qs[1];
"""
parsed = parse_qasm(qasm)
visitor = QasmProgramVisitor()
visitor(parsed)
@test visitor.instructions == [
(type="gphase", arguments=InstructionArgument[π], targets=[0, 1], controls=Pair{Int,Int}[], exponent=1.0),
(type="gphase", arguments=InstructionArgument[π/2], targets=[0, 1], controls=Pair{Int,Int}[], exponent=-1.0),
(type="gphase", arguments=InstructionArgument[2*π], targets=[0, 1], controls=[0=>0, 1=>1], exponent=1.0),
]
end
@testset "Randomized Benchmarking" begin
qasm = """
qubit[2] q;
Expand Down

0 comments on commit 358a567

Please sign in to comment.