Skip to content

Code quality: Replace assert with ValueError in visualization.py #2

@ddri

Description

@ddri

Summary

Replace assert statement with proper ValueError exception in circuit_to_latex() 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. Identified during code review. The maintainer left a # FIXME. Throw Exception comment on this line, indicating intent to fix.

Current Behavior

# visualization.py:173
assert package in ["qcircuit", "quantikz"]  # FIXME. Throw Exception

If an invalid package parameter is passed:

  • Normal Python: raises AssertionError
  • With python -O: silently continues, may cause confusing errors later

Validation Performed

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

This means no existing test exercises this code path with invalid input.

Why Fix Anyway?

  1. Maintainer flagged it with # FIXME
  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. Defensive coding — better error messages for users
  4. Asserts should be for internal invariants, not input validation

Technical Details

  • File: quantumflow/visualization.py
  • Line: 173
  • Effort: ~15 minutes
  • Risk: Very low

Proposed Fix

if package not in ["qcircuit", "quantikz"]:
    raise ValueError(f"Invalid LaTeX package '{package}'. Must be 'qcircuit' or 'quantikz'.")

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