def test_targeted_left_multiply_matches_kron_then_dot(): t = np.array([1, 2, 3, 4, 5, 6, 7, 8]) m = np.array([[2, 3], [5, 7]]) i = np.eye(2) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[0]), np.dot(cirq.kron(m, i, i), t).reshape((2, 2, 2)), atol=1e-8) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[1]), np.dot(cirq.kron(i, m, i), t).reshape((2, 2, 2)), atol=1e-8) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[2]), np.dot(cirq.kron(i, i, m), t).reshape((2, 2, 2)), atol=1e-8)
def test_targeted_left_multiply_reorders_matrices(): t = np.eye(4).reshape((2, 2, 2, 2)) m = np.array([ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, ]).reshape((2, 2, 2, 2)) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t, target_axes=[0, 1]), m, atol=1e-8) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t, target_axes=[1, 0]), np.array([ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, ]).reshape((2, 2, 2, 2)), atol=1e-8)
def test_targeted_left_multiply_out(): left = np.array([[2, 3], [5, 7]]) right = np.array([1, -1]) out = np.zeros(2) result = cirq.targeted_left_multiply( left_matrix=left, right_target=right, target_axes=[0], out=out ) assert result is out np.testing.assert_allclose(result, np.array([-1, -2]), atol=1e-8)
def test_targeted_left_multiply_matches_kron_then_dot(): t = np.array([1, 2, 3, 4, 5, 6, 7, 8]) m = np.array([[2, 3], [5, 7]]) i = np.eye(2) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[0]), np.dot(cirq.kron(m, i, i), t).reshape((2, 2, 2)), atol=1e-8, ) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[1]), np.dot(cirq.kron(i, m, i), t).reshape((2, 2, 2)), atol=1e-8, ) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[2]), np.dot(cirq.kron(i, i, m), t).reshape((2, 2, 2)), atol=1e-8, ) np.testing.assert_allclose( cirq.targeted_left_multiply(left_matrix=m, right_target=t.reshape((2, 2, 2)), target_axes=[2]), np.dot(cirq.kron(i, i, m), t).reshape((2, 2, 2)), atol=1e-8, ) common = t.reshape((2, 2, 2)) with pytest.raises(ValueError, match="out is"): cirq.targeted_left_multiply(left_matrix=m, right_target=common, out=common, target_axes=[2]) with pytest.raises(ValueError, match="out is"): cirq.targeted_left_multiply(left_matrix=m, right_target=common, out=m, target_axes=[2])
def test_targeted_left_multiply_out(): left = np.array([[2, 3], [5, 7]]) right = np.array([1, -1]) out = np.zeros(2) result = cirq.targeted_left_multiply(left_matrix=left, right_target=right, target_axes=[0], out=out) assert result is out np.testing.assert_allclose( result, np.array([-1, -2]), atol=1e-8)