-
Notifications
You must be signed in to change notification settings - Fork 28
adding permute function for reordering tensor products of quantum objects #151
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
Conversation
Sorry that we have re-organized the source code structure yesterday. So there's a conflict compare to the main branch. |
src/quantum_object.jl
Outdated
``` | ||
|
||
""" | ||
function permute end |
There was a problem hiding this comment.
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
src/quantum_object.jl
Outdated
# helper functions | ||
# ------------------------------------------------------------- | ||
|
||
function kron_rep( |
There was a problem hiding this comment.
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
src/quantum_object.jl
Outdated
return kron(xs...) | ||
end | ||
|
||
function kron_permutation(perm::Vector{Int}, dims::Vector{Int}) |
There was a problem hiding this comment.
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/quantum_objects.jl
Outdated
|
||
@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) |
There was a problem hiding this comment.
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])
test/quantum_objects.jl
Outdated
@@ -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 |
There was a problem hiding this comment.
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
src/quantum_object.jl
Outdated
|
||
function kron_rep( | ||
dims; | ||
labels="abcdefghijklmnopqrstuvwxyz"[1:length(dims)] |
There was a problem hiding this comment.
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
)
Could you also put the docstring of |
src/quantum_object.jl
Outdated
|
||
# create string representations of the subsystems | ||
# and store them in the order of the permutation | ||
xs = [] |
There was a problem hiding this comment.
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
src/quantum_object.jl
Outdated
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) |
There was a problem hiding this comment.
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
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 |
This hopefully resolves issue #95, it doesnt handle super operators, but perhaps that can be added in the future on top of this.