Skip to content

Commit e89573a

Browse files
committed
test tweaks
1 parent d01a4c6 commit e89573a

15 files changed

Lines changed: 25 additions & 22 deletions

loqs-v1.0/loqs/tools/dasktools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def run_program_list(
6464
program.shot_histories = []
6565

6666
# Write program to file so that other processes can load
67-
with NamedTemporaryFile("w+", suffix=".json", delete=False) as tempf:
67+
with NamedTemporaryFile("w+", dir='.', suffix=".json", delete=False) as tempf:
6868
p_dict = program.to_serialization(ignore_no_serialize_flags=True)
6969
json.dump(p_dict, tempf)
7070
program_filenames.append(tempf.name)

loqs-v1.0/tests/backends/test_noise_integrated.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ def test_1q_depolarizing(self):
104104
# Because we set the seed, we should exactly match output from test creation
105105
# Also, because our depol rate is 10%, we expected a flip 5% of the time (the X and Y errors)
106106
# For 1000 shots, this is ~50 shots should flip
107-
expected_outs = {0: 952, 1: 48}
108-
assert Counter(outs) == expected_outs
107+
assert abs(Counter(outs)[0] - 950) < 10
108+
assert abs(Counter(outs)[1] - 50) < 10
109109

110110
# Also test QuantumSim in the X basis
111111
stack_Xbasis = [
@@ -121,7 +121,8 @@ def test_1q_depolarizing(self):
121121
program_qsim_Xbasis.run(num_shots=1000, reset_shot_histories=True)
122122
outs = [mo["Q0"][0] for mo in program_qsim_Xbasis.collect_shot_data("measurement_outcomes", -1)]
123123
# Now Y and Z should flip. Because rate X == rate Z and we have RNG, results should be unchanged
124-
assert Counter(outs) == expected_outs
124+
assert abs(Counter(outs)[0] - 950) < 10
125+
assert abs(Counter(outs)[1] - 50) < 10
125126

126127
## STIM
127128
gate_dict, inst_dict = self._create_model_dicts(qubits, GateRep.STIM_CIRCUIT_STR)
@@ -144,15 +145,17 @@ def test_1q_depolarizing(self):
144145
outs = [mo["Q0"][0] for mo in program_stim.collect_shot_data("measurement_outcomes", -1)]
145146
# STIM handles its own RNG, so this could in principle differ from QuantumSim results
146147
# In practice, for this simple circuit, I've found that this coincidentally matches for Z basis
147-
assert Counter(outs) == expected_outs
148+
assert abs(Counter(outs)[0] - 950) < 10
149+
assert abs(Counter(outs)[1] - 50) < 10
148150

149151
# Also test STIM in the X basis
150152
program_stim_Xbasis = QuantumProgram.from_quantum_program(program_stim, stack_Xbasis)
151153

152154
program_stim_Xbasis.run(num_shots=1000)
153155
outs = [mo["Q0"][0] for mo in program_stim_Xbasis.collect_shot_data("measurement_outcomes", -1)]
154156
# But the RNG is handled differently for X basis, so we don't get a match (although it is still ~50)
155-
assert Counter(outs) == {0: 944, 1: 56}
157+
assert abs(Counter(outs)[0] - 950) < 10
158+
assert abs(Counter(outs)[1] - 50) < 10
156159

157160
# Amp damp tests, given as damping rate, dephasing rate, seed, num shots, and then expected 1 counts in order of:
158161
# QuantumSim X,I,Mz; QuantumSim H,I,Mz; QuantumSim H,I,Mx; STIM X,I,Mz; STIM H,I,Mz; STIM H,I,Mx

loqs-v1.0/tests/backends/test_npsvstate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def test_serialization(self):
256256
test, _ = state10.apply_reps([RepTuple(U_H, ["Q1"], GateRep.UNITARY)])
257257
test.apply_reps_inplace([RepTuple(U_CZ, ["Q0", "Q1"], GateRep.UNITARY)])
258258

259-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
259+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
260260
test.write(tempf.name)
261261

262262
test2: SVState = SVState.read(tempf.name)

loqs-v1.0/tests/backends/test_qsimstate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def test_serialization(self):
257257
# Don't force propagation here
258258
# So serialization should both serialize DM and operations to be applied
259259

260-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
260+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
261261
test.write(tempf.name)
262262

263263
test2: QSimState = QSimState.read(tempf.name)

loqs-v1.0/tests/backends/test_stimstate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def test_serialization(self):
219219
test = STIMState([1, 0], ["Q0", "Q1"])
220220
test.state.cx(0, 1)
221221

222-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
222+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
223223
test.write(tempf.name)
224224

225225
test2 = STIMState.read(tempf.name)

loqs-v1.0/tests/core/instructions/test_instruction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def map_qubits_fn(qubit_mapping, qubits, **kwargs):
6767

6868
ins = Instruction(apply_fn, data, map_qubits_fn, name="test")
6969

70-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
70+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
7171
ins.write(tempf.name)
7272

7373
ins2 = Frame.read(tempf.name)
@@ -84,7 +84,7 @@ def map_qubits_fn(qubit_mapping, qubits, **kwargs):
8484
# We should be able to do it a second time also
8585
# This is because we cache the serialization the first time,
8686
# so even though the second time we don't have access to the source code, it works
87-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
87+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
8888
ins2.write(tempf.name)
8989

9090
ins3 = Frame.read(tempf.name)

loqs-v1.0/tests/core/instructions/test_instructionlabel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def test_serialization(self):
7171
# Test string version
7272
ilbl = InstructionLabel("Label", "L0", self.args, self.kwargs)
7373

74-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
74+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
7575
ilbl.write(tempf.name)
7676

7777
ilbl2 = InstructionLabel.read(tempf.name)
@@ -80,7 +80,7 @@ def test_serialization(self):
8080
# And instruction version
8181
ilbl3 = InstructionLabel(self.ins, "L0", self.args, self.kwargs)
8282

83-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
83+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
8484
ilbl3.write(tempf.name)
8585

8686
ilbl4 = InstructionLabel.read(tempf.name)

loqs-v1.0/tests/core/instructions/test_instructionstack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_list_operations(self):
5252
def test_serialization(self):
5353
s = InstructionStack([self.ilbl1, self.ilbl2]) # type: ignore
5454

55-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
55+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
5656
s.write(tempf.name)
5757

5858
s2 = InstructionStack.read(tempf.name)

loqs-v1.0/tests/core/recordables/test_measurementoutcomes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_serialization(self):
5353
expected = {"Q0": [0, 1], "Q1": [1]}
5454
m = MeasurementOutcomes(outcomes)
5555

56-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
56+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
5757
m.write(tempf.name)
5858

5959
m2 = MeasurementOutcomes.read(tempf.name)

loqs-v1.0/tests/core/recordables/test_patchdict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_serialization(self):
2929
patch2 = code.create_patch(["D1", "A1"])
3030
patches = PatchDict({"L0": patch1, "L1": patch2})
3131

32-
with NamedTemporaryFile("w+", suffix='.json') as tempf:
32+
with NamedTemporaryFile("w+", dir='.', suffix='.json') as tempf:
3333
patches.write(tempf.name)
3434

3535
patches2 = PatchDict.read(tempf.name)

0 commit comments

Comments
 (0)