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

Feature Parse qasm for BlockIR(qasm::String) #8

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
add qasm test
  • Loading branch information
contra-bit committed Oct 18, 2023
commit 07e651b6d4b52feff6ce5f849768858ff8c64c0f
154 changes: 137 additions & 17 deletions test/qasm.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using YaoHIR
using YaoHIR.IntrinsicOperation
using OpenQASM

@testset "convert simple qasm file into BlockIR" begin
@testset "convert qasm" begin

qasm = """
bir = BlockIR("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q0[3];
@@ -18,30 +19,149 @@ using YaoHIR.IntrinsicOperation
CX q0[1], q0[2];
h q0[1];
measure q0[1] -> c0[1];
"""

circuit = Chain(
Gate(H, Locations(1)),
Gate(H, Locations(2)),
Gate(X, Locations(3)),
Gate(H, Locations(3)),
Ctrl(Gate(X, Locations(1)), CtrlLocations(3)),
Gate(H, Locations(1)),
Ctrl(Gate(X, Locations(2)), CtrlLocations(3)),
Gate(H, Locations(2))
)
""")

@testset "correct parsing" begin
bir !== nothing
end

bir = BlockIR(qasm)
chain = Chain(
Gate(H, Locations(1)),
Gate(H, Locations(2)),
Gate(X, Locations(3)),
Gate(H, Locations(3)),
Ctrl(Gate(X, Locations(3)), CtrlLocations(1)),
Gate(H, Locations(1)),
Ctrl(Gate(X, Locations(3)), CtrlLocations(2)),
Gate(H, Locations(2))
)

@testset "conversion" begin
# @test chain == bir.circuit
@test chain == bir.circuit
end


@testset "parse DJ-NAND" begin
bv = BlockIR("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[2];
h q[0];
h q[1];
x q[2];
h q[2];
x q[2];
h q[2];
cx q[1],q[2];
tdg q[2];
cx q[0],q[2];
t q[2];
cx q[1],q[2];
t q[1];
tdg q[2];
cx q[0],q[2];
cx q[0],q[1];
t q[0];
tdg q[1];
cx q[0],q[1];
h q[0];
h q[1];
t q[2];
h q[2];
measure q[0] -> c[0];
measure q[1] -> c[1];
""")


bv_re = BlockIR("""
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c1[1];
creg c2[1];
h q[1];
t q[1];
x q[2];
h q[2];
x q[2];
h q[2];
t q[2];
CX q[1], q[2];
tdg q[2];
CX q[1], q[2];
h q[1];
h q[2];
h q[2];
t q[2];
h q[0];
t q[0];
CX q[0], q[2];
tdg q[2];
CX q[0], q[2];
CX q[1], q[0];
tdg q[0];
tdg q[2];
CX q[0], q[2];
t q[2];
CX q[0], q[2];
CX q[1], q[0];
h q[0];
h q[2];
measure q[0] -> c2[0];
measure q[1] -> c1[0];""")

@testset "correct parsing" begin
bir !== nothing
@test bv !== nothing
end

@testset "convert into BlockIR" begin
@test bv_re !== nothing

end

end

@testset "dj 3" begin
dj3 = BlockIR("""
OPENQASM 2.0;
include "qelib1.inc";

qreg q[3];
creg c[2];
h q[0];
h q[1];
x q[2];
x q[0];
x q[1];
h q[2];
cx q[0],q[2];
x q[0];
cx q[1],q[2];
h q[0];
x q[1];
h q[1];
""")

chain = Chain(Gate(H, Locations(1)),
Gate(H, Locations(2)),
Gate(X, Locations(3)),
Gate(X, Locations(1)),
Gate(X, Locations(2)),
Gate(H, Locations(3)),
Ctrl(Gate(X, Locations(3)),
CtrlLocations(1)),
Gate(X, Locations(1)),
Ctrl(Gate(X, Locations(3)),
CtrlLocations(2)),
Gate(H, Locations(1)),
Gate(X, Locations(2)),
Gate(H, Locations(2)))

@test dj3.circuit == chain
# FIXME should an empty IR print '1 ─ return nothing' ?
# print(dj3)

end

end