Skip to content

Commit

Permalink
fix: Support for mu-s, dt
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyatt-aws committed Sep 24, 2024
1 parent a86fb9c commit a730461
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/Quasar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ const qasm_tokens = [
:spaces => re"[\t ]+",
:classical_type => re"bool|uint|int|float|angle|complex|array|bit|stretch|duration",
:durationof_token => re"durationof", # this MUST be lower than classical_type to preempt duration
:duration_literal => (float | integer) * re"dt|ns|µs|us|ms|s",
:duration_literal => (float | integer) * re"dt|ns|us|ms|s|\xce\xbc\x73", # transcode'd μs
:forbidden_keyword => re"cal|defcal|extern",
]

const dt_type = Ref{DataType}()

function __init__()
dt_type[] = Nanosecond
end

@eval @enum Token error $(first.(qasm_tokens)...)
make_tokenizer((error,
[Token(i) => j for (i,j) in enumerate(last.(qasm_tokens))]
Expand Down Expand Up @@ -444,6 +450,8 @@ function parse_duration_literal(token, qasm)
Microsecond(tryparse(Int, chop(str, tail=2)))
elseif endswith(str, "s")
Second(tryparse(Int, chop(str, tail=1)))
elseif endswith(str, "dt")
dt_type[](tryparse(Int, chop(str, tail=2)))
end
QasmExpression(:duration_literal, duration)
end
Expand Down
8 changes: 5 additions & 3 deletions test/test_openqasm3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,9 @@ get_tol(shots::Int) = return (
@test_throws Quasar.QasmParseError parse_qasm(qasm)
end
@testset "Delay $duration" for (duration, ix) in (("200us", Microsecond(200)),
("100μs", Microsecond(100)),
("50ns", Nanosecond(50)),
("20dt", Nanosecond(20)),
("10ms", Millisecond(10)),
("1s", Second(1)),
)
Expand All @@ -570,7 +572,7 @@ get_tol(shots::Int) = return (
delay[$duration] q[0], q[1];
"""
@test_warn "delay expression encountered -- currently `delay` is a no-op" parse_qasm(qasm)
circuit = BraketSimulator.Circuit(qasm)
circuit = Quasar.to_circuit(qasm)
@test circuit.instructions == [BraketSimulator.Instruction(BraketSimulator.X(), 0),
BraketSimulator.Instruction(BraketSimulator.Delay(ix), 0),
BraketSimulator.Instruction(BraketSimulator.Delay(ix), 1),
Expand All @@ -594,7 +596,7 @@ get_tol(shots::Int) = return (
#pragma braket result state_vector
"""
@test_warn "barrier expression encountered -- currently `barrier` is a no-op" parse_qasm(qasm)
circuit = BraketSimulator.Circuit(qasm)
circuit = Quasar.to_circuit(qasm)
@test circuit.instructions == [BraketSimulator.Instruction(BraketSimulator.X(), 0), BraketSimulator.Instruction(BraketSimulator.Barrier(), 0)]
simulation = BraketSimulator.StateVectorSimulator(4, 0)
ref_circ = BraketSimulator.Circuit()
Expand Down Expand Up @@ -626,7 +628,7 @@ get_tol(shots::Int) = return (
reset q[0];
"""
@test_warn "reset expression encountered -- currently `reset` is a no-op" parse_qasm(qasm)
circuit = BraketSimulator.Circuit(qasm)
circuit = Quasar.to_circuit(qasm)
@test circuit.instructions == [BraketSimulator.Instruction(BraketSimulator.X(), 0), BraketSimulator.Instruction(BraketSimulator.Reset(), 0)]
simulation = BraketSimulator.StateVectorSimulator(4, 0)
ref_circ = BraketSimulator.Circuit()
Expand Down

0 comments on commit a730461

Please sign in to comment.