2Unit tests for dist_m4ri.py Python wrapper.
9import scipy.sparse
as sp
12sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
"..")))
16EXAMPLES_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__),
"..",
"examples"))
21 assert os.path.isfile(bin_path)
22 assert os.access(bin_path, os.X_OK)
26 h_file = os.path.join(EXAMPLES_DIR,
"c204H.mmx")
34 [1, 0, 0, 1, 1, 0, 1],
35 [0, 1, 0, 1, 0, 1, 1],
45 assert all(len(cw) == 3
for cw
in cws)
49 hx_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
50 hz_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
53 Hx=hx_file, Hz=hx_file, Lz=hz_file, Lx=hz_file,
57 assert d_x == [5, 5, 0]
58 assert d_z == [5, 5, 0]
64 [1, 1, 0, 1, 1, 0, 0, 0, 0],
65 [0, 1, 1, 0, 1, 1, 0, 0, 0],
66 [0, 0, 0, 1, 1, 0, 1, 1, 0],
67 [0, 0, 0, 0, 1, 1, 0, 1, 1]
70 [1, 0, 0, 1, 0, 0, 1, 0, 0],
71 [0, 1, 0, 0, 1, 0, 0, 1, 0],
72 [0, 0, 1, 0, 0, 1, 0, 0, 1]
81 dem_file = os.path.join(EXAMPLES_DIR,
"surf_d3.dem")
84 assert d_info == [3, 3, 0]
89 assert d_info == [3, 3, 0]
91 assert all(len(cw) == 3
for cw
in cws)
96 assert d_info == [3, 3, 0]
97 assert len(cws_cc) == 128
98 assert all(len(cw) == 3
for cw
in cws_cc)
102 if not dist_m4ri._HAS_STIM:
103 pytest.skip(
"stim is not installed")
105 circuit = stim.Circuit.generated(
106 "surface_code:rotated_memory_z",
109 after_clifford_depolarization=0.001
111 dem = circuit.detector_error_model(decompose_errors=
True)
114 assert d_info == [3, 3, 0]
122 [1, 0, 0, 1, 1, 0, 1],
123 [0, 1, 0, 1, 0, 1, 1],
124 [0, 0, 1, 0, 1, 1, 1]
128 assert len(dist_m4ri._distance_cache) == 1
133 assert len(dist_m4ri._distance_cache) == 1
136 assert len(dist_m4ri._distance_cache) == 0
140 if not dist_m4ri._HAS_CODEDISTANCE:
141 pytest.skip(
"codedistance is not installed")
144 [1, 0, 0, 1, 1, 0, 1],
145 [0, 1, 0, 1, 0, 1, 1],
146 [0, 0, 1, 0, 1, 1, 1]
151 h_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
152 l_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
154 assert (dmin, dmax, rw_steps) == (5, 5, 0)
158 assert (dmin, dmax, rw_steps) == (4, 0, 0)
161 dem_file = os.path.join(EXAMPLES_DIR,
"surf_d3.dem")
165 assert rw_steps >= 100
170 h_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
171 l_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
172 dmin, dmax, rw_steps =
dist_m4ri.run_dist_m4ri(method=3, finH=h_file, finL=l_file, dmin=4, dmax=5, timeout=5, threads=4)
173 assert (dmin, dmax) == (5, 5)
176 c_file = os.path.join(EXAMPLES_DIR,
"c204H.mmx")
181 dem_file = os.path.join(EXAMPLES_DIR,
"surf_d3.dem")
190 c_file = os.path.join(EXAMPLES_DIR,
"c1920H.mmx")
195 assert entry1
is not None
196 assert entry1[
"rw_steps"] == 50
197 assert entry1[
"dmax"] > 0
198 assert d1 == entry1[
"dmax"]
203 assert entry2
is not None
204 assert entry2[
"rw_steps"] == 150
205 assert entry2[
"dmax"] <= entry1[
"dmax"]
206 assert d2 == entry2[
"dmax"]
231 json_file = str(tmp_path /
"test_cache.json")
234 c_file = os.path.join(EXAMPLES_DIR,
"c1920H.mmx")
238 assert os.path.isfile(json_file)
240 with open(json_file,
"r")
as f:
242 assert len(data1) == 1
243 key = list(data1.keys())[0]
244 assert data1[key][
"rw_steps"] == 50
245 assert data1[key][
"dmax"] == d1
250 assert entry
is not None
251 assert entry[
"rw_steps"] == 50
255 with open(json_file,
"r")
as f:
257 assert data2[key][
"rw_steps"] == 150
258 assert data2[key][
"dmax"] <= d1
261 hx_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
262 hz_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
264 Hx=hx_file, Hz=hx_file, Lz=hz_file, Lx=hz_file,
265 d_exp=5, threads=4, cache_file=json_file
268 with open(json_file,
"r")
as f:
269 data_css = json.load(f)
270 assert len(data_css) >= 2
273 dem_file = os.path.join(EXAMPLES_DIR,
"surf_d3.dem")
276 with open(json_file,
"r")
as f:
277 data_dem = json.load(f)
278 assert len(data_dem) >= 3
281 assert not os.path.exists(json_file)
285 h_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
286 l_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
288 H=h_file, L=l_file, method=3, d_exp=5, threads=4, return_info=
True
291 assert d_info == [5, 5, 0]
297 assert args1[
"classical"] == 0
298 assert args1[
"finH"] ==
"h.mtx"
299 assert args1[
"finG"] ==
"g.mtx"
300 assert args1[
"smax"] == 0
301 assert args1[
"finC"] ==
"init.nz"
302 assert args1[
"start"] == 2
306 assert args2[
"classical"] == 1
307 assert args2[
"finH"] ==
"h.mtx"
308 assert args2[
"finG"]
is None
312 assert args3[
"classical"] == 0
319 h_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
320 l_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
328 class_keys = [k
for k
in cache
if k.startswith(
"classical:")]
329 quant_keys = [k
for k
in cache
if k.startswith(
"quantum:")]
331 assert len(class_keys) >= 1
332 assert len(quant_keys) >= 1
340 assert "Lower bound (dmin = 5): Exact distance certified" in exp1
341 assert "Upper bound (dmax = 5): Weight of the smallest non-trivial codeword discovered" in exp1
342 assert "Random window steps (rw_steps = 0): Set to 0 because the exact distance d = 5 was proven" in exp1
346 assert "All cluster weights w <= 3 were exhaustively analyzed" in exp2
347 assert "Method 2 (Connected Cluster) is an exhaustive search" in exp2
351 assert "No non-trivial lower bound certified" in exp3
352 assert "120 completed random information set searches" in exp3
356 hx_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
357 lz_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
359 f
"Hx={hx_file}", f
"Hz={hx_file}", f
"Lx={lz_file}", f
"Lz={lz_file}",
360 "method=2",
"wmax=5",
"--no-cache",
"threads=4"
363 captured = capsys.readouterr()
364 assert "dX: 5 5 0 (exact)" in captured.out
365 assert "dZ: 5 5 0 (exact)" in captured.out
366 assert "(d = 5) (exact)" in captured.out
370 h_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
371 l_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
373 "--verbose",
"method=2", f
"finH={h_file}", f
"finL={l_file}",
374 "wmax=5",
"threads=4"
377 captured = capsys.readouterr()
378 assert "Cache retrieval:" in captured.out
379 assert "Lower bound" in captured.out
380 assert "Upper bound" in captured.out
381 assert "Random window steps" in captured.out
394 with tempfile.NamedTemporaryFile(suffix=
".nz", delete=
False)
as f:
399 if os.path.exists(tmp_empty):
403 with tempfile.NamedTemporaryFile(suffix=
".nz", delete=
False, mode=
"w")
as f:
405 tmp_nonempty = f.name
409 if os.path.exists(tmp_nonempty):
410 os.remove(tmp_nonempty)
415 h_file = os.path.join(EXAMPLES_DIR,
"surf_d5_H.mmx")
416 l_file = os.path.join(EXAMPLES_DIR,
"surf_d5_L.mmx")
418 tmp_out = os.path.join(tempfile.gettempdir(), f
"tmp_test_cw_{os.getpid()}.nz")
419 if os.path.exists(tmp_out):
424 "--verbose",
"--no-cache",
"method=2",
425 f
"finH={h_file}", f
"finL={l_file}",
426 f
"finC={tmp_out}", f
"outC={tmp_out}",
427 "wmax=5",
"threads=4"
430 captured = capsys.readouterr()
431 assert "Warning: finC=" in captured.out
432 assert "is empty or non-existent; silently ignoring input codewords." in captured.out
433 assert "5 5 0 (exact)" in captured.out
434 assert os.path.exists(tmp_out)
436 if os.path.exists(tmp_out):
440if __name__ ==
"__main__":
441 pytest.main([__file__,
"-v"])
str find_dist_m4ri_binary(Optional[str] custom_path=None)
int main(Optional[List[str]] argv=None)
str format_bounds_str(List[int] bounds)
List[int] format_bounds_list(int dmin, int dmax, int num_rw)
Tuple[int, int, int] run_dist_m4ri(Optional[str] dist_m4ri_path=None, int method=3, Optional[str] finH=None, Optional[str] finG=None, Optional[str] finL=None, Optional[str] fin=None, Optional[str] finC=None, Optional[str] fdem=None, int dmin=0, int dmax=0, int wmax=0, int wmin=1, int dexp=0, int dest=0, Optional[int] steps=None, Optional[int] threads=None, float timeout=60.0, Optional[int] smax=None, Optional[int] start=None, Optional[int] cbeg=None, Optional[int] cend=None, Optional[int] css=None, int noscan=0, int classical=-1, int dW=-1, int maxC=0, float pmin=0.0, Optional[str] outC=None, int seed=0, int debug=0, Optional[threading.Event] stop_event=None)
Dict[str, Any] get_distance_cache()
Dict[str, Any] parse_cli_args(List[str] argv)
Any compute_classical_distance(Any H, Optional[str] dist_m4ri=None, int method=3, Optional[int] threads=None, float timeout=60.0, Optional[int] num_steps=None, int d_exp=0, int d_min=0, int d_max=0, int dmin=0, int dmax=0, int wmin=1, int wmax=0, Optional[int] smax=None, Optional[int] start=None, Optional[int] cbeg=None, Optional[int] cend=None, int noscan=0, int dW=-1, int maxC=0, Optional[str] finC=None, Optional[str] outC=None, bool do_cws=False, bool return_info=False, Optional[Union[str, Path]] cache_file=None, str solver="dist_m4ri", str codedistance_method="QDistEvol", Optional[Dict[str, Any]] codedistance_params=None, int seed=0, int debug=0, bool verbose=False)
None clear_distance_cache(Optional[Union[str, Path]] cache_file=None, bool clear_file=False)
Optional[Dict[str, Any]] get_cached_distance(Optional[Any] H=None, Optional[Any] G=None, Optional[Any] L=None, Optional[Any] Hx=None, Optional[Any] Hz=None, Optional[Any] Lx=None, Optional[Any] Lz=None, Optional[Any] dem=None, Optional[Any] circuit=None, float pmin=0.0, Optional[Union[str, Path]] cache_file=None)
Tuple[Any,...] compute_dem_distance(Optional[Any] dem=None, Optional[Any] circuit=None, Optional[str] dist_m4ri=None, int method=3, Optional[int] threads=None, float timeout=60.0, Optional[int] num_steps=None, int d_exp=0, int d_min=0, int d_max=0, int dmin=0, int dmax=0, int wmin=1, int wmax=0, Optional[int] smax=None, Optional[int] start=None, Optional[int] cbeg=None, Optional[int] cend=None, int noscan=0, int dW=-1, int maxC=0, float pmin=0.0, Optional[str] finC=None, Optional[str] outC=None, bool do_cws=False, Optional[Union[str, Path]] cache_file=None, str solver="dist_m4ri", str codedistance_method="UndetectableErrorStim", Optional[Dict[str, Any]] codedistance_params=None, int seed=0, int debug=0, bool verbose=False, **kwargs)
None enable_distance_cache()
Optional[str] check_finc_outc(Optional[str] finC, Optional[str] outC, bool verbose=False)
Any compute_quantum_distance(Any H, Optional[Any] G=None, Optional[Any] L=None, Optional[str] dist_m4ri=None, int method=3, Optional[int] threads=None, float timeout=60.0, Optional[int] num_steps=None, int d_exp=0, int d_min=0, int d_max=0, int dmin=0, int dmax=0, int wmin=1, int wmax=0, Optional[int] smax=None, Optional[int] start=None, Optional[int] cbeg=None, Optional[int] cend=None, int noscan=0, int dW=-1, int maxC=0, Optional[str] finC=None, Optional[str] outC=None, bool do_cws=False, bool return_info=False, Optional[Union[str, Path]] cache_file=None, str solver="dist_m4ri", str codedistance_method="QDistEvol", Optional[Dict[str, Any]] codedistance_params=None, int seed=0, int debug=0, bool verbose=False)
Tuple[Any,...] compute_css_distance(Any Hx, Any Hz, Optional[Any] Lx=None, Optional[Any] Lz=None, Optional[str] dist_m4ri=None, int method=3, Optional[int] threads=None, float timeout=60.0, Optional[int] num_steps=None, int d_exp=0, int d_min=0, int d_max=0, int dmin=0, int dmax=0, int wmin=1, int wmax=0, Optional[int] smax=None, Optional[int] start=None, Optional[int] cbeg=None, Optional[int] cend=None, int noscan=0, int dW=-1, int maxC=0, Optional[str] finC=None, Optional[str] outC=None, bool do_cws=False, Optional[Union[str, Path]] cache_file=None, str solver="dist_m4ri", str codedistance_method="QDistEvol", Optional[Dict[str, Any]] codedistance_params=None, int seed=0, int debug=0, bool verbose=False, **kwargs)
str explain_bounds(List[int] bounds, Optional[int] method=None, str label="")
test_classical_distance_file()
test_cli_verbose_mode(capsys)
test_cli_argument_parsing()
test_css_distance_files()
test_quantum_cache_separation()
test_format_bounds_list_and_str()
test_css_distance_sparse()
test_classical_distance_numpy()
test_caching_cumulative_rw_steps()
test_codedistance_solver()
test_run_dist_m4ri_three_numbers()
test_quantum_distance_single_sided()
test_persistent_json_cache(tmp_path)
test_dmin_dmax_parameters()
test_cli_css_dx_dz_bounds(capsys)
test_cli_identical_finc_outc_nonexistent(capsys)