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

Migrate express functionality and AbstractRepresentation type to QuantumInterface #93

Merged
merged 6 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## v0.4.6 - 2025-01-18

- Migrate `express` functionality and representation types to QuantumInterface.

## v0.4.5 - 2024-11-14

- Updated compat lower bounds for Symbolics to v6 (and for SymbolicUtils and TermInterface)
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumSymbolics"
uuid = "efa7fd63-0460-4890-beb7-be1bbdfbaeae"
authors = ["QuantumSymbolics.jl contributors"]
version = "0.4.5"
version = "0.4.6"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand All @@ -28,7 +28,7 @@ LinearAlgebra = "1.9"
MacroTools = "0.5.13"
PrecompileTools = "1.2"
QuantumClifford = "0.8.19, 0.9"
QuantumInterface = "0.3.3"
QuantumInterface = "0.3.7"
QuantumOpticsBase = "0.4.22, 0.5"
SymbolicUtils = "3.7"
Symbolics = "6"
Expand Down
11 changes: 3 additions & 8 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import QuantumInterface:
basis,Basis,samebases,IncompatibleBases,SpinBasis,FockBasis,CompositeBasis,
nqubits,
projector,dagger,tr,ptrace,
AbstractBra,AbstractKet,AbstractOperator,AbstractSuperOperator
AbstractBra,AbstractKet,AbstractOperator,AbstractSuperOperator,
express,AbstractRepresentation,AbstractUse,UseAsState,UseAsObservable,UseAsOperation,
QuantumOpticsRepr,QuantumMCRepr,CliffordRepr

export SymQObj,QObj,
AbstractRepresentation,AbstractUse,
Expand Down Expand Up @@ -53,13 +55,6 @@ export SymQObj,QObj,
# Metadata cache helpers
##

"""An abstract type for the supported representation of quantum objects."""
abstract type AbstractRepresentation end
abstract type AbstractUse end
struct UseAsState <: AbstractUse end
struct UseAsOperation <: AbstractUse end
struct UseAsObservable <: AbstractUse end

const CacheType = Dict{Tuple{<:AbstractRepresentation,<:AbstractUse},Any}
mutable struct Metadata
express_cache::CacheType # TODO use more efficient mapping
Expand Down
26 changes: 2 additions & 24 deletions src/QSymbolicsBase/express.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SymbolicUtils: Symbolic
"""
express(s, repr::AbstractRepresentation=QuantumOpticsRepr()[, use::AbstractUse])

The main interface for expressing quantum objects in various representations.
The main interface for expressing symbolic quantum objects in various representations.

```jldoctest
julia> express(X1)
Expand All @@ -37,8 +37,6 @@ julia> express(QuantumSymbolics.X, CliffordRepr(), UseAsObservable())
+ X
```
"""
function express end
apkille marked this conversation as resolved.
Show resolved Hide resolved

function express(state::Symbolic, repr::AbstractRepresentation, use::AbstractUse)
md = metadata(state)
isnothing(md) && return express_from_cache(express_nolookup(state, repr, use))
Expand All @@ -51,10 +49,6 @@ function express(state::Symbolic, repr::AbstractRepresentation, use::AbstractUse
end
end

express(s::Number, repr::AbstractRepresentation, use::AbstractUse) = s

express(s, repr::AbstractRepresentation) = express(s, repr, UseAsState())

express_nolookup(x, repr::AbstractRepresentation, ::AbstractUse) = express_nolookup(x, repr)

express_nolookup(x, repr::AbstractRepresentation, ::UseAsState) = express_nolookup(x, repr)
Expand All @@ -72,28 +66,12 @@ function consistent_representation(reprs,state)
end
first(reprs)
end

##
# Commonly used representations -- interfaces for each one defined in separate packages
##

"""Representation using kets, bras, density matrices, and superoperators governed by `QuantumOptics.jl`."""
@kwdef struct QuantumOpticsRepr <: AbstractRepresentation
cutoff::Int = 2
end
"""Similar to `QuantumOpticsRepr`, but using trajectories instead of superoperators."""
struct QuantumMCRepr <: AbstractRepresentation end
"""Representation using tableaux governed by `QuantumClifford.jl`"""
struct CliffordRepr <: AbstractRepresentation end

express(state::Symbolic) = express(state, QuantumOpticsRepr()) # The default representation
express_nolookup(state, ::QuantumMCRepr) = express_nolookup(state, QuantumOpticsRepr())
express(state) = state

function express_nolookup(s, repr::AbstractRepresentation)
if isexpr(s)
operation(s)(express.(arguments(s), (repr,))...)
else
error("Encountered an object $(s) of type $(typeof(s)) that can not be converted to $(repr) representation") # TODO make a nice error type
end
end
end
4 changes: 2 additions & 2 deletions test/test_aqua.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# Add any new types needed to QObj, or here if QObj if not appropriate.
# Add types from elsewhere in the ecosystem here or preferably to QObj
own_types = [Base.uniontypes(QObj)...,]
own_types_union = Union{SymQObj,}
own_types = [Base.uniontypes(QObj)...,AbstractRepresentation,AbstractUse,]
own_types_union = Union{SymQObj,AbstractRepresentation,AbstractUse,}

Aqua.test_all(QuantumSymbolics, piracies=(;treat_as_own=own_types))

Expand Down
Loading