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

Generalized CTranspose + conversion methods and promotion rules #33

Merged
merged 9 commits into from
May 20, 2015

Conversation

jrevels
Copy link
Contributor

@jrevels jrevels commented May 14, 2015

This PR generalizes the CTranspose type to store Q<:AbstractQuArray and supplies the appropriate promotion rules and conversion methods. Once this is merged, we can close #31.

The only thing left before merging this in (unless something needs to be altered or I missed something) is to add some tests.

(I also snuck in a generalization of LabeledQuArray, a type alias that probably won't see much love for a while but for which I have future plans).

This also makes it simpler to call similar_type by adding a method that auto-extracts the type of a value argument (and promotes the types of two value arguments).

@acroy
Copy link
Contributor

acroy commented May 14, 2015

Looks good. I really like the new similar_type! I will try to play with it tomorrow. Any idea how we should test these things?


function tensor{B<:OrthonormalBasis}(ket::QuVector{B}, bra::DualVector{B})
function tensor{B}(ket::AbstractQuVector{B}, bra::DualVector{B})
return QuArray(kron(coeffs(ket), coeffs(bra)),
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this one you forgot?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I fixed it, and in the process of doing so uncovered an issue.

I found out that n-ary calls of tensor weren't dispatching properly because of the restriction that all arguments share a basis type. Since we parameterize the number of factors in our basis types, computing things like the following wouldn't work:

a = QuArray(rand(Complex{Float64},4), FiniteBasis(2,2))
b = QuArray(rand(Complex{Float64},1), FiniteBasis(1,1))
c = QuArray(rand(Complex{Float64},3))

ab = tensor(a,b)
tensor(ab, c) # fails, ab has a different basis type than c since the number of factors are different

After removing the basis type restriction, I added a test similar to the above. It's currently failing because type promotion for bases of different factor length is ambiguous:

julia> promote_type(typeof(ab), typeof(c))
ERROR: type: QuArray: in B, expected B<:AbstractBasis{S<:AbstractStructure}, got Type{FiniteBasis{S,N}}
 in promote_type at promotion.jl:110

I don't know what the right decision is here...perhaps we should consider abandon parameterizing factor length for basis types? Will any functions in the future require factor length as a parameter for type stability? Otherwise, we can just pull it from instances using the nfactors function (requiring it to be defined for all B<:AbstractBasis), and treat factor length as a value.

Copy link
Contributor

Choose a reason for hiding this comment

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

It feels like we had this discussion already? Anyways, at least at the moment we don't have anything that needs the factor length as a parameter, so we can just treat it as a value for now and switch back if necessary :-) I think when we introduced this I had in mind that doing partial traces/transpose could profit from it, but IMO this is to vague to stop this PR.

@jrevels
Copy link
Contributor Author

jrevels commented May 20, 2015

I added a bunch of unit tests for the QuArray and CTranspose types.

This PR should be ready to merge, given that the tests pass (and I didn't miss anything).

end

tensor{B<:OrthonormalBasis}(bra::DualVector{B}, ket::QuVector{B}) = tensor(ket, bra)
tensor(bra::DualVector, ket::AbstractQuVector) = tensor(ket, bra)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am probably just missing it, but does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it's a matter of intent...I mainly defined that thinking that if one takes the tensor product of Bra and a Ket, they would expect the outer product of the states? For example, isn't the following is an accepted use of the ⊗ notation:

⟨ ψ | ⊗ | ϕ ⟩ = | ϕ ⟩ ⊗ ⟨ ψ | = | ϕ ⟩⟨ ψ |

Not a peer-reviewed publication, obviously, but: http://physics.stackexchange.com/questions/134575/tensor-product-of-a-bra-and-a-ket

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Never thought about it that way, but it makes perfect sense of course!

@acroy
Copy link
Contributor

acroy commented May 20, 2015

This is really good stuff! Let's see what Travis says ...

Maybe it is time to include code coverage via Coverage.jl (in a separate PR of course)?

@jrevels
Copy link
Contributor Author

jrevels commented May 20, 2015

Definitely agree!

@jrevels
Copy link
Contributor Author

jrevels commented May 20, 2015

Seems like things passed!

Let me know when you're done reviewing so I can merge it (or you can merge it when you're done, if you want).

@acroy
Copy link
Contributor

acroy commented May 20, 2015

I am done - you can merge.

jrevels added a commit that referenced this pull request May 20, 2015
Generalized CTranspose + conversion methods and promotion rules. Closes #31
@jrevels jrevels merged commit 0a40866 into master May 20, 2015
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.

Generalize CTranspose to work for Q<:AbstractQuArray
2 participants