A technique derived from two related methods suggested earlier by some of the authors for optimization of finite-difference grids and absorbing boundary conditions is applied to discretization of perfectly matched layer (PML) absorbing boundary conditions for wave equations in Cartesian coordinates. We formulate simple sufficient conditions for optimality and implement them. It is found that the minimal error can be achieved using pure imaginary coordinate stretching. As such, the PML discretization is algebraically equivalent to the rational approximation of the square root on [0,1] conventionally used for approximate absorbing boundary conditions. We present optimal solutions for two cost functions, with exponential (and exponential of the square root) rates of convergence with respect to the number of the discrete PML layers using a second order finite-difference scheme with optimal grids. Results of numerical calculations are presented.