Skip to content

[BUG] Batched-coefficient qml.s_prod crashes in label(decimals=...) and qml.draw #9633

Description

@YujinSong-hep

Expected behavior

qml.s_prod documents support for batched coefficients, including examples such as scalar=[1, 2, 3].

A valid batched-coefficient scalar product operator should remain renderable through normal inspection paths such as op.label(decimals=...) and qml.draw.

If a special display format is preferred for batched coefficients, PennyLane should produce that display or gracefully fall back to an array-style label. It should not crash while formatting the coefficient.

Actual behavior

A batched-coefficient SProd can be constructed, and its matrix can be computed, but rendering crashes:

matrix shape: (2, 2, 2)
label error: TypeError unsupported format string passed to numpy.ndarray.__format__
draw error: TypeError unsupported format string passed to numpy.ndarray.__format__

Additional information

The input is documented as legal. The qml.s_prod docstring says the operator supports a batched coefficient and gives an example with a list-valued scalar.

The crash appears to come from pennylane/ops/op_math/sprod.py::SProd.label:

format(math.toarray(self.scalar), f".{decimals}f")

This assumes the coefficient is a scalar. For a documented batched coefficient, math.toarray(self.scalar) is an ndarray, and Python's scalar float format mini-language raises TypeError.

Source code

#Direct `label` reproducer:

import numpy as np
import pennylane as qml

op = qml.s_prod(np.array([1.234, 2.345]), qml.X(0))

print("matrix shape:", qml.matrix(op).shape)
print("label:", op.label(decimals=2))

#`qml.draw` reproducer:


import numpy as np
import pennylane as qml

op = qml.s_prod(np.array([1.234, 2.345]), qml.X(0))
dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def circuit():
    return qml.expval(op)

print(qml.draw(circuit)())

Tracebacks

Output:

TypeError: unsupported format string passed to numpy.ndarray.__format__

TypeError: unsupported format string passed to numpy.ndarray.__format__

System information

Version: 0.45.0
Platform info: macOS
Python version: 3.13.13

Existing GitHub issues

  • I have searched existing GitHub issues to make sure the issue does not already exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't workingcommunity-botIssue suspected to be found by a bot

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions