Skip to content

Implement DType, TensorType, DTypeVar, DTypePromotion for BackendV3 #15990

@ihincks

Description

@ihincks

These types describe the possible input and output data types of a QuantumProgram's operation nodes, see the parent epic #15902 for context.

There are a finite number of data types possible, representing the data type of an entry in a tensor. We need to reason about the types themselves before the tensor exist because we need to perform operations like tracing a quantum program for dtype and shape validitiy.

class DType(Enum):
    F32  
    F64  
    BIT  
    C128
    U8
    U32
    U64
    I32
    I64  
    ...
DTypeVar("T")                                # named dtype variable, resolved during tracing
DTypePromotion(args=(DTypeVar("x"), DTypeVar("y")))  # resolves to numpy.result_type of its args

These are used in TensorType to define generic or polymorphic operations. DTypeVar("T") acts as a placeholder: the first concrete dtype seen for "T" during tracing fixes it, and all subsequent occurrences must agree. DTypePromotion defers the output dtype to the numpy promotion rule applied to its arguments. Both are resolved at trace time and never appear in runtime data.

@dataclass(frozen=True)
class TensorType:
    dtype: DType | DTypeVar | DTypePromotion
    shape: tuple[int | str, ...]
    broadcastable: bool = False

shape entries can be concrete integers or named dimension strings (e.g. "n"). Named dimensions are resolved at trace time: if input x has TensorType(shape=("n",)) and receives a tensor of shape (5,), then n = 5. All occurrences of "n" within the same operation must resolve to the same value.

broadcastable = True means the tensor may carry extra leading (extrinsic) dimensions beyond what shape specifies. These leading dimensions participate in numpy-style broadcasting across all broadcastable inputs of the same operation. Non-broadcastable (broadcastable = False) inputs must match shape exactly.

Metadata

Metadata

Assignees

Labels

mod: primitivesRelated to the Primitives module

Type

No type

Projects

Status

Ready

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions