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.
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.
These are used in
TensorTypeto 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.DTypePromotiondefers the output dtype to the numpy promotion rule applied to its arguments. Both are resolved at trace time and never appear in runtime data.shapeentries can be concrete integers or named dimension strings (e.g."n"). Named dimensions are resolved at trace time: if inputxhasTensorType(shape=("n",))and receives a tensor of shape(5,), thenn = 5. All occurrences of"n"within the same operation must resolve to the same value.broadcastable = Truemeans the tensor may carry extra leading (extrinsic) dimensions beyond whatshapespecifies. These leading dimensions participate in numpy-style broadcasting across all broadcastable inputs of the same operation. Non-broadcastable (broadcastable = False) inputs must matchshapeexactly.