From e7a48db78f97385bd6a769c9b3385e288b8d9406 Mon Sep 17 00:00:00 2001 From: Claude Code Date: Mon, 18 Aug 2025 22:44:56 +0000 Subject: [PATCH] Add commutator and anticommutator functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add commutator(A, B) function that computes [A, B] = AB - BA - Add anticommutator(A, B) function that computes {A, B} = AB + BA - These are fundamental quantum mechanical operations for operator algebra - Includes comprehensive documentation with examples 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/QuantumInterface.jl | 14 ++++++++++++++ src/linalg.jl | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/src/QuantumInterface.jl b/src/QuantumInterface.jl index 4af5dd7..a090ac5 100644 --- a/src/QuantumInterface.jl +++ b/src/QuantumInterface.jl @@ -53,6 +53,20 @@ function traceout! end function variance end +""" + commutator(A, B) + +Compute the commutator [A, B] = AB - BA of two operators. +""" +function commutator end + +""" + anticommutator(A, B) + +Compute the anticommutator {A, B} = AB + BA of two operators. +""" +function anticommutator end + ## # Qubit specific ## diff --git a/src/linalg.jl b/src/linalg.jl index 71833e3..10ad846 100644 --- a/src/linalg.jl +++ b/src/linalg.jl @@ -8,3 +8,46 @@ directsum(a::AbstractOperator...) = reduce(directsum, a) ptrace(a::AbstractOperator, index) = arithmetic_unary_error("Partial trace", a) _index_complement(b::CompositeBasis, indices) = complement(length(b.bases), indices) reduced(a, indices) = ptrace(a, _index_complement(basis(a), indices)) + +# Commutator and anticommutator operations +""" + commutator(A, B) + +Compute the commutator [A, B] = AB - BA of two operators. + +# Arguments +- `A::AbstractOperator`: First operator +- `B::AbstractOperator`: Second operator + +# Returns +The commutator [A, B] = AB - BA + +# Examples +```julia +σx = sigmax() +σy = sigmay() +comm = commutator(σx, σy) # Returns 2i*σz +``` +""" +commutator(A::AbstractOperator, B::AbstractOperator) = A*B - B*A + +""" + anticommutator(A, B) + +Compute the anticommutator {A, B} = AB + BA of two operators. + +# Arguments +- `A::AbstractOperator`: First operator +- `B::AbstractOperator`: Second operator + +# Returns +The anticommutator {A, B} = AB + BA + +# Examples +```julia +σx = sigmax() +σz = sigmaz() +anticomm = anticommutator(σx, σz) # Returns zero for Pauli matrices +``` +""" +anticommutator(A::AbstractOperator, B::AbstractOperator) = A*B + B*A