BernDirac


A Wolfram Mathematica package for performing calculations involving matrices/vectors in the Dirac notation which is usually used in quantum mechanics/quantum computing. It utilises the built-in functions without predefined meanings, namely Ket[], Bra[], and CircleTimes[], along with their respective alias, | ⟩ ↔ escketesc, ⟨ | ↔ escbraesc and ⊗ ↔ escc*esc.
The basis which this package works in is {|0⟩,|1⟩}, which is also known as the computational basis or the Z basis.
The package was written in Wolfram Mathematica version 12.2 in Windows 10 and it has zero dependencies.
Bernard Wo - [email protected]
Project Link: https://github.com/bernie-wu/BernDirac
How to use?
Download BernDirac.wl and place it wherever you like. Then, in your Mathematica notebook, run the following line to load the package into your current Mathematica session:
Get[<path-to-BernDirac.wl>];
Functions that this package provides
After loading BernDirac.wl into your Mathematica notebook session, the following additional functions become available to use:
The special quantities, namely the Bell states also become available to use via Ket[] and Bra[] (see section Bell states).
Ket[]
Ket[] is used to denote a column vector. The alias | ⟩ for Ket[] can be obtained with escketesc.
The allowed input for Ket[] is either 0 or 1 and the output for each case is as shown here:
![Ket[0] In:Ket[0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/ket0_in.svg?v=3)

![Ket[1] In:Ket[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/ket1_in.svg?v=3)

Ket[] also supports multiple inputs, as long as they are 0 and 1.
![Ket[1,1,0] In:Ket[1,1,0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/ket110_in.svg?v=3)
![{{0},{0},{0},{0},{0},{0},{1},{0}} Out:Ket[1,1,0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/ket110_out.svg?v=3)
Note that Ket[1,1,0] is equivalent to Ket[1]⊗Ket[1]⊗Ket[0] (see CircleTimes[]).
Bra[]
Bra[] is used to denote a row vector (i.e. | ⟩=(⟨ |)† where † denotes conjugate transpose). The alias ⟨ | for Bra[] can be obtained with escbraesc.
The allowed input for Bra[] is either 0 or 1 and the output for each case is as shown here:
![Bra[0] In:Bra[0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/bra0_in.svg?v=3)

![Bra[1] In:Bra[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/bra1_in.svg?v=3)

Just like Ket[], Bra[] also supports multiple inputs, as long as they are 0 and 1.
![Bra[1,1,0] In:Bra[1,1,0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/bra110_in.svg?v=3)
![{{0,0,0,0,0,0,1,0}} Out:Bra[1,1,0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/bra110_out.svg?v=3)
Note that Bra[1,1,0] is equivalent to Bra[1]⊗Bra[1]⊗Bra[0] (see CircleTimes[]).
CircleTimes[]
The alias ⊗ for CircleTimes[], is used to denote the Kronecker product (sometimes also called Tensor product). Use escc*esc to obtain the alias.
Below, we show that ⊗ works for multiple column vectors, row vectors, and square matrices.
Column vector
![Ket[1]⊗Ket[1]⊗Ket[0] In:Ket[1]⊗Ket[1]⊗Ket[0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/ket110_tensor_in.svg?v=3)
![{{0},{0},{0},{0},{0},{0},{1},{0}} Out:Ket[1]⊗Ket[1]⊗Ket[0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/ket110_out.svg?v=3)
Row vector
![Bra[1]⊗Bra[1]⊗Bra[0] In:Bra[1]⊗Bra[1]⊗Bra[0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/bra110_tensor_in.svg?v=3)
![{{0,0,0,0,0,0,1,0}} Out:Bra[1,1,0]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/bra110_out.svg?v=3)
Square matrix
![{{{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}} Out:Ket[0].Bra[0]⊗Ket[1].Bra[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/BraKet/braket00_11_tensor_out.svg?v=3)
DiracForm[] prints the vector or matrix using the Dirac notation. It works for column vectors, row vectors, and square matrices.
Column vector
![αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1] In:αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/%25CE%25B1ket00_%25CE%25B2ket11_tensor_dirac_in.svg?v=3)
![α|0,0⟩+β|1,1⟩ Out:αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Ket/%25CE%25B1ket00_%25CE%25B2ket11_tensor_dirac_out.svg?v=3)
Row vector
![αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1] In:αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/%25CE%25B1bra00_%25CE%25B2bra11_tensor_dirac_in.svg?v=3)
![α⟨0,0|+β⟨1,1| Out:αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bra/%25CE%25B1bra00_%25CE%25B2bra11_tensor_dirac_out.svg?v=3)
Square matrix
![(αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]).(αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1]) In:(αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]).(αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1])](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/BraKet/%25CE%25B1ket00_%25CE%25B2ket11_%25CE%25B1bra00_%25CE%25B2bra11_tensor_dirac_in.svg?v=3)
![α²|0,0⟩.⟨0,0|+αβ|0,0⟩.⟨1,1|+αβ|1,1⟩.⟨0,0|+β²|1,1⟩.⟨1,1| Out:(αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]).(αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1])](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/BraKet/%25CE%25B1ket00_%25CE%25B2ket11_%25CE%25B1bra00_%25CE%25B2bra11_tensor_dirac_out.svg?v=3)


Bell states
By using the special letter capital dotted Φ and subscripts, we can access the four Bell states using Ket[] and Bra[]. Note that the special dotted Φ is known as FormalCapitalPhi in the documentation (for more information, see the formal letters section here).
The dotted Φ alias can be accessed with esc.CapitalPhiesc, and inputting subscript(s) can be achieved with ctrl+_.
![Ket[FormalCapitalPhi_00] In:Ket[FormalCapitalPhi_00]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi00_in.svg?v=3)
![|0,0⟩/√2+|1,1⟩/√2 Out:Ket[FormalCapitalPhi_00]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi00_out.svg?v=3)
![Ket[FormalCapitalPhi_01] In:Ket[FormalCapitalPhi_01]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi01_in.svg?v=3)
![|0,1⟩/√2+|1,0⟩/√2 Out:Ket[FormalCapitalPhi_01]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi01_out.svg?v=3)
![Ket[FormalCapitalPhi_10] In:Ket[FormalCapitalPhi_10]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi10_in.svg?v=3)
![|0,0⟩/√2-|1,1⟩/√2 Out:Ket[FormalCapitalPhi_10]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi10_out.svg?v=3)
![Ket[FormalCapitalPhi_11] In:Ket[FormalCapitalPhi_11]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi11_in.svg?v=3)
![|0,1⟩/√2-|1,0⟩/√2 Out:Ket[FormalCapitalPhi_11]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/Bell_states/phi11_out.svg?v=3)
PartialTr[]
PartialTr[] performs partial trace of a given system over the specified indices. This function takes 2 input arguments. The first input must be a density matrix (i.e. square matrix). The second input is a list of integer(s) indicating the indices where you would like to perform partial trace over.
![PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1}] In:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1}]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/PartialTr/partialtr1_%25CE%25B1ketbra011_%25CE%25B2ketbra110_in.svg?v=3)
![β|1,0⟩.⟨1,0|+α|1,1⟩.⟨1,1| Out:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1}]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/PartialTr/partialtr1_%25CE%25B1ketbra011_%25CE%25B2ketbra110_out.svg?v=3)
![PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1,3}] In:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1,3}]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/PartialTr/partialtr13_%25CE%25B1ketbra011_%25CE%25B2ketbra110_in.svg?v=3)
![(α+β)|1⟩.⟨1| Out:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1,3}]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/PartialTr/partialtr13_%25CE%25B1ketbra011_%25CE%25B2ketbra110_out.svg?v=3)
![PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{2,3}] In:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{2,3}]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/PartialTr/partialtr23_%25CE%25B1ketbra011_%25CE%25B2ketbra110_in.svg?v=3)
![α|0⟩.⟨0|+β|1⟩.⟨1| Out:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{2,3}]](https://api.gitread.org/api/repo-asset/bernwo/BernDirac/main/Image/PartialTr/partialtr23_%25CE%25B1ketbra011_%25CE%25B2ketbra110_out.svg?v=3)
Example
Applying bit-flip on one qubit in a system of 3 qubits


Partial trace of a Bell state


Density matrix after measuring one qubit in a Bell state


Matrix exponential of the effective Hamiltonian of the spin states in a Mølmer-Sørensen gate


Examples in Mathematica notebook
A Mathematica notebook .nb file showing some usage examples can be found in the Example folder.
Who am I and why did I create this package?
At the time of writing this I am a graduate student with homework assignments involved in heavy and tedious quantum mechanics calculations.
I created this Mathematica package to ease my life and since it helped me a tonne, I figured I should share this with the public too in case someone is also in the same boat as me.
I also know that there are other Mathematica packages out there that provides Dirac notation but those are too complicated to use for my taste, which is also partially why I made this package.
TO-DO
评论