‘qtensor’ Dialect

The QTensor dialect for one-dimensional tensors of qubits with linear semantics.

QTensor groups QCO qubits into one-dimensional tensors, for example to represent a register. Like individual QCO qubit values, each tensor value has exactly one use.

Extracting a qubit consumes the tensor and returns both the qubit and the remaining tensor. The extracted slot is unavailable until the qubit is inserted back into it. Gates may change the qubit’s state, but reinsertion must restore the same qubit identity to the same slot. Allocation and deallocation create and consume complete tensors.

A gate or call must receive distinct quantum resources. The shared quantum resource rules explain how these rules apply to helpers, loops, and dynamically computed indices.

Operations

qtensor.alloc (qtensor::AllocOp)

Tensor alloc operation

Syntax:

operation ::= `qtensor.alloc` `(`$size`)` attr-dict `:` type($result)

Allocates a one-dimensional qubit tensor with the given size and returns the allocated tensor. The qubits are initialized to the |0> state. If the size is a constant the resulting tensor has a static size, otherwise the tensor has a dynamic size.

Example:

%qtensor = qtensor.alloc(%c3) : tensor<3x!qco.qubit>

Interfaces: MemoryEffectOpInterface (MemoryEffectOpInterface), OpAsmOpInterface

Effects: MemoryEffects::Effect{MemoryEffects::Allocate on ::mlir::SideEffects::DefaultResource}

Operands:

Operand

Description

size

index

Results:

Result

Description

result

1D tensor of QCO qubit value type values

qtensor.dealloc (qtensor::DeallocOp)

Deallocate a tensor

Syntax:

operation ::= `qtensor.dealloc` $tensor attr-dict `:` type($tensor)

This operation deallocates the tensor and the values it holds, releasing its resources. Qubits that were extracted but not inserted again need to be deallocated separately.

Example:

qtensor.dealloc %tensor : tensor<3x!qco.qubit>

Interfaces: MemoryEffectOpInterface (MemoryEffectOpInterface)

Effects: MemoryEffects::Effect{MemoryEffects::Free on ::mlir::SideEffects::DefaultResource}

Operands:

Operand

Description

tensor

1D tensor of QCO qubit value type values

qtensor.extract (qtensor::ExtractOp)

Extract element from tensor

Syntax:

operation ::= `qtensor.extract` $tensor `[` $index `]` attr-dict `:` type($tensor)

Consume a tensor and return its qubit at the given index together with the remaining tensor. The index must be in bounds and its slot must not already be extracted. Restore the updated qubit to the same slot before passing the complete tensor through a helper or region boundary.

Example:

%outTensor, %q0 = qtensor.extract %tensor[%c0] : tensor<4x!qco.qubit>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface), OpAsmOpInterface

Effects: MemoryEffects::Effect{}

Operands:

Operand

Description

tensor

1D tensor of QCO qubit value type values

index

index

Results:

Result

Description

out_tensor

1D tensor of QCO qubit value type values

result

QCO qubit value type

qtensor.from_elements (qtensor::FromElementsOp)

Create a tensor from a range of values

Syntax:

operation ::= `qtensor.from_elements` $elements attr-dict `:` type($result)

The qtensor.from_elements operation is a wrapper operation of the tensor.from_elements operation. This operation creates a one-dimensional qubit tensor using the operands as its input. This also causes the input qubits to be consumed.

Example:

%tensor = qtensor.from_elements %q0, %q1, %q2 : tensor<3x!qco.qubit>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, NoMemoryEffect (MemoryEffectOpInterface), OpAsmOpInterface

Effects: MemoryEffects::Effect{}

Operands:

Operand

Description

elements

variadic of QCO qubit value type

Results:

Result

Description

result

Tensor must be statically shaped and elements must be of qubit type

qtensor.insert (qtensor::InsertOp)

Insert element into tensor

Syntax:

operation ::= `qtensor.insert` $scalar `into` $dest `[` $index `]` attr-dict `:` type($dest)

Consume an extracted qubit and the remaining tensor, restoring the qubit to its original slot. The index must be in bounds and identify that slot. Insertion preserves qubit identity; it cannot move a qubit between slots or replace another qubit. Dynamic index equality is a program precondition.

Example:

%newTensor = qtensor.insert %q0 into %tensor[%c0] : tensor<3x!qco.qubit>

Traits: AlwaysSpeculatableImplTrait

Interfaces: ConditionallySpeculatable, InferTypeOpInterface, NoMemoryEffect (MemoryEffectOpInterface), OpAsmOpInterface

Effects: MemoryEffects::Effect{}

Operands:

Operand

Description

scalar

QCO qubit value type

dest

1D tensor of QCO qubit value type values

index

index

Results:

Result

Description

result

1D tensor of QCO qubit value type values

Passes

-qtensor-shrink-to-fit

Compact static quantum tensors

Compacts static quantum tensors whose linear allocation-to-deallocation chain consists of constant-index extracts and inserts. Retained indices preserve their relative order and source-qubit mapping.

Run canonicalization first to fold size and index expressions. Tensors with nonconstant indices or unsupported uses are left unchanged.