Skip to content

feat: add QuantumOpticsRepr(lazy=true) support for LazyOperator output - #197

Open
ibishal wants to merge 1 commit into
QuantumSavory:mainfrom
ibishal:lazy-QuantumOpticsRepr-express
Open

feat: add QuantumOpticsRepr(lazy=true) support for LazyOperator output#197
ibishal wants to merge 1 commit into
QuantumSavory:mainfrom
ibishal:lazy-QuantumOpticsRepr-express

Conversation

@ibishal

@ibishal ibishal commented Jun 4, 2026

Copy link
Copy Markdown

adds express_nolookup methods for SAdd, SMulOperator, and STensor that return LazySum, LazyProduct, and LazyTensor when r.lazy is true. when its false, falls back to the existing eager path. includes tests and a short docs example. depends on qojulia/QuantumInterface.jl#80

before this, express(op, QuantumOpticsRepr()) would produce an eager Operator. now it produces lazy types:

using Test
using QuantumSymbolics
using QuantumOptics

r_lazy = QuantumOpticsRepr(lazy=true)

op = tensor(X, I) + tensor(I, Z)

eager_op = express(op, QuantumOpticsRepr())
lazy_op  = express(op, r_lazy)

println("lazy_op type:  ", typeof(lazy_op))
@test lazy_op isa LazySum
@test isapprox(dense(lazy_op), eager_op)
println("sum tests passed")

prod_op  = tensor(X, I) * tensor(I, Z)
lazy_prod = express(prod_op, r_lazy)

println("lazy_prod type: ", typeof(lazy_prod))
@test lazy_prod isa LazyProduct
@test isapprox(dense(lazy_prod), express(prod_op, QuantumOpticsRepr()))
println("product tests passed")

it produces

lazy_op type:  LazySum{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, Vector{Any}, Tuple{LazyTensor{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, ComplexF64, Vector{Int64}, Tuple{Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, LinearAlgebra.Diagonal{ComplexF64, FillArrays.Ones{ComplexF64, 1, Tuple{Base.OneTo{Int64}}}}}, Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, SparseArrays.SparseMatrixCSC{ComplexF64, Int64}}}}, LazyTensor{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, ComplexF64, Vector{Int64}, Tuple{Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, SparseArrays.SparseMatrixCSC{ComplexF64, Int64}}, Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, LinearAlgebra.Diagonal{ComplexF64, FillArrays.Ones{ComplexF64, 1, Tuple{Base.OneTo{Int64}}}}}}}}}
sum tests passed
lazy_prod type: LazyProduct{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, Int64, Tuple{LazyTensor{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, ComplexF64, Vector{Int64}, Tuple{Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, SparseArrays.SparseMatrixCSC{ComplexF64, Int64}}, Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, LinearAlgebra.Diagonal{ComplexF64, FillArrays.Ones{ComplexF64, 1, Tuple{Base.OneTo{Int64}}}}}}}, LazyTensor{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, ComplexF64, Vector{Int64}, Tuple{Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, LinearAlgebra.Diagonal{ComplexF64, FillArrays.Ones{ComplexF64, 1, Tuple{Base.OneTo{Int64}}}}}, Operator{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}, SparseArrays.SparseMatrixCSC{ComplexF64, Int64}}}}}, Tuple{Ket{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, Vector{ComplexF64}}}, Tuple{Bra{CompositeBasis{Vector{Int64}, Tuple{SpinBasis{1//2, Int64}, SpinBasis{1//2, Int64}}}, Vector{ComplexF64}}}}
product tests passed

Before considering your pull request ready for review and merging, make sure that all of the following are completed (please keep the clecklist as part of your PR):

  • The code is properly formatted and commented.
  • Substantial new functionality is documented within the docs.
  • All new functionality is tested.
  • All of the automated tests on github pass.
  • We recently started enforcing formatting checks. If formatting issues are reported in the new code you have written, please correct them. There will be plenty of old code that is flagged as we are slowly transitioning to enforced formatting. Please do not worry about or address older formatting issues -- keep your PR just focused on your planned contribution.

If you are submitting for a bug bounty:

Part of qojulia/QuantumOptics.jl#521

@ibishal

ibishal commented Jun 4, 2026

Copy link
Copy Markdown
Author

the changelog i have pasted here to avoid merge conflicts

  • Add lazy=true option to QuantumOpticsRepr. When set, express returns LazySum, LazyProduct, or LazyTensor for symbolic operator sums, products, and tensor products instead of materializing full matrices.

@ibishal

ibishal commented Jun 9, 2026

Copy link
Copy Markdown
Author

@Krastanov can i get a review on this ?

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.

1 participant