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

Improve test coverage #14

Merged
merged 41 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
0eb1604
test [test_graphstates] - test_SCZ_apply
sduquemesa Apr 12, 2022
3fc7c94
refactor + test [simulations]
sduquemesa Apr 12, 2022
53a3b4a
format - black
sduquemesa Apr 12, 2022
f24b9e8
linting- codefactor
sduquemesa Apr 12, 2022
012320e
coverage [utils/viz] - no coverage for `plt.plot` lines
sduquemesa Apr 12, 2022
00b57a0
test [iid_noise] - exception raising
sduquemesa Apr 12, 2022
cb3584a
refactor + test [examples/surface_code]
sduquemesa Apr 12, 2022
5a0a0ce
test [examples/surface_code] - remove coverage from `" __main__"`
sduquemesa Apr 13, 2022
7a3d62d
test [examples/graphstates] - remove coverage from `plt.plot` lines
sduquemesa Apr 13, 2022
d7a3559
refactor + test [examples/decoding]
sduquemesa Apr 13, 2022
8f5376d
Merge branch 'main' into improve-test-coverage
sduquemesa Apr 13, 2022
6ce1c21
test [examples/decoding] - remove coverage from `" __main__"`
sduquemesa Apr 13, 2022
da32eae
test [simulations] - append result to created file
sduquemesa Apr 13, 2022
6153d0e
test [__init__] - remove coverage from ImportError
sduquemesa Apr 13, 2022
aa15951
test [__init__] - version
sduquemesa Apr 13, 2022
dc91b83
refactor+test [benchmarks+simulations] - check execution output to file
sduquemesa Apr 13, 2022
de973bf
test [benchmarks] - remove coverage from __main__
sduquemesa Apr 13, 2022
e38e90d
test [decoders/mwpm/lemon] - ignore coverage on exception raising
sduquemesa Apr 13, 2022
409d1a6
format - pylint
sduquemesa Apr 13, 2022
ab1a8c8
format - add docstrings
sduquemesa Apr 13, 2022
5d1815e
workflows [tests] - `cov-fail-under=95`
sduquemesa Apr 14, 2022
c27d6d8
Merge branch 'main' into improve-test-coverage
sduquemesa Apr 18, 2022
cf6a77a
fix [examples/decoding] - `draw_matching_on_syndrome_plot` args
sduquemesa Apr 18, 2022
c88d403
fix [coverage] - ignore `# pragma: no cover` lines
sduquemesa Apr 18, 2022
649ebba
fix [test_init] - remove duplicated test
sduquemesa Apr 18, 2022
d343df6
coverage [coveragerc] - move unnecessary `# pragma: no cover` to `exc…
sduquemesa Apr 18, 2022
7006d29
Apply suggestions from code review
sduquemesa Apr 21, 2022
aba60c5
Merge branch 'main' into improve-test-coverage
sduquemesa Apr 21, 2022
17bcbbe
refactor [test_simulations] - merge tests
sduquemesa Apr 21, 2022
418f5bf
format - black + docs
sduquemesa Apr 21, 2022
3fdcfd1
refactor [test_init] - test_version
sduquemesa Apr 21, 2022
b7338ad
fix [test_simulations] - output file
sduquemesa Apr 21, 2022
f4b4db4
fix [simulations] - execution from main
sduquemesa Apr 21, 2022
6adfbde
refactor [examples] - decoding: draw intermediate results
sduquemesa Apr 28, 2022
e3af484
refactor [test_SCZ_apply]
sduquemesa Apr 29, 2022
d31dfe1
Merge branch 'main' into improve-test-coverage
ilan-tz May 2, 2022
fc1f260
apply suggestions from code review
sduquemesa May 3, 2022
e5d8941
Merge branch 'improve-test-coverage' of github.com:XanaduAI/flamingpy…
sduquemesa May 3, 2022
467c89b
add CHANGELOG entry
sduquemesa May 3, 2022
2fa1932
Update _version.py
ilan-tz May 3, 2022
669342a
Update _version.py
ilan-tz May 4, 2022
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
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ source = flamingpy

exclude_lines =
if __name__ == .__main__.:
pragma: no cover
plt.show()
2 changes: 1 addition & 1 deletion .github/workflows/build_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
python setup.py build_cmake --inplace

- name: Run tests
run: python -m pytest tests/ --cov=flamingpy --cov-report=xml --cov-report=term-missing -p no:warnings --tb=native --cov-fail-under=90
run: python -m pytest tests/ --cov=flamingpy --cov-report=xml --cov-report=term-missing -p no:warnings --tb=native --cov-fail-under=95

- name: Adjust coverage file for Codecov
run: bash <(sed -i 's/filename=\"/filename=\"flamingpy\//g' coverage.xml)
Expand Down
2 changes: 1 addition & 1 deletion flamingpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
try:
import flamingpy.cpp.lemonpy as lp
import flamingpy.cpp.cpp_mc_loop as cmc
except ImportError:
except ImportError: # pragma: no cover
warnings.warn("Failed to import flamingpy.cpp libraries.", ImportWarning)


Expand Down
1 change: 1 addition & 0 deletions flamingpy/benchmarks/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@
plt.xlabel("Times [seconds]")
plt.ylabel("Count")
plt.title(f"Decoding for code distance {distance}")

if __name__ == "__main__":
plt.savefig(f"benchmark_decoding_distance_{distance}.pdf")
1 change: 1 addition & 0 deletions flamingpy/benchmarks/lemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
plt.ylabel("Time")
plt.yscale("log")
plt.legend(loc="best")

if __name__ == "__main__":
plt.savefig("lemon_benchmark.pdf")
1 change: 1 addition & 0 deletions flamingpy/benchmarks/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@
plt.xlabel("Times [seconds]")
plt.ylabel("Count")
plt.title(f"Matching for code distance {distance}")

if __name__ == "__main__":
plt.savefig(f"benchmark_matching_distance_{distance}.pdf")
1 change: 1 addition & 0 deletions flamingpy/benchmarks/shortest_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@
plt.xlabel("Times [seconds]")
plt.ylabel("Count")
plt.title(f"Building matching graph for code distance {distance}")

if __name__ == "__main__":
plt.savefig(f"benchmark_shortest_path_distance_{distance}.pdf")
161 changes: 87 additions & 74 deletions flamingpy/benchmarks/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"""Benchmark for the Monte Carlo simulations estimating FT thresholds and
comparing python and cpp loops."""

# pylint: disable=too-many-arguments

# pylint: disable=no-self-use,protected-access,redefined-outer-name


import csv
import warnings
Expand All @@ -29,7 +31,7 @@

try:
import flamingpy.cpp.cpp_mc_loop as cmc
except ImportError:
except ImportError: # pragma: no cover
warnings.warn("Failed to import flamingpy.cpp.cpp_mc_loop library.", ImportWarning)


Expand Down Expand Up @@ -92,79 +94,90 @@ def ec_monte_carlo(code, trials, delta, p_swap, passive_objects=None, backend="c
return errors


# The Monte Carlo simulations

# Arguments
distance = 2
ec = "primal"
boundaries = "open"
delta = 0.01
p_swap = 0.5
trials = 100
passive = True

# The qubit code
RHG_code = SurfaceCode(distance, ec, boundaries)
RHG_lattice = RHG_code.graph
RHG_lattice.index_generator()
if passive:
# The lattice with macronodes.
pad_bool = boundaries != "periodic"
RHG_macro = RHG_lattice.macronize(pad_boundary=pad_bool)
RHG_macro.index_generator()
RHG_macro.adj_generator(sparse=True)
# The empty CV state, uninitiated with any error model.
CVRHG_reduced = CVLayer(RHG_lattice)
# Define the 4X4 beamsplitter network for a given macronode.
# star at index 0, planets at indices 1-3.
bs_network = BS_network(4)
passive_objects = [RHG_macro, RHG_lattice, CVRHG_reduced, bs_network]
else:
passive_objects = None

tic = process_time()
errors = ec_monte_carlo(RHG_code, trials, delta, p_swap, passive_objects, backend="python")
walltime_py = process_time() - tic

tic = process_time()
ec_monte_carlo(RHG_code, trials, delta, p_swap, passive_objects, backend="cpp")
walltime_cpp = process_time() - tic

# Store results in a sims_data directory in the file simulations_results.csv.
file_name = "./flamingpy/sims_data/sims_benchmark_results.csv"
# Create a CSV file if it doesn't already exist.
try:
file = open(file_name, "x", newline="", encoding="utf8")
writer = csv.writer(file)
def run_sims_benchmark(distance, ec, boundaries, delta, p_swap, trials, passive, fname=None):
"""Run full Monte Carlo error-correction simulations benchmarks.

Compare the Cython and regular Python backends. Results are stored
in a file in the sims_data directory (with name fname, if supplied).
"""

# The qubit code
RHG_code = SurfaceCode(distance, ec, boundaries)
RHG_lattice = RHG_code.graph
RHG_lattice.index_generator()
if passive:
# The lattice with macronodes.
pad_bool = boundaries != "periodic"
RHG_macro = RHG_lattice.macronize(pad_boundary=pad_bool)
RHG_macro.index_generator()
RHG_macro.adj_generator(sparse=True)
# The empty CV state, uninitiated with any error model.
CVRHG_reduced = CVLayer(RHG_lattice)
# Define the 4X4 beamsplitter network for a given macronode.
# star at index 0, planets at indices 1-3.
bs_network = BS_network(4)
passive_objects = [RHG_macro, RHG_lattice, CVRHG_reduced, bs_network]
else:
passive_objects = None

# Monte Carlo simulations
tic = process_time()
errors = ec_monte_carlo(RHG_code, trials, delta, p_swap, passive_objects, backend="python")
walltime_py = process_time() - tic

tic = process_time()
ec_monte_carlo(RHG_code, trials, delta, p_swap, passive_objects, backend="cpp")
walltime_cpp = process_time() - tic

# Store results in a sims_data directory in the file simulations_results.csv.
file_name = fname or "./flamingpy/sims_data/sims_benchmark_results.csv"
# Create a CSV file if it doesn't already exist.
try:
file = open(file_name, "x", newline="", encoding="utf8")
writer = csv.writer(file)
writer.writerow(
[
"distance",
"ec",
"boundaries",
"delta",
"p_swap",
"errors_py",
"trials",
"current_time",
"cpp_to_py_speedup",
]
)
# Open the file for appending if it already exists.
except FileExistsError:
file = open(file_name, "a", newline="", encoding="utf8")
writer = csv.writer(file)
current_time = datetime.now().time().strftime("%H:%M:%S")
writer.writerow(
[
"distance",
"ec",
"boundaries",
"delta",
"p_swap",
"errors_py",
"trials",
"current_time",
"cpp_to_py_speedup",
distance,
ec,
boundaries,
delta,
p_swap,
errors,
trials,
current_time,
walltime_py / walltime_cpp,
]
)
# Open the file for appending if it already exists.
except FileExistsError:
file = open(file_name, "a", newline="", encoding="utf8")
writer = csv.writer(file)
current_time = datetime.now().time().strftime("%H:%M:%S")
writer.writerow(
[
distance,
ec,
boundaries,
delta,
p_swap,
errors,
trials,
current_time,
walltime_py / walltime_cpp,
]
)
file.close()
file.close()


if __name__ == "__main__":

# Arguments
distance = 2
ec = "primal"
boundaries = "open"
delta = 0.01
p_swap = 0.5
trials = 100
passive = True

run_sims_benchmark(ec_monte_carlo, distance, ec, boundaries, delta, p_swap, trials, passive)
2 changes: 1 addition & 1 deletion flamingpy/decoders/mwpm/lemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

try:
import flamingpy.cpp.lemonpy as lp
except ImportError:
except ImportError: # pragma: no cover
warnings.warn("Failed to import flamingpy.cpp.lemonpy library.", ImportWarning)


Expand Down
Loading