Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CZ Gate and fix cx bug #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ RBNF.@parser QASMLang begin
reset::Reset := [:reset, qarg = bit, ';']
measure::Measure := [:measure, qarg = bit, :(->), carg = bit, ';']

uop = (inst | ugate | csemantic_gate)
uop = (inst | ugate | csemantic_gate | czsemantic_gate)
inst::Instruction := [name = id, ['(', [cargs = explist].?, ')'].?, qargs = bitlist, ';']
ugate::UGate := [:U, '(', z1 = exp, ',', y = exp, ',', z2 = exp, ')', qarg = bit, ';']
csemantic_gate::CXGate := [:CX, ctrl = bit, ',', qarg = bit, ';']
czsemantic_gate::CZGate := [:CZ | :cz, ctrl = bit, ',', qarg = bit, ';']
csemantic_gate::CXGate := [:CX | :cx, ctrl = bit, ',', qarg = bit, ';']

idlist = @direct_recur begin
init = [id]
Expand Down
18 changes: 17 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using MLStyle
using RBNF: Token

export MainProgram, IfStmt, Opaque, Barrier, RegDecl, Include, GateDecl, Gate, Reset, Measure,
Instruction, UGate, CXGate, Bit, Call, Neg, Add, Sub, Mul, Div, ASTNode
Instruction, UGate, CXGate, CZGate, Bit, Call, Neg, Add, Sub, Mul, Div, ASTNode

abstract type ASTNode end

Expand Down Expand Up @@ -98,6 +98,12 @@ struct CXGate <: ASTNode
qarg
end


struct CZGate <: ASTNode
ctrl
qarg
end

struct Bit <: ASTNode
name
address
Expand Down Expand Up @@ -316,6 +322,15 @@ function print_qasm(io::IO, stmt::CXGate)
print(io, ";")
end


function print_qasm(io::IO, stmt::CZGate)
print_kw(io, "CZ ")
print_qasm(io, stmt.ctrl)
print(io, ", ")
print_qasm(io, stmt.qarg)
print(io, ";")
end

function print_qasm(io::IO, stmt::Bit)
print_qasm(io, stmt.name)
if stmt.address !== nothing
Expand Down Expand Up @@ -365,6 +380,7 @@ end
@as_record Instruction
@as_record UGate
@as_record CXGate
@as_record CZGate
@as_record Bit
@as_record Call
@as_record Neg
Expand Down
15 changes: 14 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ end
creg c1[1];
creg c2[1];
U(-1.0, pi/2+3, 3.0) q[2];
CX q[1], q[2];
cx q[1], q[2];
custom(0.3) q[3];
barrier q;
h q[0];
measure q[0] -> c0[0];
if(c0==1) z q[2];
u3(0.1 + 0.2, 0.2, 0.3) q[0];
reset q[0];
CZ q[1], q[2];
"""

ast = OpenQASM.parse(qasm)
Expand Down Expand Up @@ -275,6 +276,18 @@ end
@test string(reset) == "reset q[0];"
end


@testset "CZ" begin
@test ast.prog[16] isa CZGate
cx = ast.prog[16]
@test cx.ctrl isa Bit
@test cx.qarg isa Bit
@test cx.ctrl.name.str == "q"
@test cx.qarg.name.str == "q"
@test cx.ctrl.address.str == "1"
@test cx.qarg.address.str == "2"
end

end

@testset "cmp_ast" begin
Expand Down
Loading