Skip to content

Commit

Permalink
Add // function for MatrixCoupling, MatrixCouplingProd and `Mat…
Browse files Browse the repository at this point in the history
…rixCouplingSum`.
  • Loading branch information
waltergu committed Oct 10, 2022
1 parent 58071b7 commit 1a1f249
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,9 @@ Get the nth power of a `MatrixCoupling`/`MatrixCouplingProd`/`MatrixCouplingSum`
/(mcp::MatrixCouplingProd, factor::Number) -> MatrixCouplingProd
/(mcs::MatrixCouplingSum, factor::Number) -> MatrixCouplingSum
/(mc::MatrixCoupling, factor::Number) -> MatrixCouplingProd
//(mcp::MatrixCouplingProd, factor::Number) -> MatrixCouplingProd
//(mcs::MatrixCouplingSum, factor::Number) -> MatrixCouplingSum
//(mc::MatrixCoupling, factor::Number) -> MatrixCouplingProd
-(mc::MatrixCoupling) -> MatrixCouplingProd
-(mcp::MatrixCouplingProd) -> MatrixCouplingProd
-(mcs::MatrixCouplingSum) -> MatrixCouplingSum
Expand All @@ -977,21 +980,22 @@ Get the nth power of a `MatrixCoupling`/`MatrixCouplingProd`/`MatrixCouplingSum`
-(mcs::MatrixCouplingSum, mc::Union{MatrixCoupling, MatrixCouplingProd}) -> MatrixCouplingSum
-(mcs₁::MatrixCouplingSum, mcs₂::MatrixCouplingSum) -> MatrixCouplingSum
Define right-division, minus, substraction operator.
Define right-division, minus and subtraction operator.
"""
@inline Base.:/(mcp::MatrixCouplingProd, factor::Number) = MatrixCouplingProd(mcp.value/factor, mcp.contents)
@inline Base.:/(mcs::MatrixCouplingSum, factor::Number) = MatrixCouplingSum(map(m->m/factor, mcs.contents))
@inline Base.:/(mc::MatrixCoupling, factor::Number) = MatrixCouplingProd(1/factor, mc)
@inline Base.://(mcp::MatrixCouplingProd, factor::Number) = MatrixCouplingProd(mcp.value//factor, mcp.contents)
@inline Base.://(mcs::MatrixCouplingSum, factor::Number) = MatrixCouplingSum(map(m->m//factor, mcs.contents))
@inline Base.://(mc::MatrixCoupling, factor::Number) = MatrixCouplingProd(1//factor, mc)
@inline Base.:-(mc::MatrixCoupling) = MatrixCouplingProd(-1, mc)
@inline Base.:-(mcp::MatrixCouplingProd) = MatrixCouplingProd(-1*mcp.value, mcp.contents)
@inline Base.:-(mcs::MatrixCouplingSum) = MatrixCouplingSum(map(m->-m, mcs.contents))

@inline Base.:-(mc₁::Union{MatrixCoupling, MatrixCouplingProd}, mc₂::Union{MatrixCoupling, MatrixCouplingProd}) = MatrixCouplingSum(mc₁, -mc₂)
@inline Base.:-(mc::Union{MatrixCoupling, MatrixCouplingProd}, mcs::MatrixCouplingSum) = MatrixCouplingSum(mc, map(m->-m, mcs.contents)...)
@inline Base.:-(mcs::MatrixCouplingSum, mc::Union{MatrixCoupling, MatrixCouplingProd}) = MatrixCouplingSum(mcs.contents..., -mc)
@inline Base.:-(mcs₁::MatrixCouplingSum, mcs₂::MatrixCouplingSum) = MatrixCouplingSum(mcs₁.contents..., map(m->-m, mcs₂.contents)...)


# Term
"""
TermFunction <: Function
Expand Down
6 changes: 3 additions & 3 deletions src/QuantumOperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,11 @@ Overloaded `/` between a quantum operator and a number.
@inline Base.:/(m::QuantumOperator, factor::Number) = m * (one(dtype(optype(m)))/factor)

"""
//(m::QuantumOperator, factor::Integer) -> QuantumOperator
//(m::QuantumOperator, factor::Number) -> QuantumOperator
Overloaded `//` between a quantum operator and an integer.
Overloaded `//` between a quantum operator and a number.
"""
@inline Base.://(m::QuantumOperator, factor::Integer) = m * (1//factor)
@inline Base.://(m::QuantumOperator, factor::Number) = m * (1//factor)

"""
^(m::QuantumOperator, n::Integer) -> QuantumOperator
Expand Down
3 changes: 3 additions & 0 deletions test/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ end
@test mc[2] == Coupling(+1, Index(:, DID(2)), Index(:, DID(1)))
@test mc^2 == mc*mc
@test mc/2 == mc*(1/2)
@test mc//2 == mc*(1//2)
@test -mc == (-1)*mc

another = MatrixCoupling((1, 2), DID, Component([:], [:], hcat(2.0)))
Expand All @@ -316,6 +317,7 @@ end
@test mcp*mcp == MatrixCouplingProd(mc, another, mc, another)
@test mcp^2 == mcp*mcp
@test mcp/2 == mcp*(1/2) == MatrixCouplingProd(1/2, mc, another)
@test mcp//2 == mcp*(1//2) == MatrixCouplingProd(1//2, mc, another)
@test -mcp == (-1)*mcp

mc₁ = MatrixCoupling((1, 2), DID, Component([1, 2], [2, 1], [0 1; 1 0]))
Expand All @@ -336,6 +338,7 @@ end
@test mcs*mcp == MatrixCouplingSum(mc₁*mc*another, mc₂*mc*another)
@test mcs^2 == mcs*mcs
@test mcs/2 == mcs*(1/2) == MatrixCouplingSum(mc₁/2, mc₂/2)
@test mcs//2 == mcs*(1//2) == MatrixCouplingSum(mc₁//2, mc₂//2)
@test -mcs == (-1)*mcs == MatrixCouplingSum(-mc₁, -mc₂)

mcs₂ = mc₁ - mc₂
Expand Down

0 comments on commit 1a1f249

Please sign in to comment.