In [1]:
versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU           E5645  @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, westmere)

DiracNotation.jl sample

By using this package, matrix representation is changed into Dirac notation.

Basic usage

In order to change display style, all you have to do is import this package.

In [2]:
using DiracNotation, LinearAlgebra, Random; Random.seed!(0);
In [3]:
ket = normalize(rand(Complex{Float64}, 4)); bra = ket';
In [4]:
dirac(ket)

$|\psi\rangle = (0.633332+0.700005im)|00\rangle+(0.12654+0.136354im)|01\rangle+(0.214441+0.15646im)|10\rangle+(0.0325272+0.0524946im)|11\rangle $

In [5]:
dirac(bra)

$\langle\psi| = (0.633332-0.700005im)\langle00|+(0.12654-0.136354im)\langle01|+(0.214441-0.15646im)\langle10|+(0.0325272-0.0524946im)\langle11| $

In [6]:
dirac(ket)

$|\psi\rangle = (0.633332+0.700005im)|00\rangle+(0.12654+0.136354im)|01\rangle+(0.214441+0.15646im)|10\rangle+(0.0325272+0.0524946im)|11\rangle $

In [7]:
op = rand(2,2);
dirac(op)

$\hat{\rho} = 0.361828|0\rangle\langle0|+0.585812|0\rangle\langle1|+0.973216|1\rangle\langle0|+0.539289|1\rangle\langle1| $

Qudit system and Operator

7 dimensional state

$| \psi \rangle \in \mathbb{C}^7$

In [8]:
dim = 7
ket = normalize(rand(dim))
dirac(ket, [dim])

$|\psi\rangle = 0.141535|0\rangle+0.495329|1\rangle+0.0909162|2\rangle+0.356754|3\rangle+0.31345|4\rangle+0.472596|5\rangle+0.526764|6\rangle $

qubit-qutrit

$| \psi \rangle \in \mathbb{C}^2 \otimes \mathbb{C}^3$

In [9]:
dims = [2,3]
ket = normalize(randn(prod(dims)))
dirac(ket, dims)

$|\psi\rangle = 0.163106|00\rangle-0.0869658|01\rangle-0.446385|02\rangle+0.848054|10\rangle-0.0657909|11\rangle+0.207483|12\rangle $

Operator

$A: V \rightarrow W$ where $V = \mathbb{C}^2$ and $W = \mathbb{C}^3$

In [10]:
A = rand(3,2)
dirac(A, [3], [2])

$\hat{\rho} = 0.0668464|0\rangle\langle0|+0.135745|0\rangle\langle1|+0.156637|1\rangle\langle0|+0.838118|1\rangle\langle1|+0.605297|2\rangle\langle0|+0.914712|2\rangle\langle1| $

Precision

In [11]:
DiracNotation.set_properties(precision=2)
In [12]:
op = rand(2,3);
leftdims = [2];
rightdims = [3];
dirac(op, leftdims, rightdims)

$\hat{\rho} = 0.3|0\rangle\langle0|+0.12|0\rangle\langle1|+0.8|0\rangle\langle2|+0.72|1\rangle\langle0|+0.77|1\rangle\langle1|+0.035|1\rangle\langle2| $

Disable mathjax

In [13]:
DiracNotation.set_properties(islatex=false)
In [14]:
op = rand(2,3);
leftdims = [2];
rightdims = [3];
dirac(op, leftdims, rightdims)
ρ = 0.48|0⟩⟨0|+0.95|0⟩⟨1|+0.12|0⟩⟨2|+0.9|1⟩⟨0|+0.8|1⟩⟨1|+0.11|1⟩⟨2|

display few terms

In [15]:
nterms = 4
DiracNotation.set_properties(islatex=true, displayall=false, numhead=nterms)
dim = 7
ket = normalize(rand(dim))
dirac(ket, [dim])

$|\psi\rangle = 0.065|0\rangle+0.64|1\rangle+0.086|2\rangle+0.69|3\rangle +... $

newline

In [16]:
# newline is supported when mathjax is disable
DiracNotation.set_properties(islatex=false, displayall=true, newline=true)

op = rand(2,3);
leftdims = [2];
rightdims = [3];
dirac(op, leftdims, rightdims)
ρ = 0.87|0⟩⟨0|+0.59|0⟩⟨1|+0.74|0⟩⟨2|
   +0.65|1⟩⟨0|+0.63|1⟩⟨1|+0.55|1⟩⟨2|
   

Restore to original setting

In [17]:
Random.seed!(0);
In [18]:
DiracNotation.reset_properties()
In [19]:
ket = normalize(rand(4)); bra = ket';
In [20]:
dirac(ket)

$|\psi\rangle = 0.65825|00\rangle+0.727547|01\rangle+0.131519|10\rangle+0.141719|11\rangle $

In [21]:
dirac(bra)

$\langle\psi| = 0.65825\langle00|+0.727547\langle01|+0.131519\langle10|+0.141719\langle11| $

In [22]:
dirac(ket)

$|\psi\rangle = 0.65825|00\rangle+0.727547|01\rangle+0.131519|10\rangle+0.141719|11\rangle $

In [23]:
op = rand(2,2);
dirac(op)

$\hat{\rho} = 0.27888|0\rangle\langle0|+0.0423017|0\rangle\langle1|+0.203477|1\rangle\langle0|+0.0682693|1\rangle\langle1| $

In [24]:
op = rand(4,6);
dirac(op, [2,2], [2,3])

$\hat{\rho} = 0.361828|00\rangle\langle00|+0.260036|00\rangle\langle01|+0.575887|00\rangle\langle02|+0.469304|00\rangle\langle10|+0.043141|00\rangle\langle11|+0.605297|00\rangle\langle12|+0.973216|01\rangle\langle00|+0.910047|01\rangle\langle01|+0.868279|01\rangle\langle02|+0.0623676|01\rangle\langle10|+0.267985|01\rangle\langle11|+0.135745|01\rangle\langle12|+0.585812|10\rangle\langle00|+0.167036|10\rangle\langle01|+0.9678|10\rangle\langle02|+0.353129|10\rangle\langle10|+0.0668464|10\rangle\langle11|+0.838118|10\rangle\langle12|+0.539289|11\rangle\langle00|+0.655448|11\rangle\langle01|+0.76769|11\rangle\langle02|+0.767602|11\rangle\langle10|+0.156637|11\rangle\langle11|+0.914712|11\rangle\langle12| $

QuantumOptics.jl

In [25]:
using QuantumOptics

Bra, Ket and Operator

In [26]:
b = SpinBasis(1//2)
up = spinup(b)
dirac(up)

$|\psi\rangle = |0\rangle $

In [27]:
down = spindown(b)
dirac(down)

$|\psi\rangle = |1\rangle $

In [28]:
psi = randstate(b)
dirac(psi)

$|\psi\rangle = (0.247822+0.419022im)|0\rangle+(0.564944+0.666215im)|1\rangle $

In [29]:
dirac(dagger(psi))

$\langle\psi| = (0.247822-0.419022im)\langle0|+(0.564944-0.666215im)\langle1| $

In [30]:
A = psi  dagger(psi); @show A.data
dirac(A)

$\rho = 0.236995|0\rangle\langle0|+(0.419164+0.0716216im)|0\rangle\langle1|+(0.419164-0.0716216im)|1\rangle\langle0|+0.763005|1\rangle\langle1| $

A.data = Complex{Float64}[0.236995+0.0im 0.419164+0.0716216im; 0.419164-0.0716216im 0.763005+0.0im]
In [31]:
dirac(A  A)

$\rho = 0.0561667|00\rangle\langle00|+(0.09934+0.016974im)|00\rangle\langle01|+(0.09934+0.016974im)|00\rangle\langle10|+(0.170569+0.0600425im)|00\rangle\langle11|+(0.09934-0.016974im)|01\rangle\langle00|+0.180828|01\rangle\langle01|+0.180828|01\rangle\langle10|+(0.319824+0.0546476im)|01\rangle\langle11|+(0.09934-0.016974im)|10\rangle\langle00|+0.180828|10\rangle\langle01|+0.180828|10\rangle\langle10|+(0.319824+0.0546476im)|10\rangle\langle11|+(0.170569-0.0600425im)|11\rangle\langle00|+(0.319824-0.0546476im)|11\rangle\langle01|+(0.319824-0.0546476im)|11\rangle\langle10|+0.582176|11\rangle\langle11| $

Composite system

Because of QuantumOptics.jl's data structure, the state is displayed reversely. If you define a state as follows

state = ψ  ϕ

the state is displayed as $|\phi \psi \rangle$

In [32]:
b = SpinBasis(1//2)
psi0 = spinup(b)
psi1 = spindown(b)
dirac(psi0  psi1)

$|\psi\rangle = |10\rangle $

In [33]:
dirac(psi1  psi0)

$|\psi\rangle = |01\rangle $