Skip to content

Commit

Permalink
add linear algebra and documentation (#61)
Browse files Browse the repository at this point in the history
* add linalg and docs

* remove space

* doc fix

* typo fix in docs

* Update docs/src/introduction.md

Co-authored-by: Stefan Krastanov <[email protected]>

* updating docs to support autogenerated API

* superop rearranging

* intro.md change

* update changelog and project.toml

---------

Co-authored-by: Stefan Krastanov <[email protected]>
  • Loading branch information
apkille and Krastanov authored Jul 22, 2024
1 parent 68fbe00 commit 4d28a97
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 159 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# News

## v0.3.4 - dev
## v0.3.4 - 2024-07-22

- Added `tr` and `ptrace` functionalities.
- New symbolic superoperator representations.
- Added linear algebra operations `exp`, `vec`, `conj`, `transpose`.
- Created a getting-started guide in docs.

## v0.3.3 - 2024-07-12

Expand Down
2 changes: 1 addition & 1 deletion 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.3.4-dev"
version = "0.3.4"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand Down
6 changes: 3 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ push!(LOAD_PATH,"../src/")
using Documenter
using DocumenterCitations
using QuantumSymbolics
using QuantumOptics
using QuantumClifford
using QuantumInterface

DocMeta.setdocmeta!(QuantumSymbolics, :DocTestSetup, :(using QuantumSymbolics, QuantumOptics, QuantumClifford); recursive=true)

Expand All @@ -25,8 +24,9 @@ function main()
authors = "Stefan Krastanov",
pages = [
"QuantumSymbolics.jl" => "index.md",
"Qubit Basis Choice" => "qubit_basis.md",
"Getting Started with QuantumSymbolics.jl" => "introduction.md",
"Express Functionality" => "express.md",
"Qubit Basis Choice" => "qubit_basis.md",
"API" => "API.md",
]
)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ express(MixedState(X1)/2+SProjector(Z1)/2, CliffordRepr())

!!! warning "Stabilizer state expressions"

The state written as $\frac{|Z₁⟩⊗|Z₁⟩+|Z₂⟩⊗|Z₂⟩}{√2}$ is a well known stabilizer state, namely a Bell state. However, automatically expressing it as a stabilizer is a prohibitively expensive computational operation in general. We do not perform that computation automatically. If you want to ensure that states you define can be automatically converted to tableaux for Clifford simulations, avoid using summation of kets. On the other hand, in all of our Clifford Monte-Carlo simulations, `⊗` is fully supported, as well as [`SProjector`](@ref), [`MixedState`](@ref), [`StabilizerState`](@ref), and summation of density matrices.
The state written as $\frac{|Z₁⟩⊗|Z₁⟩+|Z₂⟩⊗|Z₂⟩}{√2}$ is a well known stabilizer state, namely a Bell state. However, automatically expressing it as a stabilizer is a prohibitively expensive computational operation in general. We do not perform that computation automatically. If you want to ensure that states you define can be automatically converted to tableaux for Clifford simulations, avoid using summation of kets. On the other hand, in all of our Clifford Monte-Carlo simulations, `⊗` is fully supported, as well as [`projector`](@ref), [`MixedState`](@ref), [`StabilizerState`](@ref), and summation of density matrices.
251 changes: 251 additions & 0 deletions docs/src/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
# Getting Started with QuantumSymbolics.jl

```@meta
DocTestSetup = quote
using QuantumSymbolics
end
```

QuantumSymbolics is designed for manipulation and numerical translation of symbolic quantum objects. This tutorial introduces basic features of the package.

## Installation

QuantumSymbolics.jl can be installed through the Julia package system in the standard way:

```
using Pkg
Pkg.add("QuantumSymbolics")
```

## Literal Symbolic Quantum Objects

Basic objects of type [`SBra`](@ref), [`SKet`](@ref), [`SOperator`](@ref), and [`SSuperOperator`](@ref) represent symbolic quantum objects with `name` and `basis` properties. Each type can be generated with a straightforward macro:

```jldoctest
julia> using QuantumSymbolics
julia> @bra b # object of type SBra
⟨b|
julia> @ket k # object of type SKet
|k⟩
julia> @op A # object of type SOperator
A
julia> @superop S # object of type SSuperOperator
S
```

By default, each of the above macros defines a symbolic quantum object in the spin-1/2 basis. One can simply choose a different basis, such as the Fock basis or a tensor product of several bases, by passing an object of type `Basis` to the second argument in the macro call:

```jldoctest
julia> @op B FockBasis(Inf, 0.0)
B
julia> basis(B)
Fock(cutoff=Inf)
julia> @op C SpinBasis(1//2)⊗SpinBasis(5//2)
C
julia> basis(C)
[Spin(1/2) ⊗ Spin(5/2)]
```
Here, we extracted the basis of the defined symbolic operators using the `basis` function.

Symbolic quantum objects with additional properties can be defined, such as a Hermitian operator, or the zero ket (i.e., a symbolic ket equivalent to the zero vector $\bm{0}$).

## Basic Operations

Expressions containing symbolic quantum objects can be built with a variety of functions. Let us consider the most fundamental operations: multiplication `*`, addition `+`, and the tensor product ``.

We can multiply, for example, a ket by a scalar value, or apply an operator to a ket:

```jldoctest
julia> @ket k; @op A;
julia> 2*k
2|k⟩
julia> A*k
A|k⟩
```

Similar scaling procedures can be performed on bras and operators. Addition between symbolic objects is also available, for instance:

```jldoctest
julia> @op A₁; @op A₂;
julia> A₁+A₂
(A₁+A₂)
julia> @bra b;
julia> 2*b + 5*b
7⟨b|
```
Built into the package are straightforward automatic simplification rules, as shown in the last example, where `2⟨b|+5⟨b|` evaluates to `7⟨b|`.

Tensor products of symbolic objects can be performed, with basis information transferred:

```jldoctest
julia> @ket k₁; @ket k₂;
julia> tp = k₁⊗k₂
|k₁⟩|k₂⟩
julia> basis(tp)
[Spin(1/2) ⊗ Spin(1/2)]
```

Inner and outer products of bras and kets can be generated:

```jldoctest
julia> @bra b; @ket k;
julia> b*k
⟨b||k⟩
julia> k*b
|k⟩⟨b|
```

More involved combinations of operations can be explored. Here are few other straightforward examples:

```jldoctest
julia> @bra b; @ket k; @op A; @op B;
julia> 3*A*B*k
3AB|k⟩
julia> A⊗(k*b + B)
(A⊗(B+|k⟩⟨b|))
julia> A-A
𝟎
```
In the last example, a zero operator, denoted `𝟎`, was returned by subtracting a symbolic operator from itself. Such an object is of the type [`SZeroOperator`](@ref), and similar objects [`SZeroBra`](@ref) and [`SZeroKet`](@ref) correspond to zero bras and zero kets, respectively.

## Linear Algebra on Bras, Kets, and Operators

QuantumSymbolics supports a wide variety of linear algebra on symbolic bras, kets, and operators. For instance, the commutator and anticommutator of two operators, can be generated:

```jldoctest
julia> @op A; @op B;
julia> commutator(A, B)
[A,B]
julia> anticommutator(A, B)
{A,B}
julia> commutator(A, A)
𝟎
```
Or, one can take the dagger of a quantum object with the [`dagger`](@ref) function:

```jldoctest
julia> @ket k; @op A; @op B;
julia> dagger(A)
A†
julia> dagger(A*k)
|k⟩†A†
julia> dagger(A*B)
B†A†
```
Below, we state all of the supported linear algebra operations on quantum objects:

- commutator of two operators: [`commutator`](@ref),
- anticommutator of two operators: [`anticommutator`](@ref),
- complex conjugate: [`conj`](@ref),
- transpose: [`transpose`](@ref),
- projection of a ket: [`projector`](@ref),
- adjoint or dagger: [`dagger`](@ref),
- trace: [`tr`](@ref),
- partial trace: [`ptrace`](@ref),
- inverse of an operator: [`inv`](@ref),
- exponential of an operator: [`exp`](@ref),
- vectorization of an operator: [`vec`](@ref).

## Simplifying Expressions

For predefined objects such as the Pauli operators [`X`](@ref), [`Y`](@ref), and [`Z`](@ref), additional simplification can be performed with the [`qsimplify`](@ref) function. Take the following example:

```jldoctest
julia> qsimplify(X*Z)
(0 - 1im)Y
```

Here, we have the relation $XZ = -iY$, so calling [`qsimplify`](@ref) on the expression `X*Z` will rewrite the expression as `-im*Y`.

Note that simplification rewriters used in QuantumSymbolics are built from the interface of [`SymbolicUtils.jl`](https://github.com/JuliaSymbolics/SymbolicUtils.jl). By default, when called on an expression, [`qsimplify`](@ref) will iterate through every defined simplification rule in the QuantumSymbolics package until the expression can no longer be simplified.

Now, suppose we only want to use a specific subset of rules. For instance, say we wish to simplify commutators, but not anticommutators. Then, we can pass the keyword argument `rewriter=qsimplify_commutator` to [`qsimplify`](@ref), as done in the following example:

```jldoctest
julia> qsimplify(commutator(X, Y), rewriter=qsimplify_commutator)
(0 + 2im)Z
julia> qsimplify(anticommutator(X, Y), rewriter=qsimplify_commutator)
{X,Y}
```
As shown above, we apply [`qsimplify`](@ref) to two expressions: `commutator(X, Y)` and `anticommutator(X, Y)`. We specify that only commutator rules will be applied, thus the first expression is rewritten to `(0 + 2im)Z` while the second expression is simply returned. This feature can greatly reduce the time it takes for an expression to be simplified.

Below, we state all of the simplification rule subsets that can be passed to [`qsimplify`](@ref):

- `qsimplify_pauli` for Pauli multiplication,
- `qsimplify_commutator` for commutators of Pauli operators,
- `qsimplify_anticommutator` for anticommutators of Pauli operators.

## Expanding Expressions

Symbolic expressions containing quantum objects can be expanded with the [`qexpand`](@ref) function. We demonstrate this capability with the following examples.

```jldoctest
julia> @op A; @op B; @op C;
julia> qexpand(A⊗(B+C))
((A⊗B)+(A⊗C))
julia> qexpand((B+C)*A)
(BA+CA)
julia> @ket k₁; @ket k₂; @ket k₃;
julia> qexpand(k₁⊗(k₂+k₃))
(|k₁⟩|k₂⟩+|k₁⟩|k₃⟩)
julia> qexpand((A*B)*(k₁+k₂))
(AB|k₁⟩+AB|k₂⟩)
```

## Numerical Translation of Symbolic Objects

Symbolic expressions containing predefined objects can be converted to numerical representations with [`express`](@ref). Numerics packages supported by this translation capability are [`QuantumOptics.jl`](https://github.com/qojulia/QuantumOptics.jl) and [`QuantumClifford.jl`](https://github.com/QuantumSavory/QuantumClifford.jl/).

By default, [`express`](@ref) converts an object to the quantum optics state vector representation. For instance, we can represent the exponential of the Pauli operator [`X`](@ref) numerically as follows:

```jldoctest
julia> using QuantumOptics
julia> express(exp(X))
Operator(dim=2x2)
basis: Spin(1/2)sparse([1, 2, 1, 2], [1, 1, 2, 2], ComplexF64[1.5430806327160496 + 0.0im, 1.1752011684303352 + 0.0im, 1.1752011684303352 + 0.0im, 1.5430806327160496 + 0.0im], 2, 2)
```

To convert to the Clifford representation, an instance of `CliffordRepr` must be passed to [`express`](@ref). For instance, we can represent the projection of the basis state [`X1`](@ref) of the Pauli operator [`X`](@ref) as follows:

```jldoctest
julia> using QuantumClifford
julia> express(projector(X1), CliffordRepr())
𝒟ℯ𝓈𝓉𝒶𝒷
+ Z
𝒮𝓉𝒶𝒷
+ X
```
For more details on using [`express`](@ref), refer to the [express functionality page](@ref express).
6 changes: 3 additions & 3 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using TermInterface
import TermInterface: isexpr,head,iscall,children,operation,arguments,metadata,maketerm

using LinearAlgebra
import LinearAlgebra: eigvecs,ishermitian,inv
import LinearAlgebra: eigvecs,ishermitian,conj,transpose,inv,exp,vec,tr

import QuantumInterface:
apply!,
Expand All @@ -23,7 +23,7 @@ export SymQObj,QObj,
apply!,
express,
tensor,,
dagger,projector,commutator,anticommutator,tr,ptrace,
dagger,projector,commutator,anticommutator,conj,transpose,inv,exp,vec,tr,ptrace,
I,X,Y,Z,σˣ,σʸ,σᶻ,Pm,Pp,σ₋,σ₊,
H,CNOT,CPHASE,XCX,XCY,XCZ,YCX,YCY,YCZ,ZCX,ZCY,ZCZ,
X1,X2,Y1,Y2,Z1,Z2,X₁,X₂,Y₁,Y₂,Z₁,Z₂,L0,L1,Lp,Lm,Lpi,Lmi,L₀,L₁,L₊,L₋,L₊ᵢ,L₋ᵢ,
Expand All @@ -35,8 +35,8 @@ export SymQObj,QObj,
SScaled,SScaledBra,SScaledOperator,SScaledKet,
STensorBra,STensorKet,STensorOperator,
SZeroBra,SZeroKet,SZeroOperator,
SConjugate,STranspose,SProjector,SDagger,SInvOperator,SExpOperator,SVec,STrace,SPartialTrace,
MixedState,IdentityOp,
SProjector,SDagger,STrace,SPartialTrace,SInvOperator,
SApplyKet,SApplyBra,SMulOperator,SSuperOpApply,SCommutator,SAnticommutator,SBraKet,SOuterKetBra,
HGate,XGate,YGate,ZGate,CPHASEGate,CNOTGate,
XBasisState,YBasisState,ZBasisState,
Expand Down
Loading

2 comments on commit 4d28a97

@apkille
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/111548

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.4 -m "<description of version>" 4d28a9797b35b7a6aad50ba730ef2eb6adbf02ad
git push origin v0.3.4

Please sign in to comment.