@@ -153,6 +153,10 @@ def test_non_unitary_linear_combination_of_gates_has_no_unitary(terms):
153153 ),
154154 ({cirq .X : 2 , cirq .H : 1 }, {'X' : 2 + np .sqrt (0.5 ), 'Z' : np .sqrt (0.5 )}),
155155 ({cirq .XX : - 2 , cirq .YY : 3j , cirq .ZZ : 4 }, {'XX' : - 2 , 'YY' : 3j , 'ZZ' : 4 }),
156+ (
157+ {cirq .X : sympy .Symbol ('x' ), cirq .Y : - sympy .Symbol ('y' )},
158+ {'X' : sympy .Symbol ('x' ), 'Y' : - sympy .Symbol ('y' )},
159+ ),
156160 ),
157161)
158162def test_linear_combination_of_gates_has_correct_pauli_expansion (terms , expected_expansion ):
@@ -206,7 +210,11 @@ def test_linear_combinations_of_gates_invalid_powers(terms, exponent):
206210
207211@pytest .mark .parametrize (
208212 'terms, is_parameterized, parameter_names' ,
209- [({cirq .H : 1 }, False , set ()), ({cirq .X ** sympy .Symbol ('t' ): 1 }, True , {'t' })],
213+ [
214+ ({cirq .H : 1 }, False , set ()),
215+ ({cirq .X ** sympy .Symbol ('t' ): 1 }, True , {'t' }),
216+ ({cirq .X : sympy .Symbol ('t' )}, True , {'t' }),
217+ ],
210218)
211219@pytest .mark .parametrize ('resolve_fn' , [cirq .resolve_parameters , cirq .resolve_parameters_once ])
212220def test_parameterized_linear_combination_of_gates (
@@ -225,7 +233,7 @@ def get_matrix(
225233 cirq .GateOperation ,
226234 cirq .LinearCombinationOfGates ,
227235 cirq .LinearCombinationOfOperations ,
228- ]
236+ ],
229237) -> np .ndarray :
230238 if isinstance (operator , (cirq .LinearCombinationOfGates , cirq .LinearCombinationOfOperations )):
231239 return operator .matrix ()
@@ -243,13 +251,13 @@ def assert_linear_combinations_are_equal(
243251
244252 actual_matrix = get_matrix (actual )
245253 expected_matrix = get_matrix (expected )
246- assert np . allclose (actual_matrix , expected_matrix )
254+ assert cirq . approx_eq (actual_matrix , expected_matrix )
247255
248256 actual_expansion = cirq .pauli_expansion (actual )
249257 expected_expansion = cirq .pauli_expansion (expected )
250258 assert set (actual_expansion .keys ()) == set (expected_expansion .keys ())
251259 for name in actual_expansion .keys ():
252- assert abs (actual_expansion [name ] - expected_expansion [name ]) < 1e-12
260+ assert cirq . approx_eq (actual_expansion [name ], expected_expansion [name ])
253261
254262
255263@pytest .mark .parametrize (
@@ -279,6 +287,8 @@ def assert_linear_combinations_are_equal(
279287 ),
280288 ((cirq .X + cirq .Y + cirq .Z ) ** 0 , cirq .I ),
281289 ((cirq .X - 1j * cirq .Y ) ** 0 , cirq .I ),
290+ (cirq .Y - sympy .Symbol ('s' ) * cirq .Y , (1 - sympy .Symbol ('s' )) * cirq .Y ),
291+ ((cirq .X + cirq .Z ) * sympy .Symbol ('s' ) / np .sqrt (2 ), cirq .H * sympy .Symbol ('s' )),
282292 ),
283293)
284294def test_gate_expressions (expression , expected_result ):
@@ -659,6 +669,10 @@ def test_non_unitary_linear_combination_of_operations_has_no_unitary(terms):
659669 {'IIZI' : 1 , 'IZII' : 1 , 'IZZI' : - 1 },
660670 ),
661671 ({cirq .CNOT (q0 , q1 ): 2 , cirq .Z (q0 ): - 1 , cirq .X (q1 ): - 1 }, {'II' : 1 , 'ZX' : - 1 }),
672+ (
673+ {cirq .X (q0 ): - sympy .Symbol ('x' ), cirq .Y (q0 ): sympy .Symbol ('y' )},
674+ {'X' : - sympy .Symbol ('x' ), 'Y' : sympy .Symbol ('y' )},
675+ ),
662676 ),
663677)
664678def test_linear_combination_of_operations_has_correct_pauli_expansion (terms , expected_expansion ):
@@ -716,6 +730,7 @@ def test_linear_combinations_of_operations_invalid_powers(terms, exponent):
716730 [
717731 ({cirq .H (cirq .LineQubit (0 )): 1 }, False , set ()),
718732 ({cirq .X (cirq .LineQubit (0 )) ** sympy .Symbol ('t' ): 1 }, True , {'t' }),
733+ ({cirq .X (cirq .LineQubit (0 )): sympy .Symbol ('t' )}, True , {'t' }),
719734 ],
720735)
721736@pytest .mark .parametrize ('resolve_fn' , [cirq .resolve_parameters , cirq .resolve_parameters_once ])
@@ -788,6 +803,10 @@ def test_parameterized_linear_combination_of_ops(
788803 cirq .LinearCombinationOfOperations ({cirq .X (q1 ): 2 , cirq .Z (q1 ): 3 }) ** 0 ,
789804 cirq .LinearCombinationOfOperations ({cirq .I (q1 ): 1 }),
790805 ),
806+ (
807+ cirq .LinearCombinationOfOperations ({cirq .X (q0 ): sympy .Symbol ('s' )}) ** 2 ,
808+ cirq .LinearCombinationOfOperations ({cirq .I (q0 ): sympy .Symbol ('s' ) ** 2 }),
809+ ),
791810 ),
792811)
793812def test_operation_expressions (expression , expected_result ):
0 commit comments