Prev Tutorial: Unitary
Next Tutorial: Quantum Information
Abstraction of complex matrix
Constructing a Matrix object
Here is API doc
Default constructor
Python
mat = Matrix()
print("mat",mat)
Output
From numpy.ndarray
Construct based on the passed numpy.ndarray object storing a two-dimensional matrix
Python
import numpy as np
def test():
rows = 3
cols = 4
data = np.random.rand(rows, cols)
data = np.array(data, dtype=complex)
m = Matrix(data)
print("m",m)
if __name__ == "__main__":
test()
Output
m {
{
{(0.633034,0)},{(0.196654,0)},{(0.0279015,0)},{(0.463236,0)},
}
{
{(0.625185,0)},{(0.263143,0)},{(0.785747,0)},{(0.788879,0)},
}
{
{(0.00441403,0)},{(0.96572,0)},{(0.803573,0)},{(0.889913,0)},
}
}
Obtain internal data
Get numpy.ndarray
Get internal data, the result is returned as a numpy.ndarray object
Here is API doc for Matrix.ndarray
Python
import numpy as np
def test():
rows = 3
cols = 4
data = np.random.rand(rows, cols)
data = np.array(data, dtype=complex)
m = Matrix(data)
data_ = m.ndarray()
print("data_",data)
if __name__ == "__main__":
test()
Output
data_ [[0.03511921+0.j 0.75430961+0.j 0.19238195+0.j 0.19057673+0.j]
[0.61574108+0.j 0.89369063+0.j 0.81701418+0.j 0.73896602+0.j]
[0.21010302+0.j 0.35964433+0.j 0.41305091+0.j 0.44447268+0.j]]
Get row total and col total
Get the total number of rows and columns of the matrix
Here is API doc for Matrix.row_total
Here is API doc for Matrix.col_total
Python
import numpy as np
def test():
rows = 3
cols = 4
data = np.random.rand(rows, cols)
data = np.array(data, dtype=complex)
m = Matrix(data)
print("row total:",m.row_total())
print("col total:",m.col_total())
if __name__ == "__main__":
test()
Output
row total: 3
col total: 4
Get Element
Get the value of the matrix element by row index and column index
Here is API doc for Matrix.at
Python
import numpy as np
def test():
rows = 3
cols = 4
data = np.random.rand(rows, cols)
data = np.array(data, dtype=complex)
m = Matrix(data)
print("m:")
for i in range(m.row_total()):
for j in range(m.col_total()):
print(m.at(i,j),end=',')
print()
if __name__ == "__main__":
test()
Output
m:
(0.5387399873890723+0j),(0.5946435956404229+0j),(0.8662388940809534+0j),(0.7465917268170127+0j),
(0.6650096219471038+0j),(0.10914316657637124+0j),(0.5999827262878416+0j),(0.5121762859881978+0j),
(0.43882836608920894+0j),(0.1639091992188828+0j),(0.4683573326093392+0j),(0.9735070812593357+0j),
Application
Hermitian
Determine whether the current matrix is Hermitian using is_hermitian()
Get transpose matrix
Get the transpose matrix of the current matrix using transpose() or T()
Get adjoint matrix
Get the adjoint matrix of the current matrix using hermitian_conjugate() or adjoint()
Get L2
Get the L2 norm of the current matrix using L2()
Python
import numpy as np
def test():
rows = 3
cols = 4
data = np.random.rand(rows, cols)
data = np.array(data, dtype=complex)
m = Matrix(data)
print("is_hermitian",m.is_hermitian())
print("transpose",m.transpose())
print("T",m.T())
print("hermitian_conjugate",m.hermitian_conjugate())
print("adjoint",m.adjoint())
print("L2",m.L2())
if __name__ == "__main__":
test()
Output
is_hermitian False
transpose {
{
{(0.111252,0)},{(0.61036,0)},{(0.4078,0)},
}
{
{(0.694994,0)},{(0.267013,0)},{(0.93402,0)},
}
{
{(0.172868,0)},{(0.167488,0)},{(0.635921,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},
}
}
T {
{
{(0.111252,0)},{(0.61036,0)},{(0.4078,0)},
}
{
{(0.694994,0)},{(0.267013,0)},{(0.93402,0)},
}
{
{(0.172868,0)},{(0.167488,0)},{(0.635921,0)},
}
{
{(0,0)},{(0,0)},{(0,0)},
}
}
hermitian_conjugate {
{
{(0.111252,0)},{(0.61036,0)},{(0.4078,0)},
}
{
{(0.694994,0)},{(0.267013,0)},{(0.93402,0)},
}
{
{(0.172868,0)},{(0.167488,0)},{(0.635921,0)},
}
{
{(0.619626,0)},{(0.823553,0)},{(0.017278,0)},
}
}
adjoint {
{
{(0.111252,0)},{(0.61036,0)},{(0.4078,0)},
}
{
{(0.694994,0)},{(0.267013,0)},{(0.93402,0)},
}
{
{(0.172868,0)},{(0.167488,0)},{(0.635921,0)},
}
{
{(0.619626,0)},{(0.823553,0)},{(0.017278,0)},
}
}
L2 (1.6323515294877904+0j)
Boolean function
Equal
Determine whether the internal data of two Matrix objects are equal
Here is API doc
Python
import numpy as np
def test():
rows = 3
cols = 4
data = np.random.rand(rows, cols)
data = np.array(data, dtype=complex)
m = Matrix(data)
m2 = m
print(m==m2)
if __name__ == "__main__":
test()
Output