Python 量子運算(二四):張量積
2023/02/10
-----
Fig. 24.1. Tensor product.
-----
張量積的定義
「In mathematics, the tensor product V⊗ W of two vector spaces V and W (over the same field) is a vector space to which is associated a bilinear map V × W → V ⊗ W that maps a pair (v,w), v ∈ V, w ∈ W to an element of V ⊗ W denoted v ⊗ w. An element of the form v ⊗ w is called the tensor product of v and w.」[1]。
張量積與外積
「If arranged into a rectangular array, the coordinate vector of x ⊗ y is the outer product of the coordinate vectors of x and y. Therefore, the tensor product is a generalization of the outer product.」[1]。
-----
張量積與克羅內克積
「Tensor Product
It is a more general case of the Kronecker product, it takes the right hand operand and duplicates it in every element of the left hand operand. Each of these copies is scalar multiplied by the corresponding element.
張量積
這是 Kronecker 乘積的更一般情況,它採用右邊運算元並將其複製到左邊運算元的每個元素中。 這些副本中的每一個都是純量乘以相應的元素。
」[2]。
「
@classmethod
def _tensor(cls, a, b):
ret = copy.copy(a)
ret._op_shape = a._op_shape.tensor(b._op_shape)
ret._data = np.kron(a.data, b.data)
return ret
」[4]。
-----
代碼 24.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | # Program 24.1:Tensor product import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt class Point: def __init__(self, x, y, z): self.x = x self.y = y self.z = z def Line(ax, A, B): ax.plot([A.x, B.x], [A.y, B.y], [A.z, B.z], 'b') return def Cube(ax, P1, P2): # parallel to x axis for z in range(P1.z, P2.z+1): S = Point(P1.x, P1.y, z) E = Point(P2.x, P1.y, z) Line(ax, S, E) for y in range(P1.y, P2.y+1): S = Point(P1.x, y, P2.z) E = Point(P2.x, y, P2.z) Line(ax, S, E) # parallel to y axis for x in range(P1.x, P2.x+1): S = Point(x, P1.y, P2.z) E = Point(x, P2.y, P2.z) Line(ax, S, E) for z in range(P1.z, P2.z+1): S = Point(P2.x, P1.y, z) E = Point(P2.x, P2.y, z) Line(ax, S, E) # parallel to z axis for x in range(P1.x, P2.x+1): S = Point(x, P1.y, P1.z) E = Point(x, P1.y, P2.z) Line(ax, S, E) for y in range(P1.y, P2.y+1): S = Point(P2.x, y, P1.z) E = Point(P2.x, y, P2.z) Line(ax, S, E) return def Set(ax, lim): ax.set_xlim([0, lim]) ax.set_ylim([0, lim]) ax.set_zlim([0, lim]) ax.set_axis_off() return def Subplot_1(): ax = plt.subplot(221) # string setting s1 = ( r'$\mathbf{v} \otimes \mathbf{w} = \ $' ) s2 = ( r'$\begin{bmatrix}$' r'$v_{1} \mathbf{w} \\$' r'$\vdots \\$' r'$v_{m} \mathbf{w}$' r'$\end{bmatrix}$' ) # string output ax.text(0.1, 0.65, s1) ax.text(0.1, 0.35, s2) plt.title('Tensor Product') ax.text(0.5, 0.05, '(a)', fontsize=20) ax.set_axis_off() return def Subplot_2(): ax = fig.add_subplot(222, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(1, 1, 3) Cube(ax, P1, P2) Set(ax, 6) for i in range(P2.z): for j in range(P2.x): ax.text(j, 0, i, r'$\mathbf{w}$') plt.title('Rank 1 Tensor', color='r') ax.text(P2.x, P2.y, P2.z, r'vector $(\mathbf{v} \otimes \mathbf{w})$') ax.text(4, 0, -1, '(b)', fontsize=20) return def Subplot_3(): ax = fig.add_subplot(223, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(1, 1, 3) Cube(ax, P1, P2) Set(ax, 6) plt.title('Rank 1 Tensor', color='r') ax.text(P2.x, P2.y, P2.z, r'vector $\mathbf{v}$') ax.text(4, 0, -1, '(c)', fontsize=20) return def Subplot_4(): ax = fig.add_subplot(224, projection='3d') # 3d subplot P1 = Point(0, 0, 0) P2 = Point(1, 1, 2) Cube(ax, P1, P2) Set(ax, 6) plt.title("Rank 1 Tensor", color='r') ax.text(P2.x, P2.y, P2.z, r'vector $\mathbf{w}$') ax.text(4, 0, -1, '(d)', fontsize=20) return # figure setting mpl.rcParams['text.usetex'] = True mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}' mpl.rcParams['font.size'] = 40 fig = plt.figure(figsize=(16, 16)) Subplot_1() Subplot_2() Subplot_3() Subplot_4() # plt.savefig('/content/drive/My Drive/pqc/0024_001.png') plt.show() |
解說:
-----
References
# 張量積
[1] Tensor product - Wikipedia
https://en.wikipedia.org/wiki/Tensor_product
# 定義:線性組合
[2] 淺談張量分解(四):外積、Kronecker積和張量積- 知乎
https://zhuanlan.zhihu.com/p/26774182
# 線性組合:兩層網路
[3] The Tensor Product, Demystified
https://www.math3ma.com/blog/the-tensor-product-demystified
# 兩層網路:矩陣
[4] (25) Tensors for Beginners 13: Tensor Product vs Kronecker Product - YouTube
https://www.youtube.com/watch?v=qp_zg_TD0qE
[5] Maths - Tensor operations - Martin Baker
https://www.euclideanspace.com/maths/algebra/matrix/tensor/operations/index.htm
[6] https://qiskit.org/documentation/stubs/qiskit.quantum_info.Operator.tensor.html
https://qiskit.org/documentation/stubs/qiskit.quantum_info.Operator.tensor.html
[7] https://qiskit.org/documentation/_modules/qiskit/quantum_info/operators/operator.html#Operator.tensor
-----
Python 量子運算(目錄)
https://mandhistory.blogspot.com/2022/01/quantum-computing.html
-----
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。