Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding permute function for reordering tensor products of quantum objects #151

Closed
wants to merge 0 commits into from

Conversation

aarontrowbridge
Copy link
Contributor

This hopefully resolves issue #95, it doesnt handle super operators, but perhaps that can be added in the future on top of this.

@ytdHuang
Copy link
Member

ytdHuang commented May 31, 2024

Sorry that we have re-organized the source code structure yesterday. So there's a conflict compare to the main branch.
Could you put all the new functions you made in src/qobj/arithmetic_and_attributes.jl.

```

"""
function permute end
Copy link
Member

@ytdHuang ytdHuang May 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just directly put the definition of permute here, we can merge those three methods in just one line, namely

function permute(
    A::QuantumObject{<:AbstractArray{T},ObjType},
    order::AbstractVector{Int}
) where {T,ObjType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
    if length(order) != length(A.dims)
        throw(ArgumentError("The order vector must have the same length as the number of subsystems"))
    end
    perm = kron_permutation(order, A.dims)
    return QuantumObject(A.data[perm, perm], A.type, A.dims[order])
end

# helper functions
# -------------------------------------------------------------

function kron_rep(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest this to be an internal function, can you rename this as _kron_rep

return kron(xs...)
end

function kron_permutation(perm::Vector{Int}, dims::Vector{Int})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest this to be an internal function, can you rename this as _kron_permutation


@test permute(ket_abc, [2, 3, 1]) ≈ tensor(ket_b, ket_c, ket_a)
@test permute(bra_abc, [2, 3, 1]) ≈ tensor(bra_b, bra_c, bra_a)
@test permute(op_abc, [2, 3, 1]) ≈ tensor(op_b, op_c, op_a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add the tests for raising potential errors ?
For example:

permute(ket_abc, [1, 2])
permute(ket_abc, [4, 5, 6])

@@ -291,4 +291,27 @@
@test typeof(SparseMatrixCSC(Md).data) == SparseMatrixCSC{Int64,Int64}
@test typeof(SparseMatrixCSC(Ms).data) == SparseMatrixCSC{Int64,Int64}
@test typeof(SparseMatrixCSC{ComplexF64}(Ms).data) == SparseMatrixCSC{ComplexF64,Int64}

# tensor tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make the comment

# tensor and permute tests


function kron_rep(
dims;
labels="abcdefghijklmnopqrstuvwxyz"[1:length(dims)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a better way to define these labels for arbitrary length(dims)?
Because it seems that this doesn't work if the number of sub-systems is larger than 26 (a ~ z)

@ytdHuang ytdHuang linked an issue May 31, 2024 that may be closed by this pull request
@ytdHuang
Copy link
Member

Could you also put the docstring of permute in to the documentation API list (in the section Qobj arithmetic and attributes)


# create string representations of the subsystems
# and store them in the order of the permutation
xs = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should about to create empty lists of undefined types and then pushing to them. This is inefficient and allocates memory

if length(order) != length(op.dims)
throw(ArgumentError("The order vector must have the same length as the number of subsystems"))
end
perm = kron_permutation(order, op.dims)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t know how much inefficient is calling data[perm, perm], especially for sparse matrices

@albertomercurio
Copy link
Member

Hi @aarontrowbridge, thank you for this PR.

I’m not sure if this is the most efficient and simple way to permute the dimensions. I would suggest to use reshaping and PermuteDimsArray, like in the current ptrace implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support permute to re-order the tensor (Kronecker) product
3 participants