SkillAgentSearch skills...

BernDirac

A Mathematica package for performing calculations involving matrices/vectors in the Dirac notation which is usually used in quantum mechanics/quantum computing.

Install / Use

/learn @bernwo/BernDirac

README

BernDirac

Wolfram Language License: GPL v3 Platforms

BernDiracLogo

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, <code>| ⟩ ↔ <kbd>esc</kbd>ket<kbd>esc</kbd></code>, <code>⟨ | ↔ <kbd>esc</kbd>bra<kbd>esc</kbd></code> and <code>⊗ ↔ <kbd>esc</kbd>c*<kbd>esc</kbd></code>.

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.

Contact

Bernard Wo - bernardwu+BernDirac@outlook.my
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 <code><kbd>esc</kbd>ket<kbd>esc</kbd></code>. The allowed input for Ket[] is either 0 or 1 and the output for each case is as shown here:

In:Ket[0]
Out:{{1},{0}}

In:Ket[1]
Out:{{0},{1}}

Ket[] also supports multiple inputs, as long as they are 0 and 1.

In:Ket[1,1,0]
Out:Ket[1,1,0]

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 <code><kbd>esc</kbd>bra<kbd>esc</kbd></code>. The allowed input for Bra[] is either 0 or 1 and the output for each case is as shown here:

In:Bra[0]
Out:{{1,0}}

In:Bra[1]
Out:{{0,1}}

Just like Ket[], Bra[] also supports multiple inputs, as long as they are 0 and 1.

In:Bra[1,1,0]
Out:Bra[1,1,0]

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 <code><kbd>esc</kbd>c*<kbd>esc</kbd></code> to obtain the alias. Below, we show that works for multiple column vectors, row vectors, and square matrices.
Column vector

In:Ket[1]⊗Ket[1]⊗Ket[0]
Out:Ket[1]⊗Ket[1]⊗Ket[0]

Row vector

In:Bra[1]⊗Bra[1]⊗Bra[0]
Out:Bra[1,1,0]

Square matrix

In:Ket[0].Bra[0]⊗Ket[1].Bra[1] Out:Ket[0].Bra[0]⊗Ket[1].Bra[1]

DiracForm[]

DiracForm[] prints the vector or matrix using the Dirac notation. It works for column vectors, row vectors, and square matrices.
Column vector

In:αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]
Out:αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]

Row vector

In:αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1]
Out:αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1]

Square matrix

In:(αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]).(αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1])
Out:(αKet[0]⊗Ket[0]+βKet[1]⊗Ket[1]).(αBra[0]⊗Bra[0]+βBra[1]⊗Bra[1])

In:{{a e+b g,a f+b h},{c e+d g,c f+d h}}
Out:{{a e+b g,a f+b h},{c e+d g,c f+d h}}

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 <code><kbd>esc</kbd>.CapitalPhi<kbd>esc</kbd></code>, and inputting subscript(s) can be achieved with <code><kbd>ctrl</kbd>+<kbd>_</kbd></code>.

In:Ket[FormalCapitalPhi_00]
Out:Ket[FormalCapitalPhi_00]

In:Ket[FormalCapitalPhi_01]
Out:Ket[FormalCapitalPhi_01]

In:Ket[FormalCapitalPhi_10]
Out:Ket[FormalCapitalPhi_10]

In:Ket[FormalCapitalPhi_11]
Out:Ket[FormalCapitalPhi_11]

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.

In:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{1}]
Out: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,3}]
Out: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],{2,3}]
Out:PartialTr[αKet[0,1,1].Bra[0,1,1]+βKet[1,1,0].Bra[1,1,0],{2,3}]

Example

Applying bit-flip on one qubit in a system of 3 qubits

In:Example1
Out:Example1

Partial trace of a Bell state

In:Example2
Out:Example2

Density matrix after measuring one qubit in a Bell state

![In:Example3](Image/Example/eg_postmeasureme

Related Skills

View on GitHub
GitHub Stars65
CategoryDevelopment
Updated16d ago
Forks14

Languages

Mathematica

Security Score

100/100

Audited on Mar 9, 2026

No findings