Skip to content

✨ Add qtensor dialect to MLIR#1542

Draft
li-mingbao wants to merge 21 commits intomunich-quantum-toolkit:mainfrom
li-mingbao:qtensor
Draft

✨ Add qtensor dialect to MLIR#1542
li-mingbao wants to merge 21 commits intomunich-quantum-toolkit:mainfrom
li-mingbao:qtensor

Conversation

@li-mingbao
Copy link
Contributor

Description

This PR adds the qtensor dialect to the environment.
The qtensor dialect is an adjusted version of the standard tensor dialect that supports linear typing for tensors with qubit types.

At the moment the following operations are supported:
extract, extract_slice, from_elements, insert, insert_slice.

  • Both extract operations are modified so that they also return the input tensor as result.
  • The insert and the from_elements operations are just wrapper operations that are canonicalized to a standard tensor operation.
  • The insert_slice is identical to the standard operation but the canonicalizations are slightly adjusted to fit the new extract operation.

In addition, a qtensor.dealloc operation is added that deallocates a tensor and serves as a sink operation.

Example:

module {
  func.func @main() -> i64 attributes {passthrough = ["entry_point"]} {
    %c0_i64 = arith.constant 0 : i64
    %c0 = arith.constant 0 : index
    %0 = qco.alloc : !qco.qubit
    %1 = qco.alloc : !qco.qubit
    %2 = qco.alloc : !qco.qubit
    %from_elements = tensor.from_elements %0, %1, %2 : tensor<3x!qco.qubit>
    %q_extracted_slice:2 = qtensor.extract_slice %from_elements[0] [2] [1] : tensor<3x!qco.qubit> to tensor<2x!qco.qubit>
    %q_extracted:2 = qtensor.extract %q_extracted_slice#0[%c0] : tensor<2x!qco.qubit>
    %3 = qco.h %q_extracted#0 : !qco.qubit -> !qco.qubit
    %inserted = tensor.insert %3 into %q_extracted#1[%c0] : tensor<2x!qco.qubit>
    %inserted_slice = qtensor.insert_slice %inserted into %q_extracted_slice#1[0] [2] [1] : tensor<2x!qco.qubit> into tensor<3x!qco.qubit>
    qtensor.dealloc %inserted_slice : tensor<3x!qco.qubit>
    return %c0_i64 : i64
  }
}

Checklist:

  • The pull request only contains commits that are focused and relevant to this change.
  • I have added appropriate tests that cover the new/changed functionality.
  • I have updated the documentation to reflect these changes.
  • I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals.
  • I have added migration instructions to the upgrade guide (if needed).
  • The changes follow the project's style guidelines and introduce no new warnings.
  • The changes are fully tested and pass the CI checks.
  • I have reviewed my own code changes.

@li-mingbao
Copy link
Contributor Author

@denialhaag @burgholzer
This is the implementation of the proposed qtensor dialect.
It is still a WIP as I have to adjust the copied code parts but the base implementation should work.

Currently the new operations are implemented on top of the other operations and do not replace the allocQubitRegister operation yet.
Also, currently when you create a qubit tensor with the QCOProgramBuilder, it creates the given amount of qubits with alloc and then create the tensor with from_elements. The qubits created in this way are not tracked and only the resulting tensor is tracked.
Therefore the qubits are not explicitly deallocated but only the tensor at the end is deallocated.

Let me know if this is ok or if the qubits should be deallocated separately with the tensor at the end.

@denialhaag
Copy link
Member

@denialhaag @burgholzer This is the implementation of the proposed qtensor dialect. It is still a WIP as I have to adjust the copied code parts but the base implementation should work.

Thanks a lot for getting this started! 🙂

Also, currently when you create a qubit tensor with the QCOProgramBuilder, it creates the given amount of qubits with alloc and then create the tensor with from_elements.

Is there an argument against defining a QTensor_AllocOp as a counterpart to QTensor_DeallocOp? That would allow us to avoid the superfluous qubit allocations. 🤔

Therefore the qubits are not explicitly deallocated but only the tensor at the end is deallocated.

Let me know if this is ok or if the qubits should be deallocated separately with the tensor at the end.

That sounds fine to me! If a qubit is extracted and modified, it should probably be inserted again before the tensor is deallocated, though. 🤔

@denialhaag denialhaag added feature New feature or request MLIR Anything related to MLIR labels Mar 4, 2026
@burgholzer
Copy link
Member

@denialhaag @burgholzer This is the implementation of the proposed qtensor dialect. It is still a WIP as I have to adjust the copied code parts but the base implementation should work.

Thanks a lot for getting this started! 🙂

Also from my side: thanks! this is great to see 🙌🏼

Also, currently when you create a qubit tensor with the QCOProgramBuilder, it creates the given amount of qubits with alloc and then create the tensor with from_elements.

Is there an argument against defining a QTensor_AllocOp as a counterpart to QTensor_DeallocOp? That would allow us to avoid the superfluous qubit allocations. 🤔

I think I would also prefer that. This matches more closely with the notion of how registers are allocated and feels quite natural to me.
I believe quite some nice convenience can be built around the QCO Program Builder and the concept of a quantum register.

Therefore the qubits are not explicitly deallocated but only the tensor at the end is deallocated.
Let me know if this is ok or if the qubits should be deallocated separately with the tensor at the end.

That sounds fine to me! If a qubit is extracted and modified, it should probably be inserted again before the tensor is deallocated, though. 🤔

To guarantee proper linear types any qubit and any register value needs to be used exactly once, i.e., every value needs to have a sink.
Either all qubits are inserted back into the register before the register itself is being deallocated (my preference at the moment), or all qubits are individually deallocated and then the register is deallocated (I don't particularly like the semantics of that), but it would work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants