Skip to content

Commit 2982046

Browse files
authored
Fixes test warnings (#128)
* Fixes test warnings
1 parent 2fb6b16 commit 2982046

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

python/quantum-pecos/src/pecos/decoders/mwpm2d/mwpm2d.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,23 @@ def decode(self, measurements, error_params=None):
9393
real_graph.add_edge(vi, vj, weight=0)
9494

9595
# Find a matching
96-
matching_edges = nx.max_weight_matching(real_graph, maxcardinality=True)
96+
# Handle different NetworkX versions
97+
try:
98+
# For NetworkX >= 2.5
99+
matching_edges = nx.algorithms.matching.max_weight_matching(
100+
real_graph,
101+
maxcardinality=True,
102+
)
103+
# Convert to a list of tuples if it's a set of frozen sets (NetworkX 2.5+)
104+
if isinstance(matching_edges, set):
105+
matching_edges = list(matching_edges)
106+
except (TypeError, AttributeError):
107+
# For older NetworkX versions
108+
matching_edges = nx.max_weight_matching(
109+
real_graph,
110+
maxcardinality=True,
111+
)
112+
97113
matching = {n1: n2 for n2, n1 in matching_edges}
98114
matching.update(dict(matching_edges))
99115

python/quantum-pecos/src/pecos/decoders/mwpm2d/precomputing.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ def code_surface4444medial(instr):
8686
return decoder_data
8787

8888

89+
def compute_all_shortest_paths(graph):
90+
"""
91+
Compute all shortest paths in a graph, handling NetworkX API changes.
92+
93+
This function will explicitly generate the all-pairs shortest paths
94+
to be compatible with different NetworkX versions.
95+
96+
Args:
97+
graph: NetworkX graph
98+
99+
Returns:
100+
Dictionary of dictionaries with path[source][target] = list of nodes in path
101+
"""
102+
# Compute all-pairs shortest paths explicitly
103+
all_paths = {}
104+
for source in graph.nodes():
105+
# For each source, get paths to all targets
106+
source_paths = nx.single_source_shortest_path(graph, source)
107+
all_paths[source] = source_paths
108+
109+
return all_paths
110+
111+
89112
def surface4444_identity(instr):
90113
"""For X and Z decoding separately:
91114
@@ -246,7 +269,8 @@ def surface4444_identity(instr):
246269
edge2d = edges_z
247270
virtual_edge_data = virtual_edge_data_z
248271

249-
paths = dict(nx.shortest_path(temp_graph))
272+
# Use a future-proof approach to get all shortest paths
273+
paths = compute_all_shortest_paths(temp_graph)
250274

251275
for n1, wdict in paths.items():
252276
for n2, syn_path in wdict.items():
@@ -499,7 +523,8 @@ def surface4444medial_identity(instr):
499523
edge2d = edges_z
500524
virtual_edge_data = virtual_edge_data_z
501525

502-
paths = dict(nx.shortest_path(temp_graph))
526+
# Use a future-proof approach to get all shortest paths
527+
paths = compute_all_shortest_paths(temp_graph)
503528

504529
for n1, wdict in paths.items():
505530
for n2, syn_path in wdict.items():

python/quantum-pecos/src/pecos/simulators/basic_sv/gates_two_qubit.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def _apply_two_qubit_matrix(state, qubits: tuple[int, int], matrix: np.array) ->
3535
raise ValueError(msg)
3636

3737
# Reshape the matrix into an ndarray of shape (2,2,2,2)
38-
reshaped_matrix = np.reshape(matrix, newshape=(2, 2, 2, 2))
38+
# Use positional argument for backward compatibility with NumPy < 2.0
39+
reshaped_matrix = np.reshape(matrix, (2, 2, 2, 2))
3940

4041
# Use np.einsum to apply the gate to `qubit`.
4142
# To do so, we need to assign subscript labels to each array axis.

python/quantum-pecos/src/pecos/simulators/basic_sv/state.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ def reset(self):
106106
self.internal_vector[0] = 1
107107
# Internally use a ndarray representation so that it's easier to apply gates
108108
# without needing to apply tensor products.
109+
110+
# Use positional argument for backward compatibility with NumPy < 2.0
109111
self.internal_vector = np.reshape(
110112
self.internal_vector,
111-
newshape=[2] * self.num_qubits,
113+
[2] * self.num_qubits,
112114
)
113115
return self
114116

115117
@property
116118
def vector(self) -> ArrayLike:
117-
return np.reshape(self.internal_vector, newshape=2**self.num_qubits)
119+
# Use positional argument for backward compatibility with NumPy < 2.0
120+
return np.reshape(self.internal_vector, 2**self.num_qubits)

python/tests/pytest.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ markers =
1818
# ProjectQ has a bunch of deprecation warnings from NumPy because they still use np.matrix instead of np.array
1919
# TODO: comment this to deal with ProjectQ gate warnings
2020
filterwarnings =
21-
ignore::PendingDeprecationWarning:projectq.ops._gates
22-
ignore::DeprecationWarning:dateutil.tz.tz.*
21+
ignore::PendingDeprecationWarning:projectq.*
2322
ignore::DeprecationWarning:cuquantum

0 commit comments

Comments
 (0)