2020import cirq
2121from cirq import value
2222from cirq import unitary_eig
23+ from cirq .linalg .decompositions import MAGIC , MAGIC_CONJ_T
2324
2425X = np .array ([[0 , 1 ], [1 , 0 ]])
2526Y = np .array ([[0 , - 1j ], [1j , 0 ]])
@@ -45,9 +46,7 @@ def assert_kronecker_factorization_not_within_tolerance(matrix, g, f1, f2):
4546
4647
4748def assert_magic_su2_within_tolerance (mat , a , b ):
48- M = cirq .linalg .decompositions .MAGIC
49- MT = cirq .linalg .decompositions .MAGIC_CONJ_T
50- recon = cirq .linalg .combinators .dot (MT , cirq .linalg .combinators .kron (a , b ), M )
49+ recon = cirq .linalg .combinators .dot (MAGIC_CONJ_T , cirq .linalg .combinators .kron (a , b ), MAGIC )
5150 assert np .allclose (recon , mat ), "Failed to decompose within tolerance."
5251
5352
@@ -149,14 +148,15 @@ def test_kron_factor_special_unitaries(f1, f2):
149148 assert_kronecker_factorization_within_tolerance (p , g , g1 , g2 )
150149
151150
152- def test_kron_factor_fail ():
153- mat = cirq .kron_with_controls (cirq .CONTROL_TAG , X )
154- g , f1 , f2 = cirq .kron_factor_4x4_to_2x2s (mat )
155- with pytest .raises (ValueError ):
156- assert_kronecker_factorization_not_within_tolerance (mat , g , f1 , f2 )
157- mat = cirq .kron_factor_4x4_to_2x2s (np .diag ([1 , 1 , 1 , 1j ]))
158- with pytest .raises (ValueError ):
159- assert_kronecker_factorization_not_within_tolerance (mat , g , f1 , f2 )
151+ def test_kron_factor_invalid_input ():
152+ mats = [
153+ cirq .kron_with_controls (cirq .CONTROL_TAG , X ),
154+ np .array ([[1 , 1 , 1 , 1 ], [1 , 1 , 1 , 1 ], [1 , 1 , 1 , 1 ], [1 , 2 , 3 , 4 ]]),
155+ np .diag ([1 , 1 , 1 , 1j ]),
156+ ]
157+ for mat in mats :
158+ with pytest .raises (ValueError , match = "Invalid 4x4 kronecker product" ):
159+ cirq .kron_factor_4x4_to_2x2s (mat )
160160
161161
162162def recompose_so4 (a : np .ndarray , b : np .ndarray ) -> np .ndarray :
@@ -165,8 +165,7 @@ def recompose_so4(a: np.ndarray, b: np.ndarray) -> np.ndarray:
165165 assert cirq .is_special_unitary (a )
166166 assert cirq .is_special_unitary (b )
167167
168- magic = np .array ([[1 , 0 , 0 , 1j ], [0 , 1j , 1 , 0 ], [0 , 1j , - 1 , 0 ], [1 , 0 , 0 , - 1j ]]) * np .sqrt (0.5 )
169- result = np .real (cirq .dot (np .conj (magic .T ), cirq .kron (a , b ), magic ))
168+ result = np .real (cirq .dot (MAGIC_CONJ_T , cirq .kron (a , b ), MAGIC ))
170169 assert cirq .is_orthogonal (result )
171170 return result
172171
@@ -656,7 +655,7 @@ def test_kak_vector_matches_vectorized():
656655 np .testing .assert_almost_equal (actual , expected )
657656
658657
659- def test_KAK_vector_local_invariants_random_input ():
658+ def test_kak_vector_local_invariants_random_input ():
660659 actual = _local_invariants_from_kak (cirq .kak_vector (_random_unitaries ))
661660 expected = _local_invariants_from_kak (_kak_vecs )
662661
@@ -697,7 +696,7 @@ def test_kak_vector_on_weyl_chamber_face():
697696 (np .kron (X , X ), (0 , 0 , 0 )),
698697 ),
699698)
700- def test_KAK_vector_weyl_chamber_vertices (unitary , expected ):
699+ def test_kak_vector_weyl_chamber_vertices (unitary , expected ):
701700 actual = cirq .kak_vector (unitary )
702701 np .testing .assert_almost_equal (actual , expected )
703702
0 commit comments