Skip to content

Fix syntax parameterized gates. #175

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

Merged
merged 1 commit into from
May 3, 2024
Merged
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
8 changes: 6 additions & 2 deletions opensquirrel/exporter/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def visit_gate(self, gate: Gate):
self.output += "<anonymous-gate>\n"
return

formatted_args = (arg.accept(self) for arg in gate.arguments)
self.output += f"{gate.name} {', '.join(formatted_args)}\n"
gate_name = gate.name
if any(not isinstance(arg, Qubit) for arg in gate.arguments):
params = [arg.accept(self) for arg in gate.arguments if not isinstance(arg, Qubit)]
gate_name += f"({', '.join(params)})"
qubit_args = (arg.accept(self) for arg in gate.arguments if isinstance(arg, Qubit))
self.output += f"{gate_name} {', '.join(qubit_args)}\n"

def visit_comment(self, comment: Comment):
self.output += f"\n/* {comment.str} */\n\n"
Expand Down
116 changes: 58 additions & 58 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def test_simple(self):

qubit[3] qreg

Ry qreg[0], 1.23
Ry qreg[1], 2.34
Ry(1.23) qreg[0]
Ry(2.34) qreg[1]
CNOT qreg[0], qreg[1]
Rx qreg[0], -2.3
Ry qreg[1], -3.14
Rx(-2.3) qreg[0]
Ry(-3.14) qreg[1]
"""
)

Expand Down Expand Up @@ -59,26 +59,26 @@ def test_simple(self):

qubit[3] qreg

Rz qreg[0], 3.1415927
Rz(3.1415927) qreg[0]
X90 qreg[0]
Rz qreg[0], 1.9115927
Rz(1.9115927) qreg[0]
X90 qreg[0]
Rz qreg[1], 3.1415927
Rz(3.1415927) qreg[1]
X90 qreg[1]
Rz qreg[1], 2.372389
Rz(2.372389) qreg[1]
X90 qreg[1]
Rz qreg[1], 3.1415927
Rz(3.1415927) qreg[1]
CZ qreg[0], qreg[1]
Rz qreg[0], 1.5707963
Rz(1.5707963) qreg[0]
X90 qreg[0]
Rz qreg[0], 0.84159265
Rz(0.84159265) qreg[0]
X90 qreg[0]
Rz qreg[0], 1.5707963
Rz qreg[1], 3.1415927
Rz(1.5707963) qreg[0]
Rz(3.1415927) qreg[1]
X90 qreg[1]
Rz qreg[1], 1.572389
Rz(1.572389) qreg[1]
X90 qreg[1]
Rz qreg[1], 3.1415927
Rz(3.1415927) qreg[1]
""",
)

Expand All @@ -89,13 +89,13 @@ def test_measurement(self):

qubit[3] qreg

Ry qreg[2], 2.34
Rz qreg[0], 1.5707963
Ry qreg[0], -0.2
Ry(2.34) qreg[2]
Rz(1.5707963) qreg[0]
Ry(-0.2) qreg[0]
CNOT qreg[1], qreg[0]
Rz qreg[0], 1.5789
Rz(1.5789) qreg[0]
CNOT qreg[1], qreg[0]
Rz qreg[1], 2.5707963
Rz(2.5707963) qreg[1]
measure qreg[0,2]
""",
)
Expand All @@ -107,27 +107,27 @@ def test_measurement(self):

qubit[3] qreg

Rz qreg[0], 1.5707963
Rz(1.5707963) qreg[0]
X90 qreg[0]
Rz qreg[0], 2.9415927
Rz(2.9415927) qreg[0]
X90 qreg[0]
Rz qreg[0], 3.1415927
Rz(3.1415927) qreg[0]
CNOT qreg[1], qreg[0]
Rz qreg[0], -2.3521427
Rz(-2.3521427) qreg[0]
X90 qreg[0]
Rz qreg[0], 3.1415927
Rz(3.1415927) qreg[0]
X90 qreg[0]
Rz qreg[0], 0.78945
Rz(0.78945) qreg[0]
CNOT qreg[1], qreg[0]
Rz qreg[2], 3.1415927
Rz(3.1415927) qreg[2]
X90 qreg[2]
Rz qreg[2], 0.80159265
Rz(0.80159265) qreg[2]
X90 qreg[2]
Rz qreg[1], -1.8561945
Rz(-1.8561945) qreg[1]
X90 qreg[1]
Rz qreg[1], 3.1415927
Rz(3.1415927) qreg[1]
X90 qreg[1]
Rz qreg[1], 1.2853981
Rz(1.2853981) qreg[1]
measure qreg[0]
measure qreg[2]
""",
Expand Down Expand Up @@ -157,13 +157,13 @@ def test_consecutive_measurements(self):
qubit[3] qreg

X90 qreg[0]
Rz qreg[0], 1.5707963
Rz(1.5707963) qreg[0]
X90 qreg[0]
X90 qreg[1]
Rz qreg[1], 1.5707963
Rz(1.5707963) qreg[1]
X90 qreg[1]
X90 qreg[2]
Rz qreg[2], 1.5707963
Rz(1.5707963) qreg[2]
X90 qreg[2]
measure qreg[0]
measure qreg[1]
Expand All @@ -178,8 +178,8 @@ def test_measure_order(self):

qubit[2] qreg

Rz qreg[1], -2.3561945
Rz qreg[1], 1.5707963
Rz(-2.3561945) qreg[1]
Rz(1.5707963) qreg[1]
measure qreg[1,0]
"""
)
Expand All @@ -190,11 +190,11 @@ def test_measure_order(self):

qubit[2] qreg

Rz qreg[1], 2.7488936
Rz(2.7488936) qreg[1]
X90 qreg[1]
Rz qreg[1], 3.1415927
Rz(3.1415927) qreg[1]
X90 qreg[1]
Rz qreg[1], -0.3926991
Rz(-0.3926991) qreg[1]
measure qreg[1]
measure qreg[0]
"""
Expand All @@ -218,15 +218,15 @@ def test_qi(self):
H q[2]
H q[1]
H q[0]
Rz q[0], 1.5707963
Ry q[0], -0.2
Rz(1.5707963) q[0]
Ry(-0.2) q[0]
CNOT q[1], q[0]
Rz q[0], 1.5789
Rz(1.5789) q[0]
CNOT q[1], q[0]
CNOT q[1], q[2]
Rz q[1], 2.5707963
CR q[2], q[3], 2.123
Ry q[1], -1.5707963
Rz(2.5707963) q[1]
CR(2.123) q[2], q[3]
Ry(-1.5707963) q[1]
"""
)

Expand All @@ -240,30 +240,30 @@ def test_qi(self):
qubit[4] q

X90 q[1]
Rz q[1], 1.5707963
Rz(1.5707963) q[1]
X90 q[1]
Rz q[0], -0.2
Rz(-0.2) q[0]
X90 q[0]
Rz q[0], 1.5707963
Rz(1.5707963) q[0]
X90 q[0]
Rz q[0], 1.5707963
Rz(1.5707963) q[0]
CNOT q[1], q[0]
Rz q[0], -2.3521427
Rz(-2.3521427) q[0]
X90 q[0]
Rz q[0], 3.1415927
Rz(3.1415927) q[0]
X90 q[0]
Rz q[0], 0.78945
Rz(0.78945) q[0]
CNOT q[1], q[0]
X90 q[2]
Rz q[2], 1.5707963
Rz(1.5707963) q[2]
X90 q[2]
CNOT q[1], q[2]
CR q[2], q[3], 2.123
Rz q[1], 2.5707963
CR(2.123) q[2], q[3]
Rz(2.5707963) q[1]
X90 q[1]
Rz q[1], 1.5707964
Rz(1.5707964) q[1]
X90 q[1]
Rz q[1], 3.1415927
Rz(3.1415927) q[1]
"""

self.assertEqual(output, expected)
Expand Down Expand Up @@ -291,7 +291,7 @@ def test_export_quantify_scheduler(self):
H qreg[1]
CZ qreg[0], qreg[1]
CNOT qreg[0], qreg[1]
CRk qreg[0], qreg[1], 4
CRk(4) qreg[0], qreg[1]
H qreg[0]
"""
)
Expand Down
10 changes: 5 additions & 5 deletions test/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_write(self):
qubit[3] myqubitsregister

H myqubitsregister[0]
CR myqubitsregister[0], myqubitsregister[1], 1.234
CR(1.234) myqubitsregister[0], myqubitsregister[1]
""",
)

Expand All @@ -49,9 +49,9 @@ def test_anonymous_gate(self):

qubit[1] q

CR q[0], q[1], 1.234
CR(1.234) q[0], q[1]
<anonymous-gate>
CR q[0], q[1], 1.234
CR(1.234) q[0], q[1]
""",
)

Expand All @@ -72,7 +72,7 @@ def test_comment(self):

/* My comment */

CR q[0], q[1], 1.234
CR(1.234) q[0], q[1]
""",
)

Expand All @@ -87,7 +87,7 @@ def test_cap_significant_digits(self):

qubit[3] q

CR q[0], q[1], 1.6546515
CR(1.6546515) q[0], q[1]
""",
)

Expand Down