Skip to content

Add a len subcommand (print the number of sites in a Pauli string) #9

Description

@moon-dust

Background

qsym-rs is a deliberately small symbolic Pauli-algebra engine and qsym CLI (see docs/design/qsym-rs.md). A Pauli string has one operator per site, and the CLI already exposes that length inside qsym show XIZ, which prints a report containing len=3. The CLI also has a script-friendly weight subcommand that prints only the Pauli weight.

There is no script-friendly way to ask only for the length. Users who want just the site count must parse the human-readable show output.

Task

Add a new len subcommand: qsym len <A>.

It should parse one Pauli string the same way show and weight do. On success, print exactly the number of sites as a decimal integer followed by a newline. On parse failure, reuse the existing CLI error path: non-zero exit and error: ... on stderr.

This should be a CLI-only change in src/main.rs: add a Len { a: String } variant to Cmd and a match arm that calls PauliString::parse(&a)?, then prints p.ops.len(). Do not change src/lib.rs.

Verification

  1. cargo run -q -- len XIZ → stdout is exactly 3, exit code 0.
  2. Distinguishing fixture: cargo run -q -- len IIIIX → stdout is exactly 5. This catches implementations that hardcode the first example's length.
  3. Negative control: cargo run -q -- len XQZ → exits non-zero and stderr contains error: not a Pauli letter. This proves the command reuses normal parsing instead of counting raw characters blindly.
  4. Regression check: cargo run -q -- weight XIZ still prints exactly 2, and cargo run -q -- show XIZ still includes len=3 weight=2.

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