def test_linear_combinations_of_operations_valid_powers(terms, exponent, expected_terms): combination = cirq.LinearCombinationOfOperations(terms) actual_result = combination**exponent expected_result = cirq.LinearCombinationOfOperations(expected_terms) assert cirq.approx_eq(actual_result, expected_result) assert len(actual_result) == len(expected_terms)
def test_linear_combination_of_operations_is_consistent(terms): combination_1 = cirq.LinearCombinationOfOperations(terms) combination_2 = cirq.LinearCombinationOfOperations({}) combination_2.update(terms) combination_3 = cirq.LinearCombinationOfOperations({}) for gate, coefficient in terms.items(): combination_3[gate] += coefficient assert combination_1 == combination_2 == combination_3
def test_linear_combination_of_operations_has_correct_pauli_expansion( terms, expected_expansion): combination = cirq.LinearCombinationOfOperations(terms) actual_expansion = cirq.pauli_expansion(combination) assert set(actual_expansion.keys()) == set(expected_expansion.keys()) for name in actual_expansion.keys(): assert abs(actual_expansion[name] - expected_expansion[name]) < 1e-12
def test_parameterized_linear_combination_of_ops(terms, is_parameterized, parameter_names): op = cirq.LinearCombinationOfOperations(terms) assert cirq.is_parameterized(op) == is_parameterized assert cirq.parameter_names(op) == parameter_names resolved = cirq.resolve_parameters(op, {p: 1 for p in parameter_names}) assert not cirq.is_parameterized(resolved)
def test_linear_combinations_of_operations_invalid_powers(terms, exponent): combination = cirq.LinearCombinationOfOperations(terms) with pytest.raises(TypeError): _ = combination**exponent
def test_linear_combination_of_operations_has_correct_matrix( terms, expected_matrix): combination = cirq.LinearCombinationOfOperations(terms) assert np.allclose(combination.matrix(), expected_matrix)
def test_linear_combination_of_operations_has_correct_qubits( terms, expected_qubits): combination = cirq.LinearCombinationOfOperations(terms) assert combination.qubits == expected_qubits
def test_empty_linear_combination_of_operations_accepts_all_operations(op): combination = cirq.LinearCombinationOfOperations({}) combination[op] = -0.5j assert len(combination) == 1
}, 2), ({ cirq.Z(q0): 1, }, -1), ({ cirq.X(q0): 1, }, sympy.Symbol('k')), )) def test_linear_combinations_of_operations_invalid_powers(terms, exponent): combination = cirq.LinearCombinationOfOperations(terms) with pytest.raises(TypeError): _ = combination**exponent @pytest.mark.parametrize('expression, expected_result', ( (cirq.LinearCombinationOfOperations({cirq.XX(q0, q1): 2}), cirq.LinearCombinationOfOperations( {cirq.ParallelGateOperation(cirq.X, (q0, q1)): 2})), (cirq.LinearCombinationOfOperations({cirq.CNOT(q0, q1): 2}), cirq.LinearCombinationOfOperations( { cirq.IdentityGate(2).on(q0, q1): 1, cirq.PauliString({q1: cirq.X}): 1, cirq.PauliString({q0: cirq.Z}): 1, cirq.PauliString({ q0: cirq.Z, q1: cirq.X, }): -1 })), (cirq.LinearCombinationOfOperations({cirq.X(q0): 1})** 2, cirq.LinearCombinationOfOperations({cirq.I(q0): 1})),
}, { 'II': 1, 'ZX': -1 }), )) def test_linear_combination_of_operations_has_correct_pauli_expansion( terms, expected_expansion): combination = cirq.LinearCombinationOfOperations(terms) actual_expansion = cirq.pauli_expansion(combination) assert set(actual_expansion.keys()) == set(expected_expansion.keys()) for name in actual_expansion.keys(): assert abs(actual_expansion[name] - expected_expansion[name]) < 1e-12 @pytest.mark.parametrize('expression, expected_result', ( (cirq.LinearCombinationOfOperations({cirq.XX(q0, q1): 2}), cirq.LinearCombinationOfOperations( {cirq.ParallelGateOperation(cirq.X, (q0, q1)): 2})), (cirq.LinearCombinationOfOperations({cirq.CNOT(q0, q1): 2}), cirq.LinearCombinationOfOperations( { cirq.IdentityGate(2).on(q0, q1): 1, cirq.PauliString({q1: cirq.X}): 1, cirq.PauliString({q0: cirq.Z}): 1, cirq.PauliString({ q0: cirq.Z, q1: cirq.X, }): -1 })), )) def test_operation_expressions(expression, expected_result):
def test_non_unitary_linear_combination_of_operations_has_no_unitary(terms): combination = cirq.LinearCombinationOfOperations(terms) assert not cirq.has_unitary(combination) with pytest.raises((TypeError, ValueError)): _ = cirq.unitary(combination)
def test_unitary_linear_combination_of_operations_has_correct_unitary( terms, expected_unitary): combination = cirq.LinearCombinationOfOperations(terms) assert cirq.has_unitary(combination) assert np.allclose(cirq.unitary(combination), expected_unitary)