Skip to content

Commit

Permalink
fix: test for const arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws committed Jun 29, 2024
1 parent 21d7904 commit 3fda391
Show file tree
Hide file tree
Showing 2 changed files with 24 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 @@ -615,7 +615,7 @@ function parse_expression(tokens::Vector{Tuple{Int64, Int32, Token}}, stack, sta
token_name = parse_classical_type(pushfirst!(tokens, start_token), stack, start, qasm)
elseif start_token[end] (string_token, integer, float, hex, oct, bin, irrational, dot, boolean)
token_name = parse_literal(pushfirst!(tokens, start_token), stack, start, qasm)
elseif start_token[end] (mutable, readonly)
elseif start_token[end] (mutable, readonly, const_token)
token_name = parse_identifier(start_token, qasm)
elseif start_token[end] == dim_token
raw_dim = qasm[start_token[1]:start_token[1]+start_token[2]-1]
Expand Down Expand Up @@ -659,7 +659,7 @@ function parse_expression(tokens::Vector{Tuple{Int64, Int32, Token}}, stack, sta
stop = parse_expression(tokens, stack, start, qasm)::QasmExpression
end
expr = QasmExpression(:range, QasmExpression[start, step, stop])
elseif next_token[end] == classical_type && start_token[end] (mutable, readonly)
elseif next_token[end] == classical_type && start_token[end] (mutable, readonly, const_token)
type = parse_classical_type(tokens, stack, start, qasm)
is_mutable = (start_token[end] == mutable)
header = is_mutable ? :classical_declaration : :const_declaration
Expand Down
22 changes: 22 additions & 0 deletions test/test_openqasm3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,28 @@ get_tol(shots::Int) = return (
@test visitor.classical_defs["array_1"].val == [1, 2, 3, 4, 5]
@test visitor.classical_defs["array_2"].val == collect(1:10)
end
@testset "Const array argument" begin
qasm_string = """
qubit q;
def sum(const array[int[8], #dim = 1] arr) -> int {
int size = sizeof(arr);
int x = 0;
for int i in [0:size - 1] {
x += arr[i];
}
return x;
}
array[int, 10] arr = {9, 3, 6, 2, 2, 4, 3, 1, 12, 7};
int s = sum(arr);
rx(s*π/4) q;
"""
parsed = parse_qasm(qasm_string)
visitor = QasmProgramVisitor()
visitor(parsed)
@test visitor.classical_defs["s"].val == sum([ 9, 3, 6, 2, 2, 4, 3, 1, 12, 7 ])
end
@testset "Array ref subroutine with mutation" begin
qasm = """
def mutate_array(mutable array[int[8], #dim = 1] arr) {
Expand Down

0 comments on commit 3fda391

Please sign in to comment.