  Spin glasses featured by frustrated interactions and metastable states have
important applications in chemistry, material sciences and artificial neural
networks. However, the solution of the spin glass models is hindered by the
computational complexity that exponentially increases with the sample size.
Photonic Ising machines based on spatial light modulation can speed up the
calculation by obtaining the Hamiltonian from the modulated light intensity.
However, the large-scale generalization to various spin couplings and higher
dimensions is still elusive. Here, we develop a Fourier-mask method to program
the spin couplings in photonic Ising machines. We observe the phase transition
of the two-dimensional Mattis model and the J$\mathrm{_1}$-J$\mathrm{_2}$ model
and study the critical phenomena. We also demonstrate that the
three-dimensional Ising model, which has not been analytically solved, can be
effectively constructed and simulated in two-dimensional lattices with Fourier
masks. Our strategy provides a flexible route to tuning couplings and
dimensions of statistical spin models, and improves the applicability of
optical simulation in neural networks and combinatorial optimization problems.
