Skip to content

Commit 20576e8

Browse files
johnzl-777kaihsin
andcommitted
Add tests from other infrastructure + enforce structure (#411)
Some of the tests here were contributed by @liupengy19 in another repo that uses infrastructure based on SquinToStim. While they were originally "higher up" in the pipeline the root cause of the problems was in SquinToStim (as opposed to that other infrastructure). Therefore I thought it better to move the tests here, closer to what actually caused them to fail in the first place. I've also opted to put the tests in their proper locations, as opposed to just have one file for them from @kaihsin 's original structure (understandably faster but makes it more confusing as to why the test is where it is). --------- Co-authored-by: Kai-Hsin Wu <[email protected]>
1 parent 680a4f9 commit 20576e8

File tree

5 files changed

+55
-35
lines changed

5 files changed

+55
-35
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
I_ERROR[loss](0.10000000) 0
2+
I_ERROR[loss](0.10000000) 1
3+
I_ERROR[loss](0.10000000) 2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MZ(0.00000000) 0
2+
MZ(0.00000000) 1

test/stim/passes/test_squin_meas_to_stim.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from bloqade import squin
66
from bloqade.squin import op, qubit
77
from bloqade.stim.emit import EmitStimMain
8+
from bloqade.squin.qubit import MeasurementResult
89
from bloqade.stim.passes import SquinToStimPass
910

1011

@@ -13,7 +14,16 @@ def codegen(mt: ir.Method):
1314
emit = EmitStimMain()
1415
emit.initialize()
1516
emit.run(mt=mt, args=())
16-
return emit.get_output()
17+
return emit.get_output().strip()
18+
19+
20+
def load_reference_program(filename):
21+
"""Load stim file."""
22+
path = os.path.join(
23+
os.path.dirname(__file__), "stim_reference_programs", "qubit", filename
24+
)
25+
with open(path, "r") as f:
26+
return f.read().strip()
1727

1828

1929
def test_cond_on_measurement():
@@ -38,12 +48,21 @@ def main():
3848

3949
SquinToStimPass(main.dialects)(main)
4050

41-
main.print()
4251

43-
path = os.path.join(
44-
os.path.dirname(__file__), "stim_reference_programs", "simple_if_rewrite.txt"
45-
)
46-
with open(path, "r") as f:
47-
base_stim_prog = f.read()
52+
def test_addition_assignment_on_measures_in_list():
53+
54+
@squin.kernel(fold=False)
55+
def main():
56+
q = qubit.new(2)
57+
results = []
58+
59+
result: MeasurementResult = qubit.measure(q[0])
60+
results += [result]
61+
result: MeasurementResult = qubit.measure(q[1])
62+
results += [result]
63+
64+
SquinToStimPass(main.dialects)(main)
65+
66+
base_stim_prog = load_reference_program("addition_assignment_measure.stim")
4867

49-
assert base_stim_prog.rstrip() == codegen(main)
68+
assert base_stim_prog == codegen(main)

test/stim/passes/test_squin_noise_to_stim.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
from kirin import ir
44

55
from bloqade.squin import op, noise, qubit, kernel
6+
from bloqade.stim.emit import EmitStimMain
67
from bloqade.stim.passes import SquinToStimPass
78

8-
from .test_squin_qubit_to_stim import codegen as _codegen
99

10-
11-
def codegen(mt: ir.Method) -> str:
12-
"""Generate stim code."""
13-
return _codegen(mt).strip()
10+
def codegen(mt: ir.Method):
11+
# method should not have any arguments!
12+
emit = EmitStimMain()
13+
emit.initialize()
14+
emit.run(mt=mt, args=())
15+
return emit.get_output().strip()
1416

1517

1618
def load_reference_program(filename):
@@ -240,3 +242,19 @@ def test():
240242
SquinToStimPass(test.dialects)(test)
241243
expected_stim_program = load_reference_program("broadcast_iid_y_flip_channel.stim")
242244
assert codegen(test) == expected_stim_program
245+
246+
247+
def test_apply_loss():
248+
249+
@kernel
250+
def test():
251+
q = qubit.new(3)
252+
loss = noise.qubit_loss(0.1)
253+
qubit.apply(loss, q[0])
254+
qubit.apply(loss, q[1])
255+
qubit.apply(loss, q[2])
256+
257+
SquinToStimPass(test.dialects)(test)
258+
259+
expected_stim_program = load_reference_program("apply_loss.stim")
260+
assert codegen(test) == expected_stim_program

test/stim/passes/test_squin_to_stim_cases.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)