Skip to content

Commit

Permalink
Fix flamingpy build and CI (#127)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
timmysilv authored Dec 6, 2024
1 parent 282e7df commit 59c6cd5
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 ...

Expand Down
1 change: 0 additions & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ python:
- requirements: doc/dev_requirements.txt
- method: setuptools
path: .
system_packages: true
1 change: 1 addition & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ scipy>=1.6
thewalrus>=0.19.0
plotly>=4.5.0
pylint==2.13.5
pandas>=2.0
1 change: 0 additions & 1 deletion doc/tutorials/run_error_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
====================================
"""


######################################################################
# *Author: Ilan Tzitrin*
#
Expand Down
1 change: 0 additions & 1 deletion doc/tutorials/run_graph_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
============
"""


######################################################################
# *Authors: Ilan Tzitrin and Luis Mantilla*
#
Expand Down
7 changes: 6 additions & 1 deletion flamingpy/cv/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions flamingpy/decoders/unionfind/algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,21 @@ 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]
union(
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:
Expand Down
1 change: 1 addition & 0 deletions flamingpy/examples/lc_equivalence.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
2 changes: 1 addition & 1 deletion flamingpy/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions tests/cv/test_cv_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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."""
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion tests/examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# pylint: disable=import-outside-toplevel,unused-import

import pytest
import numpy as np
from flamingpy.codes import alternating_polarity


Expand All @@ -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():
Expand Down

0 comments on commit 59c6cd5

Please sign in to comment.