Getting Started with QuantumSymbolics.jl
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
, SKet
, SOperator
, and SSuperOperator
represent symbolic quantum objects with name
and basis
properties. Each type can be generated with a straightforward macro:
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:
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:
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:
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:
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:
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:
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
, and similar objects SZeroBra
and SZeroKet
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:
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
function:
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
, - anticommutator of two operators:
anticommutator
, - complex conjugate:
conj
, - transpose:
transpose
, - projection of a ket:
projector
, - adjoint or dagger:
dagger
, - trace:
tr
, - partial trace:
ptrace
, - inverse of an operator:
inv
, - exponential of an operator:
exp
, - vectorization of an operator:
vec
.
Predefined Quantum Objects
So far in this tutorial, we have considered arbitrary kets, bras, operators, and their corresponding operations. This package supports predefined quantum objects and operations in several formalisms, which are discussed in detail in other sections (see, for example, the quantum harmonic oscillators or qubit basis pages). To get a taste of what's available, let us consider a few symbolic examples. For a complete description, see the full API page.
Quantum gates and their basis states can be represented symbolically:
julia> CNOT # CNOT Gate
CNOT
julia> X, Y, Z, I # Pauli operators
(X, Y, Z, 𝕀)
julia> X1, X2 # Eigenstates of the Pauli X operator
(|X₁⟩, |X₂⟩)
julia> CPHASE * (Z1 ⊗ Z2) # Application of CPHASE gate on |01⟩
CPHASE|Z₁⟩|Z₂⟩
We also have symbolic representations of bosonic systems:
julia> FockState(4) # Fock state with 4 excitation quanta
|4⟩
julia> Create, Destroy # creation and annihilation operators
(a†, a)
julia> DisplaceOp(im) # Displacement operator for single bosonic mode
D(im)
julia> N * vac # Application of number operator on vacuum state
n|0⟩
If we want to substitute a predefined quantum object into a general symbolic expression, we can use the substitute
command from Symbolics.jl
:
julia> using Symbolics
julia> @op A; @ket k;
julia> ex = 2*A + projector(k)
(2A+𝐏[|k⟩])
julia> substitute(ex, Dict([A => X, k => X1]))
(2X+𝐏[|X₁⟩])
Simplifying Expressions
For predefined objects such as the Pauli operators X
, Y
, and Z
, additional simplification can be performed with the qsimplify
function. Take the following example:
julia> qsimplify(X*Z)
(0 - 1im)Y
Here, we have the relation $XZ = -iY$, so calling qsimplify
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
. By default, when called on an expression, qsimplify
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
, as done in the following example:
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
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
:
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
function. We demonstrate this capability with the following examples.
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
. Numerics packages supported by this translation capability are QuantumOptics.jl
and QuantumClifford.jl
.
By default, express
converts an object to the quantum optics state vector representation. For instance, we can represent the exponential of the Pauli operator X
numerically as follows:
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
. For instance, we can represent the projection of the basis state X1
of the Pauli operator X
as follows:
julia> using QuantumClifford
julia> express(projector(X1), CliffordRepr())
𝒟ℯ𝓈𝓉𝒶𝒷
+ Z
𝒮𝓉𝒶𝒷
+ X
For more details on using express
, refer to the express functionality page.