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

WIP: converting OpenQASM to YaoBlocks #14

Open
wants to merge 2 commits into
base: qasm_to_blocks
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/docs/build/
Manifest.toml
2 changes: 1 addition & 1 deletion src/YaoBlocksQASM.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module YaoBlocksQASM

export convert_to_qasm
export convert_to_qasm, toblocks, @qasm_str

using YaoBlocks

Expand Down
101 changes: 73 additions & 28 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,70 @@ using OpenQASM, YaoBlocksQASM, Yao
using Test

@testset "YaoBlocksQASM.jl" begin
qasm = """
OPENQASM 2.0;
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c1[3];
h q[0];
CX q[1],q[2];
cy q[1],q[0];
cz q[0],q[2];
x q[0];
swap q[1],q[2];
id q[0];
t q[1];
rz(0.7) q[2];
z q[0];
p(0.7) q[1];
ry(0.7) q[2];
y q[0];
rx(0.7) q[1];
measure q[0] -> c1[0];
measure q[1] -> c1[1];
measure q[2] -> c1[2];
"""

ast = OpenQASM.parse(qasm)
qc = chain(
3,
put(1 => H),
control(2, 3 => X),
control(2, 1 => Y),
control(1, 3 => Z),
put(1 => X),
swap(2, 3),
put(1 => I2),
put(2 => T),
put(3 => Rz(0.7)),
put(1 => Z),
put(2 => shift(0.7)),
put(3 => Ry(0.7)),
put(1 => Y),
put(2 => Rx(0.7)),
Yao.Measure(3),
)

qc_blocks = toblocks(Module(), LineNumberNode(1), OpenQASM.parse( qasm))

@testset "convert YaoBlocks to QASM" begin
ast1 = convert_to_qasm(qc, 1)

@test ast.version == ast1.version

for i = 1:length(ast.prog)
@test "$(ast.prog[i])" == "$(ast1.prog[i])"
end
end

@testset "convert QASM to YaoBlocks" begin
@info typeof(qc_blocks)
println("converted circuit $qc_blocks")

end

@testset "use marco" begin
circuit = @qasm_str """OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c1[3];
Expand All @@ -24,32 +86,15 @@ using Test
measure q[0] -> c1[0];
measure q[1] -> c1[1];
measure q[2] -> c1[2];
"""

ast = OpenQASM.parse(qasm)
qc = chain(
3,
put(1 => H),
control(2, 3 => X),
control(2, 1 => Y),
control(1, 3 => Z),
put(1 => X),
swap(2, 3),
put(1 => I2),
put(2 => T),
put(3 => Rz(0.7)),
put(1 => Z),
put(2 => shift(0.7)),
put(3 => Ry(0.7)),
put(1 => Y),
put(2 => Rx(0.7)),
Yao.Measure(3),
)
ast1 = convert_to_qasm(qc, 1)
"""
println(circuit)
end

# @testset "check equivalence of converting to and from OpenQASM from YaoBlocks" begin
# reconverted_circuit = convert_to_qasm(qc,3)
# @test circuit == reconverted_circuit
# # TODO implemet full test
# end

@test ast.version == ast1.version

for i = 1:length(ast.prog)
@test "$(ast.prog[i])" == "$(ast1.prog[i])"
end
end