utils
¤
A toolkit for KAK and U3 decompositions.
Functions:
| Name | Description |
|---|---|
generate_random_unitary_matrix |
Generate a random complex unitary matrix of the specified dimension. |
is_equiv_unitary |
Distinguish whether two unitary operators are equivalent, regardless of the global phase. |
simult_svd |
Simultaneous SVD of two matrices, based on Eckart-Young theorem. |
glob_phase |
Extract the global phase \(\alpha\) from a d x d matrix. \(U = e^{i\alpha} S\) in which S is in SU(d). |
remove_glob_phase |
Remove the global phase of a 2 x 2 unitary matrix by means of ZYZ decomposition. |
kron_factor_4x4_to_2x2s |
Decompose a 4 x 4 matrix U into the Kronecker product of two 2 x 2 unitary matrices \(U = A \otimes B\) and a global scalar factor. |
kak_decompose |
Perform KAK decomposition of an arbitrary two-qubit gate. |
zyz_decompose |
Perform the ZYZ decomposition of a 2 x 2 unitary matrix. |
u3_decompose |
Decompose a 2 x 2 unitary matrix into U3 gate and obtain the parameters and global phase. |
generate_random_unitary_matrix(dim: int, seed: int | None = None) -> np.ndarray
¤
Generate a random complex unitary matrix of the specified dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dim
|
int
|
The dimension of the unitary matrix. |
required |
seed
|
int | None
|
A seed for the random number generator to ensure reproducibility. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A dim x dim complex unitary matrix. |
Source code in quark/circuit/utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
is_equiv_unitary(mat1: np.ndarray, mat2: np.ndarray) -> bool
¤
Distinguish whether two unitary operators are equivalent, regardless of the global phase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat1
|
ndarray
|
The first unitary matrix to compare. |
required |
mat2
|
ndarray
|
The second unitary matrix to compare. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mat1 and mat2 have different dimensions. |
ValueError
|
If mat1 is not unitary. |
ValueError
|
If mat2 is not unitary. |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if mat1 and mat2 are equivalent; False otherwise. |
Source code in quark/circuit/utils.py
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 | |
simult_svd(mat1: np.ndarray, mat2: np.ndarray) -> tuple[tuple[np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray]]
¤
Simultaneous SVD of two matrices, based on Eckart-Young theorem. Given two real matrices A and B who satisfy the condition of simultaneous SVD, then \(A = U D_1 V^{\dagger}, B = U D_2 V^{\dagger}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat1
|
ndarray
|
real matrix |
required |
mat2
|
ndarray
|
real matrix |
required |
Returns:
| Type | Description |
|---|---|
tuple[tuple[ndarray, ndarray], tuple[ndarray, ndarray]]
|
tuple[tuple[np.ndarray, np.ndarray], tuple[np.ndarray, np.ndarray]]: A tuple containing two tuples: The first tuple contains the orthogonal matrices U and V (both in SO(2)). The second tuple contains the diagonal matrices D1 and D2. |
Source code in quark/circuit/utils.py
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 | |
glob_phase(mat: np.ndarray) -> float
¤
Extract the global phase \(\alpha\) from a d x d matrix. \(U = e^{i\alpha} S\) in which S is in SU(d).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
ndarray
|
A d x d unitary matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Global phase rad, in range of (-pi, pi]. |
Source code in quark/circuit/utils.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
remove_glob_phase(mat: np.ndarray) -> np.ndarray
¤
Remove the global phase of a 2 x 2 unitary matrix by means of ZYZ decomposition.
That is, remove \(e^{i\alpha}\) from \(U = e^{i\alpha} R_z(\phi) R_y(\theta) R_z(\lambda)\) and return \(R_z(\phi) R_y(\theta) R_z(\lambda)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
ndarray
|
A 2 x 2 unitary matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A 2 x 2 matrix without global phase. |
Source code in quark/circuit/utils.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
kron_factor_4x4_to_2x2s(mat: np.ndarray) -> tuple[complex, np.ndarray, np.ndarray]
¤
Decompose a 4 x 4 matrix U into the Kronecker product of two 2 x 2 unitary matrices \(U = A \otimes B\) and a global scalar factor.
This function assumes the input matrix is the Kronecker product of two 2x2 unitary matrices. If the matrix is not factorizable as a Kronecker product of two 2x2 unitaries, or if the matrix has a zero determinant, the output may be incorrect or an error is raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
ndarray
|
A 4 x 4 unitary matrix to be factored. |
required |
Returns:
| Type | Description |
|---|---|
tuple[complex, ndarray, ndarray]
|
tuple[complex, np.ndarray, np.ndarray]: A complex scalar g representing the global factor. f1 (np.ndarray): A 2 x 2 matrix, representing part of the Kronecker product. f2 (np.ndarray): Another 2 x 2 matrix, representing part of the Kronecker product. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input matrix cannot be tensor-factored into two 2 x 2 matrices. |
ZeroDivisionError
|
If a zero determinant causes a division by zero during factor extraction. |
Source code in quark/circuit/utils.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
kak_decompose(mat: np.ndarray) -> tuple[list[np.ndarray], list[np.ndarray]]
¤
Perform KAK decomposition of an arbitrary two-qubit gate.
For more detail, please refer to An Introduction to Cartan's KAK Decomposition for QC
Programmers click here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
ndarray
|
A 4 x 4 unitary matrix. |
required |
Returns:
| Type | Description |
|---|---|
tuple[list[ndarray], list[ndarray]]
|
tuple[list[np.ndarray], list[np.ndarray]]: rots1: A list of four 2 x 2 matrices representing the rotation gates acting on the first qubit. rots2: A list of four 2 x 2 matrices representing the rotation gates acting on the second qubit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input matrix is not a valid 4x4 unitary matrix. |
Source code in quark/circuit/utils.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
zyz_decompose(mat: np.ndarray) -> tuple[float, float, float, float]
¤
Perform the ZYZ decomposition of a 2 x 2 unitary matrix.
The decomposition is based on the relation:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
ndarray
|
A 2 x 2 unitary matrix to decompose. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float]
|
tuple[float, float, float, float]: A tuple of four phase angles: theta: The rotation angle of the R_y gate. phi: The first rotation angle of the R_z gate. lambda: The second rotation angle of the R_z gate. alpha: The global phase factor. |
Source code in quark/circuit/utils.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
u3_decompose(mat: np.ndarray) -> tuple[float, float, float, float]
¤
Decompose a 2 x 2 unitary matrix into U3 gate and obtain the parameters and global phase.
The decomposition is based on the relation:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat
|
ndarray
|
A 2 x 2 unitary matrix to decompose. |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float, float]
|
tuple[float, float, float, float]: A tuple containing the three parameters \(\theta\), \(\phi\), \(\lambda\) of a standard U3 gate and global phase \(p\). |
Source code in quark/circuit/utils.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |