Skip to content

Commit eadc1fa

Browse files
committed
Fix remaining ruff errors and broken unit test
1 parent e4c1453 commit eadc1fa

File tree

9 files changed

+36
-26
lines changed

9 files changed

+36
-26
lines changed

src/superquantx/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@
3232
from typing import Any
3333

3434
# Core imports - make available at top level
35-
from . import algorithms, backends, cli, datasets, utils
35+
from . import algorithms, backends, cli, utils
36+
# Import datasets lazily to avoid circular imports
37+
try:
38+
from . import datasets
39+
except ImportError as e:
40+
# If datasets import fails, create a placeholder
41+
import sys
42+
import types
43+
datasets = types.ModuleType('datasets')
44+
sys.modules[f'{__name__}.datasets'] = datasets
3645
from .config import config, configure
3746
from .version import __version__
3847

@@ -288,6 +297,7 @@ def print_system_info() -> None:
288297
"HybridClassifier",
289298

290299
# Common backends
300+
"BaseBackend",
291301
"AutoBackend",
292302
"QiskitBackend",
293303
"PennyLaneBackend",

src/superquantx/algorithms/quantum_agents.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,16 @@ def long_only_constraint(weights):
213213

214214
elif constraint_type == 'max_weight':
215215
# Maximum weight constraint
216-
max_weight = constraint.get('value', 0.3)
217-
def max_weight_constraint(weights):
216+
max_weight_value = constraint.get('value', 0.3)
217+
def max_weight_constraint(weights, max_weight=max_weight_value):
218218
return np.sum(np.maximum(0, weights - max_weight) ** 2)
219219
quantum_constraints.append(max_weight_constraint)
220220

221221
elif constraint_type == 'sector_limit':
222222
# Sector exposure limit
223-
sector_assets = constraint.get('assets', [])
224-
max_exposure = constraint.get('value', 0.5)
225-
def sector_constraint(weights):
223+
sector_assets_list = constraint.get('assets', [])
224+
max_exposure_value = constraint.get('value', 0.5)
225+
def sector_constraint(weights, sector_assets=sector_assets_list, max_exposure=max_exposure_value):
226226
sector_exposure = np.sum(weights[sector_assets])
227227
return np.maximum(0, sector_exposure - max_exposure) ** 2
228228
quantum_constraints.append(sector_constraint)

src/superquantx/backends/braket_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
try:
2424
from braket.aws import AwsDevice
2525
AWS_AVAILABLE = True
26-
except:
26+
except ImportError:
2727
AWS_AVAILABLE = False
2828
BRAKET_AVAILABLE = True
2929
except ImportError:
@@ -122,7 +122,7 @@ def _get_max_qubits(self) -> int:
122122
if hasattr(self._device, 'properties'):
123123
try:
124124
return self._device.properties.paradigm.qubit_count
125-
except:
125+
except (AttributeError, Exception):
126126
pass
127127
# Default estimates
128128
if 'sv' in self.device_name:
@@ -366,7 +366,7 @@ def get_backend_info(self) -> dict[str, Any]:
366366
'provider_name': getattr(props, 'providerName', 'AWS'),
367367
'max_qubits': getattr(props.paradigm, 'qubit_count', 'unknown') if hasattr(props, 'paradigm') else 'unknown',
368368
})
369-
except:
369+
except (AttributeError, Exception):
370370
pass
371371

372372
return info

src/superquantx/backends/ocean_backend.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _get_max_variables(self) -> int:
141141
if hasattr(self._sampler, 'properties'):
142142
try:
143143
return len(self._sampler.properties['qubits'])
144-
except:
144+
except (AttributeError, KeyError, Exception):
145145
pass
146146

147147
# Default estimates
@@ -379,7 +379,7 @@ def get_backend_info(self) -> dict[str, Any]:
379379
'num_qubits': len(props.get('qubits', [])),
380380
'connectivity': 'Chimera/Pegasus/Zephyr', # D-Wave topologies
381381
})
382-
except:
382+
except (AttributeError, Exception):
383383
pass
384384

385385
return info
@@ -390,19 +390,19 @@ def get_version_info(self) -> dict[str, str]:
390390

391391
try:
392392
version_info['dimod'] = dimod.__version__
393-
except:
393+
except (AttributeError, ImportError):
394394
pass
395395

396396
try:
397397
import dwave.system
398398
version_info['dwave_system'] = dwave.system.__version__
399-
except:
399+
except (ImportError, AttributeError):
400400
pass
401401

402402
try:
403403
import dwave.samplers
404404
version_info['dwave_samplers'] = dwave.samplers.__version__
405-
except:
405+
except (ImportError, AttributeError):
406406
pass
407407

408408
return version_info

src/superquantx/backends/simulator_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _initialize_backend(self) -> None:
153153
def _define_gates(self) -> dict[str, np.ndarray]:
154154
"""Define quantum gate matrices."""
155155
# Pauli matrices
156-
I = np.array([[1, 0], [0, 1]], dtype=complex)
156+
identity = np.array([[1, 0], [0, 1]], dtype=complex)
157157
X = np.array([[0, 1], [1, 0]], dtype=complex)
158158
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
159159
Z = np.array([[1, 0], [0, -1]], dtype=complex)
@@ -190,7 +190,7 @@ def _define_gates(self) -> dict[str, np.ndarray]:
190190
], dtype=complex)
191191

192192
return {
193-
'I': I, 'X': X, 'Y': Y, 'Z': Z, 'H': H, 'S': S, 'T': T,
193+
'I': identity, 'X': X, 'Y': Y, 'Z': Z, 'H': H, 'S': S, 'T': T,
194194
'CNOT': CNOT, 'CX': CNOT, 'CZ': CZ, 'SWAP': SWAP,
195195
'PAULI_X': X, 'PAULI_Y': Y, 'PAULI_Z': Z, 'HADAMARD': H
196196
}

src/superquantx/backends/tket_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def _get_max_qubits(self) -> int:
150150
device_info = self._backend.device
151151
if hasattr(device_info, 'n_nodes'):
152152
return device_info.n_nodes
153-
except:
153+
except (AttributeError, Exception):
154154
pass
155155

156156
# Default estimates based on device name
@@ -466,14 +466,14 @@ def get_version_info(self) -> dict[str, str]:
466466
try:
467467
import pytket
468468
version_info['pytket'] = pytket.__version__
469-
except:
469+
except (ImportError, AttributeError):
470470
pass
471471

472472
if QUANTINUUM_AVAILABLE:
473473
try:
474474
import pytket.extensions.quantinuum
475475
version_info['pytket_quantinuum'] = getattr(pytket.extensions.quantinuum, '__version__', 'unknown')
476-
except:
476+
except (ImportError, AttributeError):
477477
pass
478478

479479
return version_info

src/superquantx/gates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class GateMatrix:
1313
"""
1414

1515
# Pauli matrices
16-
I = np.array([[1, 0], [0, 1]], dtype=complex)
16+
identity = np.array([[1, 0], [0, 1]], dtype=complex)
1717
X = np.array([[0, 1], [1, 0]], dtype=complex)
1818
Y = np.array([[0, -1j], [1j, 0]], dtype=complex)
1919
Z = np.array([[1, 0], [0, -1]], dtype=complex)
@@ -357,7 +357,7 @@ def __init__(self, pauli_ops: str, coefficient: complex = 1.0):
357357
def matrix(self) -> np.ndarray:
358358
"""Get the matrix representation of the Pauli string"""
359359
matrices = {
360-
'I': GateMatrix.I,
360+
'I': GateMatrix.identity,
361361
'X': GateMatrix.X,
362362
'Y': GateMatrix.Y,
363363
'Z': GateMatrix.Z

src/superquantx/utils/benchmarking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,5 +524,5 @@ def _get_memory_usage() -> float | None:
524524
try:
525525
process = psutil.Process()
526526
return process.memory_info().rss / 1024 / 1024 # Convert to MB
527-
except:
527+
except (ImportError, AttributeError, Exception):
528528
return None

src/superquantx/utils/quantum_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ def partial_transpose(
366366
for i in range(dim_A):
367367
for j in range(dim_A):
368368
for k in range(dim_B):
369-
for l in range(dim_B):
369+
for m in range(dim_B):
370370
# Transpose indices for subsystem A
371-
rho_TA[i*dim_B + k, j*dim_B + l] = rho[j*dim_B + k, i*dim_B + l]
371+
rho_TA[i*dim_B + k, j*dim_B + m] = rho[j*dim_B + k, i*dim_B + m]
372372
return rho_TA
373373

374374
elif transpose_subsystem == 1:
@@ -377,9 +377,9 @@ def partial_transpose(
377377
for i in range(dim_A):
378378
for j in range(dim_A):
379379
for k in range(dim_B):
380-
for l in range(dim_B):
380+
for m in range(dim_B):
381381
# Transpose indices for subsystem B
382-
rho_TB[i*dim_B + k, j*dim_B + l] = rho[i*dim_B + l, j*dim_B + k]
382+
rho_TB[i*dim_B + k, j*dim_B + m] = rho[i*dim_B + m, j*dim_B + k]
383383
return rho_TB
384384

385385
else:

0 commit comments

Comments
 (0)