From 59c6cd504dd23e00dd123275b51f515baa2a9f8e Mon Sep 17 00:00:00 2001 From: Matthew Silverman Date: Fri, 6 Dec 2024 17:38:39 -0500 Subject: [PATCH] Fix flamingpy build and CI (#127) * add panda to dev requirements * fix some tests * run black * backwards-compatible sparse check * fix missed test and RTD * changelog * add name to contribution * undo xfail --- .github/CHANGELOG.md | 3 ++- .readthedocs.yml | 1 - dev_requirements.txt | 1 + doc/tutorials/run_error_correction.py | 1 - doc/tutorials/run_graph_states.py | 1 - flamingpy/cv/ops.py | 7 ++++++- flamingpy/decoders/unionfind/algos.py | 14 ++++++++------ flamingpy/examples/lc_equivalence.py | 1 + flamingpy/simulations.py | 2 +- tests/cv/test_cv_ops.py | 12 +++++------- tests/examples/test_examples.py | 3 ++- 11 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index ab8e0a49..9410e6fe 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -8,6 +8,7 @@ * An instance of a depracated `fig.gca` with a keyword argument was fixed. [#124](https://github.com/XanaduAI/flamingpy/pull/124) * Remove the tight layout setting from `draw_EGraph_matplotlib`, which was causing a warning. [#125](https://github.com/XanaduAI/flamingpy/pull/125) * Bump tj-actions/branch-names from 5 to 8 to fix vulnerability. [#126](https://github.com/XanaduAI/flamingpy/pull/126) +* Apply minor tweaks to source code and tests to update compatibility across Python versions. [#127](https://github.com/XanaduAI/flamingpy/pull/127) ### Improvements @@ -21,7 +22,7 @@ This release contains contributions from (in alphabetical order): -Nariman Saadatmand +Nariman Saadatmand, [Matthew Silverman](https://github.com/timmysilv) See full commit details ... diff --git a/.readthedocs.yml b/.readthedocs.yml index d944cd4e..efa12158 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -19,4 +19,3 @@ python: - requirements: doc/dev_requirements.txt - method: setuptools path: . - system_packages: true diff --git a/dev_requirements.txt b/dev_requirements.txt index 391a0c35..73845529 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -15,3 +15,4 @@ scipy>=1.6 thewalrus>=0.19.0 plotly>=4.5.0 pylint==2.13.5 +pandas>=2.0 diff --git a/doc/tutorials/run_error_correction.py b/doc/tutorials/run_error_correction.py index 1eca6a5e..b48cb5d0 100644 --- a/doc/tutorials/run_error_correction.py +++ b/doc/tutorials/run_error_correction.py @@ -4,7 +4,6 @@ ==================================== """ - ###################################################################### # *Author: Ilan Tzitrin* # diff --git a/doc/tutorials/run_graph_states.py b/doc/tutorials/run_graph_states.py index c87bd660..f3b8d19d 100644 --- a/doc/tutorials/run_graph_states.py +++ b/doc/tutorials/run_graph_states.py @@ -4,7 +4,6 @@ ============ """ - ###################################################################### # *Authors: Ilan Tzitrin and Luis Mantilla* # diff --git a/flamingpy/cv/ops.py b/flamingpy/cv/ops.py index ba436df2..225545a8 100644 --- a/flamingpy/cv/ops.py +++ b/flamingpy/cv/ops.py @@ -28,6 +28,11 @@ def invert_permutation(p): return p_inverted +def issparse(array): + """Check if an array is sparse. Backwards-compatible with old SciPy versions.""" + return isinstance(array, getattr(sp, "sparray", sp.coo_matrix)) + + def SCZ_mat(adj, sparse=True): """Return a symplectic matrix corresponding to CZ gate application. @@ -59,7 +64,7 @@ def SCZ_mat(adj, sparse=True): # Construct symplectic symplectic = block_func([[identity, zeros], [adj, identity]]) - if not sparse and isinstance(symplectic, sp.coo_matrix): + if not sparse and issparse(symplectic): return symplectic.toarray() return symplectic diff --git a/flamingpy/decoders/unionfind/algos.py b/flamingpy/decoders/unionfind/algos.py index 0fe84a40..11e568c3 100644 --- a/flamingpy/decoders/unionfind/algos.py +++ b/flamingpy/decoders/unionfind/algos.py @@ -108,9 +108,9 @@ def initialize_cluster_trees(stabilizer_graph): root_stabilizer = erasure_graph_nodes[component.pop()] cluster_root = Root( node_dict[root_stabilizer], - parity=root_stabilizer.parity - if isinstance(root_stabilizer, Stabilizer) - else "boundary", + parity=( + root_stabilizer.parity if isinstance(root_stabilizer, Stabilizer) else "boundary" + ), ) # boundary nodes are represented by tuples for vertex in component: vertex_stabilizer = erasure_graph_nodes[vertex] @@ -118,9 +118,11 @@ def initialize_cluster_trees(stabilizer_graph): cluster_root, Root( node_dict[vertex_stabilizer], - parity=vertex_stabilizer.parity - if isinstance(vertex_stabilizer, Stabilizer) - else "boundary", + parity=( + vertex_stabilizer.parity + if isinstance(vertex_stabilizer, Stabilizer) + else "boundary" + ), ), ) if cluster_root.parity: diff --git a/flamingpy/examples/lc_equivalence.py b/flamingpy/examples/lc_equivalence.py index 5604e2c9..070676f8 100644 --- a/flamingpy/examples/lc_equivalence.py +++ b/flamingpy/examples/lc_equivalence.py @@ -1,4 +1,5 @@ """Example for testing LC equivalence of graph states.""" + from flamingpy.utils.graph_states import star_graph, complete_graph, linear_cluster, ring_graph print("Testing LC equivalence of graph states:", "\n") diff --git a/flamingpy/simulations.py b/flamingpy/simulations.py index 1ef73535..30852a0e 100644 --- a/flamingpy/simulations.py +++ b/flamingpy/simulations.py @@ -30,7 +30,7 @@ try: import mpi4py.rc - mpi4py.rc.threaded = False + mpi4py.rc.threads = False from mpi4py import MPI except ImportError: # pragma: no cover warnings.warn("Failed to import mpi4py libraries.", ImportWarning) diff --git a/tests/cv/test_cv_ops.py b/tests/cv/test_cv_ops.py index 5907a73d..bb1de7ea 100644 --- a/tests/cv/test_cv_ops.py +++ b/tests/cv/test_cv_ops.py @@ -25,7 +25,7 @@ import scipy.sparse as sp from flamingpy.codes.graphs import EGraph -from flamingpy.cv.ops import invert_permutation, SCZ_mat, SCZ_apply +from flamingpy.cv.ops import invert_permutation, SCZ_mat, SCZ_apply, issparse now = datetime.now() int_time = int(str(now.year) + str(now.month) + str(now.day) + str(now.hour) + str(now.minute)) @@ -50,13 +50,11 @@ def random_graph(request): class TestSCZ: """Tests for symplectic CZ matrices.""" - @pytest.mark.parametrize( - "sparse, expected_out_type", sorted([(True, sp.coo_matrix), (False, np.ndarray)]) - ) - def test_SCZ_mat_sparse_param(self, random_graph, sparse, expected_out_type): + @pytest.mark.parametrize("sparse", [True, False]) + def test_SCZ_mat_sparse_param(self, random_graph, sparse): """Tests the SCZ_mat function outputs sparse or dense arrays.""" SCZ = SCZ_mat(random_graph[2], sparse=sparse) - assert isinstance(SCZ, expected_out_type) + assert issparse(SCZ) if sparse else isinstance(SCZ, np.ndarray) def test_SCZ_mat(self, random_graph): """Tests the SCZ_mat function.""" @@ -65,7 +63,7 @@ def test_SCZ_mat(self, random_graph): # Check if SCZ_mat adjusts type of output matrix based on # type of input. assert isinstance(SCZ, np.ndarray) - assert isinstance(SCZ_sparse, sp.coo_matrix) + assert issparse(SCZ_sparse) # Check that structure of SCZ matrix is correct. for mat in (SCZ, SCZ_sparse.toarray()): assert np.array_equal(mat[:N, :N], np.identity(N)) diff --git a/tests/examples/test_examples.py b/tests/examples/test_examples.py index 4777637c..a30f26f5 100644 --- a/tests/examples/test_examples.py +++ b/tests/examples/test_examples.py @@ -17,6 +17,7 @@ # pylint: disable=import-outside-toplevel,unused-import import pytest +import numpy as np from flamingpy.codes import alternating_polarity @@ -31,7 +32,7 @@ def test_decoder_example(noise, decoder): ec = "primal" result = decode_surface_code(distance, boundaries, ec, noise, decoder, draw=True) - assert result.__class__.__name__ == "bool_" + assert isinstance(result, np.bool_) def test_gkp_example():