Matrix Utilities

Symbols

OpenQuantumBase.PauliVecConstant
PauliVec

A constant that holds the eigenvectors of the Pauli matrices. Indices 1, 2 and 3 corresponds to the eigenvectors of $σ_x$, $σ_y$ and $σ_z$.

Examples

julia> σx*PauliVec[1][1] == PauliVec[1][1]
true
OpenQuantumBase.:⊗Function
⊗(A, B)

Calculate the tensor product of A and B.

Examples

julia> σx⊗σz
4×4 Array{Complex{Float64},2}:
 0.0+0.0im   0.0+0.0im  1.0+0.0im   0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -1.0+0.0im
 1.0+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im
 0.0+0.0im  -1.0+0.0im  0.0+0.0im  -0.0+0.0im

Math utilities

OpenQuantumBase.check_positivityMethod
check_positivity(m)

Check if matrix m is positive. Return true is the minimum eigenvalue of m is greater than or equal to 0.

OpenQuantumBase.check_unitaryMethod
check_unitary(𝐔; rtol, atol)

Test if 𝐔 is a unitary matrix. The function checks how close both $𝐔𝐔^†$ and $𝐔^†𝐔$ are to $I$, with relative and absolute error given by rtol, atol.

Examples

julia> check_unitary(exp(-1.0im*5*0.5*σx))
true
OpenQuantumBase.check_density_matrixMethod
check_density_matrix(ρ; atol, rtol)

Check whether matrix ρ is a valid density matrix. atol and rtol is the absolute and relative error tolerance to use when checking ρ ≈ 1.

Examples

julia> check_density_matrix(σx)
false
julia> check_density_matrix(σi/2)
true
OpenQuantumBase.fidelityMethod
fidelity(ρ, σ)

Calculate the fidelity between two density matrices ρ and σ: $Tr[\sqrt{\sqrt{ρ}σ\sqrt{ρ}}]²$.

Examples

julia> ρ = PauliVec[1][1]*PauliVec[1][1]'
julia> σ = PauliVec[3][1]*PauliVec[3][1]'
julia> fidelity(ρ, σ)
0.49999999999999944
OpenQuantumBase.partial_traceMethod
partial_trace(ρ, qubit_2_keep)

Calculate the partial trace of the density matrix ρ, assuming all the subsystems are qubits. qubit_2_keep denotes the indices whose corresponding qubits are not traced out.

Examples

julia> ρ1 = [0.4 0.2; 0.2 0.6]; ρ2 = [0.5 0; 0 0.5];
julia> partial_trace(ρ1⊗ρ2, [1])
2×2 Array{Float64,2}:
 0.4  0.2
 0.2  0.6
OpenQuantumBase.partial_traceMethod
partial_trace(ρ, sys_dim, dim_2_keep)

Calculate the partial trace of the density matrix ρ. Assume ρ is in the tensor space of $ℋ₁⊗ℋ₂⊗…$, sys_dim is an array of the corresponding sub-system dimensions. dim_2_keep is an array of the indices whose corresponding subsystems are not traced out.

Examples

julia> ρ1 = [0.4 0.2; 0.2 0.6]; ρ2 = [0.5 0; 0 0.5];
julia> partial_trace(ρ1⊗ρ2, [2, 2], [1])
2×2 Array{Float64,2}:
 0.4  0.2
 0.2  0.6
OpenQuantumBase.matrix_decomposeMethod
matrix_decompose(mat, basis)

Decompse matrix mat onto matrix basis basis

Examples

julia> matrix_decompose(1.0*σx+2.0*σy+3.0*σz, [σx,σy,σz])
3-element Array{Complex{Float64},1}:
 1.0 + 0.0im
 2.0 + 0.0im
 3.0 + 0.0im

Construction utilities

OpenQuantumBase.q_translateMethod
q_translate(h::String; sp = false)

Convert a string h representing multi-qubits Pauli matrices summation into its numerical form. Generate sparse matrix when sp is set to true.

Examples

julia> q_translate("X+2.0Z")
2×2 Array{Complex{Float64},2}:
 2.0+0.0im   1.0+0.0im
 1.0+0.0im  -2.0+0.0im
OpenQuantumBase.q_translate_stateMethod
q_translate_state(h::String; normal=false)

Convert a string representation of quantum state to a vector. The keyword argument normal indicates whether to normalize the output vector. (Currently only '0' and '1' are supported)

Examples

Single term:

julia> q_translate_state("001")
8-element Array{Complex{Float64},1}:
 0.0 + 0.0im
 1.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im
 0.0 + 0.0im

Multiple terms:

julia> q_translate_state("(101)+(001)", normal=true)
8-element Array{Complex{Float64},1}:
                0.0 + 0.0im
 0.7071067811865475 + 0.0im
                0.0 + 0.0im
                0.0 + 0.0im
                0.0 + 0.0im
 0.7071067811865475 + 0.0im
                0.0 + 0.0im
                0.0 + 0.0im
OpenQuantumBase.single_clauseMethod
single_clause(ops::Vector{String}, q_ind, weight, num_qubit; sp=false)

Construct a single clause of the multi-qubits Hamiltonian. ops is a list of Pauli operator names which appear in this clause. q_ind is the list of indices corresponding to the Pauli matrices in ops. weight is the constant factor of this clause. num_qubit is the total number of qubits. A sparse matrix can be construct by setting sp to true. The following example construct a clause of $Z_1 I Z_3/2$.

Examples

julia> single_clause(["z", "z"], [1, 3], 0.5, 3)
8×8 Array{Complex{Float64},2}:
 0.5+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im
 0.0+0.0im  -0.5+0.0im  0.0+0.0im  -0.0+0.0im   0.0+0.0im  -0.0+0.0im   0.0+0.0im  -0.0+0.0im
 0.0+0.0im   0.0+0.0im  0.5+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -0.5+0.0im   0.0+0.0im  -0.0+0.0im   0.0+0.0im  -0.0+0.0im
 0.0+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im  -0.5+0.0im  -0.0+0.0im  -0.0+0.0im  -0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -0.0+0.0im  -0.0+0.0im   0.5-0.0im  -0.0+0.0im   0.0-0.0im
 0.0+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im  -0.0+0.0im  -0.0+0.0im  -0.5+0.0im  -0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -0.0+0.0im  -0.0+0.0im   0.0-0.0im  -0.0+0.0im   0.5-0.0im
OpenQuantumBase.single_clauseMethod
single_clause(ops::Vector{T}, q_ind, weight, num_qubit; sp=false) where T<:AbstractMatrix

Construct a single clause of the multi-qubits Hamiltonian. ops is a list of single-qubit operators that appear in this clause. The sparse identity matrix is used once sp is set to true. The following example construct a clause of $Z_1 I Z_3/2$.

Examples

julia> single_clause([σz, σz], [1, 3], 0.5, 3)
8×8 Array{Complex{Float64},2}:
 0.5+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im
 0.0+0.0im  -0.5+0.0im  0.0+0.0im  -0.0+0.0im   0.0+0.0im  -0.0+0.0im   0.0+0.0im  -0.0+0.0im
 0.0+0.0im   0.0+0.0im  0.5+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im   0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -0.5+0.0im   0.0+0.0im  -0.0+0.0im   0.0+0.0im  -0.0+0.0im
 0.0+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im  -0.5+0.0im  -0.0+0.0im  -0.0+0.0im  -0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -0.0+0.0im  -0.0+0.0im   0.5-0.0im  -0.0+0.0im   0.0-0.0im
 0.0+0.0im   0.0+0.0im  0.0+0.0im   0.0+0.0im  -0.0+0.0im  -0.0+0.0im  -0.5+0.0im  -0.0+0.0im
 0.0+0.0im  -0.0+0.0im  0.0+0.0im  -0.0+0.0im  -0.0+0.0im   0.0-0.0im  -0.0+0.0im   0.5-0.0im
OpenQuantumBase.standard_driverMethod
standard_driver(num_qubit; sp=false)

Construct the standard driver Hamiltonian for a system of num_qubit qubits. For example, a two qubits standard driver matrix is $IX + XI$. Generate sparse matrix when sp is set to true.

OpenQuantumBase.local_field_termMethod
local_field_term(h, idx, num_qubit; sp=false)

Construct local Hamiltonian of the form $∑hᵢσᵢᶻ$. idx is the index of all local field terms and h is a list of the corresponding weights. num_qubit is the total number of qubits. Generate sparse matrix when sp is set to true.

Examples

julia> local_field_term([1.0, 0.5], [1, 2], 2) == σz⊗σi+0.5σi⊗σz
true
OpenQuantumBase.two_local_termMethod
two_local_term(j, idx, num_qubit; sp=false)

Construct local Hamiltonian of the form $∑Jᵢⱼσᵢᶻσⱼᶻ$. idx is the index of all two local terms and j is a list of the corresponding weights. num_qubit is the total number of qubits. Generate sparse matrix when sp is set to true.

Examples

julia> two_local_term([1.0, 0.5], [[1,2], [1,3]], 3) == σz⊗σz⊗σi + 0.5σz⊗σi⊗σz
true
OpenQuantumBase.collective_operatorMethod
collective_operator(op, num_qubit; sp=false)

Construct the collective operator for a system of num_qubit qubits. op is the name of the collective Pauli matrix. For example, the following code construct an $IZ + ZI$ matrix. Generate sparse matrix when sp is set to true.

Examples

julia> collective_operator("z", 2)
4×4 Array{Complex{Float64},2}:
 2.0+0.0im  0.0+0.0im  0.0+0.0im   0.0+0.0im
 0.0+0.0im  0.0+0.0im  0.0+0.0im   0.0+0.0im
 0.0+0.0im  0.0+0.0im  0.0+0.0im   0.0+0.0im
 0.0+0.0im  0.0+0.0im  0.0+0.0im  -2.0+0.0im