def test_acts_like_kron_multiplies_sizes(): assert np.allclose(cirq.kron_with_controls(), np.eye(1)) assert np.allclose(cirq.kron_with_controls(np.eye(2), np.eye(3), np.eye(4)), np.eye(24)) u = np.array([[2, 3], [5, 7]]) assert np.allclose( cirq.kron_with_controls(u, u), np.array([[4, 6, 6, 9], [10, 14, 15, 21], [10, 15, 14, 21], [25, 35, 35, 49]]), )
def test_supports_controls(): u = np.array([[2, 3], [5, 7]]) assert np.allclose(cirq.kron_with_controls(cirq.CONTROL_TAG), np.array([[1, 0], [0, 1]])) assert np.allclose( cirq.kron_with_controls(cirq.CONTROL_TAG, u), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 2, 3], [0, 0, 5, 7]])) assert np.allclose( cirq.kron_with_controls(u, cirq.CONTROL_TAG), np.array([[1, 0, 0, 0], [0, 2, 0, 3], [0, 0, 1, 0], [0, 5, 0, 7]]))
def test_kron_factor_fail(): with pytest.raises(ValueError): _ = cirq.kron_factor_4x4_to_2x2s( cirq.kron_with_controls(cirq.CONTROL_TAG, X)) with pytest.raises(ValueError): _ = cirq.kron_factor_4x4_to_2x2s(np.diag([1, 1, 1, 1j]))
def test_kron_factor_fail(): mat = cirq.kron_with_controls(cirq.CONTROL_TAG, X) g, f1, f2 = cirq.kron_factor_4x4_to_2x2s(mat) with pytest.raises(ValueError): assert_kronecker_factorization_not_within_tolerance(mat, g, f1, f2) mat = cirq.kron_factor_4x4_to_2x2s(np.diag([1, 1, 1, 1j])) with pytest.raises(ValueError): assert_kronecker_factorization_not_within_tolerance(mat, g, f1, f2)
def test_controlled_op_to_operations_equivalent_on_known_and_random(mat): qc = cirq.NamedQubit('c') qt = cirq.NamedQubit('qt') operations = cirq.controlled_op_to_operations( control=qc, target=qt, operation=mat) actual_effect = _operations_to_matrix(operations, (qc, qt)) intended_effect = cirq.kron_with_controls(cirq.CONTROL_TAG, mat) assert cirq.allclose_up_to_global_phase(actual_effect, intended_effect)
def test_controlled_op_to_gates_equivalent_on_known_and_random(mat): qc = cirq.QubitId() qt = cirq.QubitId() operations = decompositions.controlled_op_to_native_gates( control=qc, target=qt, operation=mat) actual_effect = _operations_to_matrix(operations, (qc, qt)) intended_effect = cirq.kron_with_controls(cirq.CONTROL_TAG, mat) assert cirq.allclose_up_to_global_phase(actual_effect, intended_effect)
def test_controlled_op_to_gates_equivalent_on_known_and_random(mat): qc = cirq.QubitId() qt = cirq.QubitId() operations = decompositions.controlled_op_to_native_gates(control=qc, target=qt, operation=mat) actual_effect = _operations_to_matrix(operations, (qc, qt)) intended_effect = cirq.kron_with_controls(cirq.CONTROL_TAG, mat) assert cirq.allclose_up_to_global_phase(actual_effect, intended_effect)