Note that your equation is quadratic in $X$ and therefore the solution is not unique, and not even guaranteed to exist. For example, if $X$ is a solution, then so is $-X$. Depending on the structure of $Y$ and $Z$, you might have more.
Also, the matrix $Z$ is similar to the matrix $Y$. That is, they both represent the same linear transformation but in different bases. Therefore, your equation does not have a solution if, $\operatorname{tr}(A)\ne\operatorname{tr}(B)$ or $\operatorname{det}(A)\ne\operatorname{det}(B)$.
Otherwise there exist a solution, but as I said, it is not unique. You can easily find it by solving, enrtywise, the 4 equations.