Skip to content

Code quality: Replace assert with ValueError in paulialgebra.py #3

@ddri

Description

@ddri

Summary

Replace assert statement with proper ValueError exception in pauli_decompose_hermitian() function.

Classification: Code Quality Improvement

Not a validated bug. Tests pass with python -O (asserts disabled). This is a defensive coding improvement, not a fix for observed failures.

Context

Part of tracking issue #1. The function validates that the number of qubits matches the matrix size, but uses assert instead of a proper exception.

Current Behavior

# paulialgebra.py:554
if qubits is None:
    qubits = list(range(N))
else:
    assert len(qubits) == N

Validation Performed

$ python -O -m pytest quantumflow/ --tb=short
# 1332 passed — no tests fail when asserts are disabled

Why Fix Anyway?

  1. Input validation should use exceptions, not asserts
  2. Follows pattern from upstream PRs Replace assert False with KeyError in DAGCircuit methods gecrooks/quantumflow#133, Replace assert with proper validation in channel_to_kraus() gecrooks/quantumflow#134
  3. Better error messages for users

Technical Details

  • File: quantumflow/paulialgebra.py
  • Line: 554
  • Effort: ~15 minutes
  • Risk: Very low

Proposed Fix

if qubits is None:
    qubits = list(range(N))
elif len(qubits) != N:
    raise ValueError(
        f"Number of qubits ({len(qubits)}) must match matrix size. "
        f"Matrix size {np.size(matrix)} requires {N} qubits."
    )

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions